本节介绍如何使用Uql()
和UQLStream()
方法在数据库中执行UQL。
UQL由嬴图开发,用于操作嬴图数据库。详情参阅UQL文档。
Uql()
在数据库中执行UQL语句。
参数
uql: str
:待执行的UQL语句。config: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves 5 movie nodes from the graph 'miniCircle'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
response, _ := driver.Uql("find().nodes({@movie}) as n 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
UQLStream()
在数据库中执行UQL语句并以增量形式返回结果。在处理大型数据集时,避免同时加载所有数据至内存中。
参数
uql: string
:待执行的UQL语句。cb: func(*http.Response) error
:用于处理流式传入的http.Response
的回调函数;如果处理过程中出现问题,返回错误。config: *configuration.RequestConfig
(可选):请求配置。
返回值
error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
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
}
uql := "n().e().n() as p return p"
err = driver.UQLStream(uql, 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.
完整示例
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.Uql("find().nodes({@movie}) as n return n{*} limit 5", requestConfig)
nodeList, _, _ := response.Alias("n").AsNodes()
for _, node := range nodeList {
fmt.Println(node.Get("name"))
}
}