本节介绍如何使用gql()
和gqlStream()
方法在数据库中执行GQL。
GQL(图查询语言)是ISO标准化的图数据库查询语言。详情参阅GQL文档。
gql()
在数据库中执行GQL语句。
参数
gql: string
:待执行的GQL语句。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Retrieves 5 movie nodes from the graph 'miniCircle' and prints their names
const requestConfig: RequestConfig = {
graph: "miniCircle",
};
let response = await conn.gql(
"MATCH (n:movie) RETURN n LIMIT 5",
requestConfig
);
const nodeList = response.items?.get("n")?.asNodes();
nodeList?.forEach((node) => {
console.log(node.get("name"));
});
The Shawshank Redemption
Farewell My Concubine
Léon: The Professional
Titanic
Life is Beautiful
gqlStream()
在数据库中执行GQL语句并以增量形式返回结果。在处理大型数据集时,避免同时加载所有数据至内存中。
参数
gql: string
:待执行的GQL语句。cb: RequestType.QueryResponseListener
:流处理监听器。config: RequestConfig
(可选):请求配置。
返回值
void
// Retrieves all 1-step paths from the graph 'miniCircle'
const requestConfig: RequestConfig = {
graph: "miniCircle",
};
let count = 0;
let response = await conn.gqlStream(
"MATCH p = ()-[]-() RETURN p",
// Define the event handler functions
{
onStart: () => {
console.log("Stream started.")
},
onData: async (res) => {
let paths = res.items?.get("p")?.asPaths();
count = count + (paths?.length || 0);
console.log(count);
},
onEnd: () => {
console.log("Stream ended.");
},
onError: (err) => {
console.log(err);
}
},
requestConfig
);
Stream started.
1024
2048
3072
3227
3464
3895
Stream ended.
完整示例
import { UltipaDriver } from "@ultipa-graph/ultipa-driver";
import { ULTIPA } from "@ultipa-graph/ultipa-driver/dist/types";
let sdkUsage = async () => {
// URI example: ultipaConfig.hosts: ["mqj4zouys.us-east-1.cloud.ultipa.com:60010"]
const ultipaConfig: ULTIPA.UltipaConfig = {
hosts: ["192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"],
username: "<username>",
password: "<password>"
};
const conn = new UltipaDriver(ultipaConfig);
const isSuccess = await conn.test();
console.log(`Connection succeeds: ${isSuccess}`);
// Retrieves 5 movie nodes from the graph 'miniCircle' and prints their names
const requestConfig: ULTIPA.RequestConfig = {
graph: "miniCircle",
};
const response = await conn.gql(
"MATCH (n:movie) RETURN n LIMIT 5",
requestConfig
);
let nodeList = response.items?.get("n")?.asNodes();
nodeList?.forEach((node) => {
console.log(node.get("name"));
});
};
sdkUsage().catch(console.error);