This section introduces the Gql()
and GQLStream()
methods to execute GQL in the database.
GQL (Graph Query Language) is the ISO-standard query language for graph databases. For detailed information on GQL, refer to the documentation.
Gql()
Executes a GQL query in the database.
Parameters
gql: string
: The GQL query to be executed.config: *configuration.RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.error
: An error object that contains details about any issues encountered during the operation. If the operation succeeds,nil
is returned.
// Retrieves 5 movie nodes from the graph 'miniCircle'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
response, _ := driver.Gql("MATCH (n:movie) RETURN n LIMIT 5", requestConfig)
nodeList, _, _ := response.Alias("n").AsNodes()
for _, node := range nodeList {
fmt.Println(node.Get("name"))
}
The Shawshank Redemption
Farewell My Concubine
Léon: The Professional
Titanic
Life is Beautiful
GQLStream()
Executes a GQL query in the database and returns the results incrementally, allowing handling of large datasets without loading everything into memory at once.
Parameters
gql: string
: The GQL query to be executed.cb: func(*http.Response) error
: A callback function that processes thehttp.Response
as it streams in. The function should return an error if something goes wrong during processing.config: *configuration.RequestConfig
(Optional): Request configuration.
Returns
error
: An error object that contains details about any issues encountered during the operation. If the operation succeeds,nil
is returned.
import (
...
sdkhttp "github.com/ultipa/ultipa-go-sdk/sdk/http" // renamed to avoid conflict
...
)
...
// Retrieves all 1-step paths from the graph 'miniCircle'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
fmt.Println("Stream started.")
cb := func(resp *sdkhttp.Response) error {
// Print query statistics
printers.PrintStatistics(resp.Statistic)
// Try to parse response as paths (assuming RETURN p yields paths)
paths, err := resp.Get(0).AsPaths()
if err != nil {
fmt.Println("Error parsing paths:", err)
return err
}
for i, p := range paths {
fmt.Printf("Path %d: %+v\n", i+1, p)
}
return nil
}
gql := "MATCH p = ()-[]-() RETURN p"
err = driver.GQLStream(gql, cb, requestConfig)
if err != nil {
log.Fatalln("Stream error:", err)
}
fmt.Println("Stream ended.")
Stream started.
Total Cost : 0.67s | Engine Cost : 0s
Path 1: &{NodeUUIDs:[6196955286285058158 7998395137233256563] EdgeUUIDs:[2356] Nodes:map[6196955286285058158:0xc0009e7950 7998395137233256563:0xc0009e7ad0] Edges:map[2356:0xc00029e6e0]}
Path 2: &{NodeUUIDs:[6196955286285058158 9223377534412914884] EdgeUUIDs:[2524] Nodes:map[6196955286285058158:0xc0009e7e30 9223377534412914884:0xc000a14030] Edges:map[2524:0xc00029ed70]}
...
Path 237: &{NodeUUIDs:[18302634383191834827 17942343114467311744] EdgeUUIDs:[2633] Nodes:map[17942343114467311744:0xc0004d3a70 18302634383191834827:0xc0004d38c0] Edges:map[2633:0xc0005374f0]}
Stream ended.
Full Example
package main
import (
"fmt"
"log"
"github.com/ultipa/ultipa-go-sdk/sdk"
"github.com/ultipa/ultipa-go-sdk/sdk/configuration"
)
func main() {
config := &configuration.UltipaConfig{
// URI example: Hosts: []string{"mqj4zouys.us-east-1.cloud.ultipa.com:60010"},
Hosts: []string{"192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"},
Username: "<usernmae>",
Password: "<password>",
}
driver, err := sdk.NewUltipaDriver(config)
if err != nil {
log.Fatalln("Failed to connect to Ultipa:", err)
}
// Retrieves 5 movie nodes from the graph 'miniCircle'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
response, _ := driver.Gql("MATCH (n:movie) RETURN n LIMIT 5", requestConfig)
nodeList, _, _ := response.Alias("n").AsNodes()
for _, node := range nodeList {
fmt.Println(node.Get("name"))
}
}