diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 6ce5140c..1e1ac12d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -20,4 +20,8 @@ jobs: sleep 10 popd go test -v -race - \ No newline at end of file + - name: Run examples + run: | + go run basic_example/graph_client_basic_example.go + go run gorountines_example/graph_client_goroutines_example.go + diff --git a/Makefile b/Makefile index 887e306f..85a2f5ed 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: build test test-dev fmt ci +.PHONY: build test fmt ci run-examples default: build @@ -19,3 +19,7 @@ ci: cd .. && \ go test -v -race; \ cd ./nebula-docker-compose && docker-compose down -v + +run-examples: + go run basic_example/graph_client_basic_example.go + go run gorountines_example/graph_client_goroutines_example.go diff --git a/basic_example/graph_client_basic_example.go b/basic_example/graph_client_basic_example.go index 920aef7e..ea93155a 100644 --- a/basic_example/graph_client_basic_example.go +++ b/basic_example/graph_client_basic_example.go @@ -13,10 +13,12 @@ import ( ) const ( - address = "127.0.0.1" - port = 9669 - username = "user" - password = "password" + address = "127.0.0.1" + // The default port of Nebula Graph 2.x is 9669. + // 3699 is only for testing. + port = 3699 + username = "root" + password = "nebula" ) // Initialize logger @@ -47,14 +49,14 @@ func main() { checkResultSet := func(prefix string, res *nebula.ResultSet) { if !res.IsSucceed() { - fmt.Printf("%s, ErrorCode: %v, ErrorMsg: %s", prefix, res.GetErrorCode(), res.GetErrorMsg()) + log.Fatal(fmt.Sprintf("%s, ErrorCode: %v, ErrorMsg: %s", prefix, res.GetErrorCode(), res.GetErrorMsg())) } } { // Prepare the query - createSchema := "CREATE SPACE IF NOT EXISTS test; " + - "USE test;" + + createSchema := "CREATE SPACE IF NOT EXISTS basic_example_space(vid_type=FIXED_STRING(20)); " + + "USE basic_example_space;" + "CREATE TAG IF NOT EXISTS person(name string, age int);" + "CREATE EDGE IF NOT EXISTS like(likeness double)" @@ -66,6 +68,17 @@ func main() { } checkResultSet(createSchema, resultSet) } + // Drop space + { + query := "DROP SPACE IF EXISTS basic_example_space" + // Send query + resultSet, err := session.Execute(query) + if err != nil { + fmt.Print(err.Error()) + return + } + checkResultSet(query, resultSet) + } fmt.Print("\n") log.Info("Nebula Go Client Basic Example Finished") diff --git a/client_test.go b/client_test.go index 2867f0bd..f9d6931f 100644 --- a/client_test.go +++ b/client_test.go @@ -276,7 +276,7 @@ func TestServiceDataIO(t *testing.T) { } checkResultSet(createSchema, resultSet) } - time.Sleep(5 * time.Second) + time.Sleep(3 * time.Second) // Load data { @@ -950,14 +950,13 @@ func startContainer(t *testing.T, containerName string) { } } -func tryToExecute(session *Session, query string) (*ResultSet, error) { - var err error +func tryToExecute(session *Session, query string) (resp *ResultSet, err error) { for i := 3; i > 0; i-- { - resp, err := session.Execute(query) + resp, err = session.Execute(query) if err == nil && resp.IsSucceed() { - return resp, nil + return } time.Sleep(2 * time.Second) } - return nil, err + return } diff --git a/gorountines_example/graph_client_goroutines_example.go b/gorountines_example/graph_client_goroutines_example.go index 72061f9e..27c9406d 100644 --- a/gorountines_example/graph_client_goroutines_example.go +++ b/gorountines_example/graph_client_goroutines_example.go @@ -16,10 +16,12 @@ import ( ) const ( - address = "127.0.0.1" - port = 9669 - username = "user" - password = "password" + address = "127.0.0.1" + // The default port of Nebula Graph 2.x is 9669. + // 3699 is only for testing. + port = 3699 + username = "root" + password = "nebula" ) // Initialize logger @@ -54,11 +56,11 @@ func main() { // Method used to check execution response checkResultSet := func(prefix string, res *nebula.ResultSet) { if !res.IsSucceed() { - fmt.Printf("%s, ErrorCode: %v, ErrorMsg: %s", prefix, res.GetErrorCode(), res.GetErrorMsg()) + log.Fatal(fmt.Sprintf("%s, ErrorCode: %v, ErrorMsg: %s", prefix, res.GetErrorCode(), res.GetErrorMsg())) } } { - createSchema := "CREATE SPACE IF NOT EXISTS example_space; " + + createSchema := "CREATE SPACE IF NOT EXISTS example_space(vid_type=FIXED_STRING(20)); " + "USE example_space;" + "CREATE TAG IF NOT EXISTS person(name string, age int);" + "CREATE EDGE IF NOT EXISTS like(likeness double)" @@ -145,6 +147,17 @@ func main() { // Print ValueWrapper using String() fmt.Printf("Print using ValueWrapper.String(): %s", valueWrapper.String()) } + // Drop space + { + query := "DROP SPACE IF EXISTS example_space" + // Send query + resultSet, err := session.Execute(query) + if err != nil { + fmt.Print(err.Error()) + return + } + checkResultSet(query, resultSet) + } }(&wg) wg.Wait() diff --git a/nebula-docker-compose/docker-compose.yaml b/nebula-docker-compose/docker-compose.yaml index 62e658ee..6e16f2d9 100644 --- a/nebula-docker-compose/docker-compose.yaml +++ b/nebula-docker-compose/docker-compose.yaml @@ -16,6 +16,7 @@ services: - --v=0 - --minloglevel=0 - --timezone_name=+08:00 + - --heartbeat_interval_secs=1 healthcheck: test: ["CMD", "curl", "-sf", "http://metad0:11000/status"] interval: 30s @@ -51,6 +52,7 @@ services: - --v=0 - --minloglevel=0 - --timezone_name=+08:00 + - --heartbeat_interval_secs=1 healthcheck: test: ["CMD", "curl", "-sf", "http://metad1:11000/status"] interval: 30s @@ -86,6 +88,7 @@ services: - --v=0 - --minloglevel=0 - --timezone_name=+08:00 + - --heartbeat_interval_secs=1 healthcheck: test: ["CMD", "curl", "-sf", "http://metad2:11000/status"] interval: 30s @@ -121,6 +124,7 @@ services: - --v=0 - --minloglevel=0 - --timezone_name=+08:00 + - --heartbeat_interval_secs=1 depends_on: - metad0 - metad1 @@ -160,6 +164,7 @@ services: - --v=0 - --minloglevel=0 - --timezone_name=+08:00 + - --heartbeat_interval_secs=1 depends_on: - metad0 - metad1 @@ -199,6 +204,7 @@ services: - --v=0 - --minloglevel=0 - --timezone_name=+08:00 + - --heartbeat_interval_secs=1 depends_on: - metad0 - metad1 @@ -235,9 +241,10 @@ services: - --log_dir=/logs - --v=0 - --minloglevel=0 - - --enable_authorize + - --enable_authorize=true - --timezone_name=+08:00 - --system_memory_high_watermark_ratio=0.99 + - --heartbeat_interval_secs=1 depends_on: - metad0 - metad1 @@ -273,8 +280,10 @@ services: - --log_dir=/logs - --v=0 - --minloglevel=0 + - --enable_authorize=true - --timezone_name=+08:00 - --system_memory_high_watermark_ratio=0.99 + - --heartbeat_interval_secs=1 depends_on: - metad0 - metad1 @@ -310,8 +319,10 @@ services: - --log_dir=/logs - --v=0 - --minloglevel=0 + - --enable_authorize=true - --timezone_name=+08:00 - --system_memory_high_watermark_ratio=0.99 + - --heartbeat_interval_secs=1 depends_on: - metad0 - metad1