本节为您介绍如何在UQL语句中使用Connection
对象的uql()
方法和uqlStream()
方法查询数据库。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
UQL是全面操作嬴图数据库的专用语言。更多嬴图GQL的详细信息,请参阅本文档.
uql()
在当前图集或数据库上执行GQL查询语句并返回查询结果。
Parameters:
string
:待执行的UQL查询语句。RequestConfig
(可选):配置请求。
Returns:
Response
:请求的结果。
// 获取图集miniCircle中@movie下的5个点并打印其名称
let requestConfig = <RequestType.RequestConfig>{
graphSetName: "miniCircle",
useMaster: true,
};
let resp = await conn.uql(
"find().nodes({@movie}) as n return n{*} limit 5",
requestConfig
);
let node_list = resp.data?.get(0).asNodes();
node_list?.forEach((node) => {
console.log(node.get("name"));
});
The Shawshank Redemption
Farewell My Concubine
Léon: The Professional
Titanic
Life is Beautiful
更多示例,请参阅嬴图数据与Node.js类型映射.
uqlStream()
在当前图集或数据库上执行GQL查询语句并以增量形式返回查询结果。在处理大型数据集时,无需立刻加载所有数据至内存中。
参数:
string
:待执行的UQL查询语句。UqlResponseStream
:流处理监听器。RequestConfig
(可选):配置请求。
返回值:
void
// 获取图集miniCircle的1步路径
let requestConfig = <RequestType.RequestConfig>{
graphSetName: "miniCircle",
useMaster: true,
};
let count = 0;
let resp = await conn.uqlStream(
"n().e().n() as paths return paths{*}",
{
onData: async (res) => {
let paths = res.data.get(0).asPaths();
count = count + paths.length;
console.log(count);
},
onEnd: () => {
console.log("END");
},
onStart: () => {
console.log("Start");
},
onError: (err) => {
console.log(err);
},
},
requestConfig
);
Start
1250
1392
END
完整示例
import { ConnectionPool, ULTIPA } from "@ultipa-graph/ultipa-node-sdk";
import { GraphExra } from "@ultipa-graph/ultipa-node-sdk/dist/connection/extra/graph.extra";
import { getEdgesPrintInfo } from "@ultipa-graph/ultipa-node-sdk/dist/printers/edge";
import { RequestType } from "@ultipa-graph/ultipa-node-sdk/dist/types";
import { ListFormat } from "typescript";
let sdkUsage = async () => {
// 设置连接
//URI示例: hosts="mqj4zouys.us-east-1.cloud.ultipa.com:60010"
let hosts = [
"192.168.1.85:60061",
"192.168.1.86:60061",
"192.168.1.87:60061",
];
let username = "***";
let password = "***";
let connPool = new ConnectionPool(hosts, username, password);
// 建立与数据库的连接
let conn = await connPool.getActive();
let isSuccess = await conn.test();
console.log(isSuccess);
// 配置请求
let requestConfig = <RequestType.RequestConfig>{
graphSetName: "miniCircle",
useMaster: true,
};
// 获取10个点并打印返回的第一个点的_id和name属性值
let resp = await conn.uql(
"find().nodes({@movie}) as n return n{*} limit 10",
requestConfig
);
let node_list = resp.data?.get(0).asNodes();
console.log(node_list[0].id, node_list[0].get("name"));
};
sdkUsage().then(console.log).catch(console.log);