DataItem
使用 UQLReply 的 get()
、alias()
方法时将获得 DataItem 类。
DataItem 的方法:
方法 | 类型 | 说明 |
---|---|---|
asNodes() | Node[] | 将 NODE 类型的 DataItem 转为 Node[] |
asEdges() | Edge[] | 将 EDGE 类型的 DataItem 转为 Edge[] |
asPaths() | Path[] | 将 PATH 类型的 DataItem 转为 Path[] |
asGraphs() | ResponseType.Graph[] | 将默认别名 _graph 的 DataItem 转为 Graph[] |
asSchemas() | ResponseType.Graph[] | 将默认别名 _nodeSchema、_edgeSchema 的 DataItem 转为 Schema[] |
asProperties() | ResponseType.Graph[] | 将默认别名 _nodeProperty、_edgeProperty 的 DataItem 转为 Property[] |
asAlgos() | ResponseType.Graph[] | 将默认别名 _algoList 的 DataItem 转为 Algo[] |
asTable() | Table | 将 TABLE 类型的 DataItem 转为 Table |
asArray() | any[][] | 将 ARRAY 类型的 DataItem 转为 any[][] |
asAttrs() | AttrAlias | 将 ATTR 类型的 DataItem 转为 AttrAlias |
toJSONString() | string | 将任意类型的 DataItem 转为 json 格式的字符串 |
也可以使用方法
asTable()
将默认别名 _graph、_nodeSchema、_edgeSchema 等的 DataItem 转为 Table。
Node
Node 类的字段:
字段 | 类型 | 说明 |
---|---|---|
id | string | Node 的 ID |
uuid | string | Node 的 UUID |
schema | string | Node 的 Schema |
values | object | Node 的自定义属性 |
Node 类的方法:
方法 | 类型 | 说明 |
---|---|---|
getID() | string | 获取当前 Node 的 ID |
getUUID() | string | 获取当前 Node 的 UUID |
getSchema() | string | 获取当前 Node 的 Schema |
getValues() | object | 获取当前 Node 的 Values(自定义属性) |
get(prop_name: string) | any | 获取当前 Node 的某个自定义属性 |
set(prop_name: string, value: any) | void | 设置当前 Node 的某个自定义属性,属性名不存在时将则添加该键值对 |
示例:发送 UQL 语句查询一列点,获取第二个点的 ID,同时将第一个点的 rating 改为 8 并输出
import { ConnectionPool, printers } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
// 创建名为 conn 的连接,此部分代码省略
let resp = await conn.uql("find().nodes({@movie}) as nodes return nodes{*} limit 5");
printers.DataItem.node(resp.data?.alias("nodes"));
console.log("ID of 2nd node is: " + resp.data?.alias("nodes").asNodes()[1].getID());
console.log("rating of 1st node was: " + resp.data?.alias("nodes").asNodes()[0].get("rating"));
resp.data?.alias("nodes").asNodes()[0].set("rating",8)
console.log("rating of 1st node now is: " + resp.data?.alias("nodes").asNodes()[0].get("rating"));
};
sdkUsage();
输出:
Alias: nodes Type: RESULT_TYPE_NODE
@movie
┌─────────┬─────────┬──────────────────────────┬────────┬────────┬──────┐
│ (index) │ @schema │ _id │ _uuid │ rating │ year │
├─────────┼─────────┼──────────────────────────┼────────┼────────┼──────┤
│ 0 │ 'movie' │ 'ULTIPA80000000000003E9' │ '1001' │ 9 │ 1994 │
│ 1 │ 'movie' │ 'ULTIPA80000000000003EA' │ '1002' │ 7 │ 1993 │
│ 2 │ 'movie' │ 'ULTIPA80000000000003EB' │ '1003' │ 6 │ 1998 │
│ 3 │ 'movie' │ 'ULTIPA80000000000003EC' │ '1004' │ 9 │ 1994 │
│ 4 │ 'movie' │ 'ULTIPA80000000000003ED' │ '1005' │ 9 │ 1997 │
└─────────┴─────────┴──────────────────────────┴────────┴────────┴──────┘
ID of 2nd node is: ULTIPA80000000000003EA
rating of 1st node was: 9
rating of 1st node now is: 8
Edge
Edge 类的字段:
字段 | 类型 | 说明 |
---|---|---|
uuid | string | Edge 的 UUID |
from_uuid | string | Edge 起点的 UUID |
to_uuid | string | Edge 终点的 UUID |
from | string | Edge 起点的 ID |
to | string | Edge 终点的 ID |
schema | string | Edge 的 Schema |
values | object | Edge 的自定义属性 |
Edge 类的方法:
方法 | 类型 | 说明 |
---|---|---|
getUUID() | string | 获取当前 Edge 的 UUID |
getFromUUID() | string | 获取当前 Edge 起点的 UUID |
getToUUID() | string | 获取当前 Edge 终点的 UUID |
getFrom() | string | 获取当前 Edge 起点的 ID |
getTo() | string | 获取当前 Edge 终点的 ID |
getSchema() | string | 获取当前 Edge 的 Schema |
getValues() | object | 获取当前 Edge 的 Values(自定义属性) |
get(prop_name: string) | any | 获取当前 Edge 的某个自定义属性 |
set(prop_name: string, value: any) | void | 设置当前 Edge 的某个自定义属性,属性名不存在时将则添加该键值对 |
示例:发送 UQL 语句查询一列边,获取第二个边的起点的 ID,为第一个边添加属性 value 并设置为 8
import { ConnectionPool, printers } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
// 创建名为 conn 的连接,此部分代码省略
let resp = await conn.uql("find().edges({@default}) as edges return edges{*} limit 5");
printers.DataItem.edge(resp.data?.alias("edges"));
console.log("ID of start node of 2nd edge is: " + resp.data?.alias("edges").asEdges()[1].getFrom());
console.log("value of 1st edge was: " + resp.data?.alias("edges").asEdges()[0].get("value"));
resp.data?.alias("edges").asEdges()[0].set("value",8)
console.log("value of 1st edge now is: " + resp.data?.alias("edges").asEdges()[0].get("value"));
};
sdkUsage();
输出:
Alias: edges Type: RESULT_TYPE_EDGE
@default
┌─────────┬───────────┬───────┬──────────────────────────┬──────────────────────────┬────────────┬──────────┐
│ (index) │ @schema │ _uuid │ _from │ _to │ _from_uuid │ _to_uuid │
├─────────┼───────────┼───────┼──────────────────────────┼──────────────────────────┼────────────┼──────────┤
│ 0 │ 'default' │ '21' │ 'ULTIPA0000003' │ 'ULTIPA0000004' │ '20' │ '21' │
│ 1 │ 'default' │ '24' │ 'ULTIPA8000000000000001' │ 'ULTIPA8000000000000002' │ '56' │ '34' │
│ 2 │ 'default' │ '29' │ 'ULTIPA800000000000001A' │ 'ULTIPA800000000000001B' │ '24' │ '65' │
└─────────┴───────────┴───────┴──────────────────────────┴──────────────────────────┴────────────┴──────────┘
ID of start node of 2nd edge is: ULTIPA8000000000000001
value of 1st edge was: undefined
value of 1st edge now is: 8
Path
Path 类的字段:
字段 | 类型 | 说明 |
---|---|---|
nodes | Node[] | Path 中的 Node 列表 |
edges | Edge[] | Path 中的 Edge 列表 |
length | number | Path 的长度,即 Edge 的数量 |
Path 类的方法:
方法 | 类型 | 说明 |
---|---|---|
getLength() | number | 获取当前 Path 的长度,即 Edge 的数量 |
getNodes() | Node[] | 获取当前 Path 的 Node 列表 |
getEdges() | Edge[] | 获取当前 Path 的 Edge 列表 |
示例:发送 UQL 语句查询一列路径,获取第一个路径的第二个点
import { ConnectionPool, printers } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
// 创建名为 conn 的连接,此部分代码省略
let resp = await conn.uql("n().e()[2].n() as paths return paths{*} limit 3");
printers.DataItem.path(resp.data?.alias("paths"));
console.log("the 2nd node in the 1st path:");
console.log(resp.data?.alias("paths").asPaths()[0].getNodes()[1]);
};
sdkUsage();
输出:
Alias: paths Type: RESULT_TYPE_PATH
┌─────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ (index) │ Values
│
├─────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ 0 │ '(ULTIPA80000000000003E9) <- [1475] - (ULTIPA8000000000000014) - [1350] -> (ULTIPA8000000000000066)' │
│ 1 │ '(ULTIPA80000000000003E9) <- [1363] - (ULTIPA8000000000000018) - [1353] -> (ULTIPA800000000000005F)' │
│ 2 │ '(ULTIPA80000000000003E9) <- [1645] - (ULTIPA8000000000000016) - [1374] -> (ULTIPA80000000000003FB)' │
└─────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────┘
the 2nd node in the 1st path:
Node {
id: 'ULTIPA8000000000000014',
uuid: '20',
schema: 'account',
values: { name: 'Meng', industry: 'Construction', gender: 'female', year: 1982 }
}
Graph
Graph 类的字段:
字段 | 类型 | 说明 |
---|---|---|
id | string | Graph 的 ID |
name | string | Graph 的名称 |
description | string | Graph 的描述 |
totalNodes | string | Graph 的点数量 |
totalEdges | string | Graph 的边数量 |
status | string | Graph 的状态(MOUNTED、UNMOUNTED) |
clusterID | string | 集群 ID |
示例:发送 UQL 语句查询图集列表,获取第三个图集的名称
import { ConnectionPool, printers } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
// 创建名为 conn 的连接,此部分代码省略
let resp = await conn.uql("show().graph()");
printers.DataItem.table(resp.data?.alias("_graph"));
console.log("name of 3rd graph is: " + resp.data?.alias("_graph").asGraphs()[2].name);
};
sdkUsage();
输出:
Alias: _graph Type: RESULT_TYPE_TABLE
┌─────────┬────────┬────────────────────┬────────────┬────────────┬─────────────┬─────────────┐
│ (index) │ id │ name │ totalNodes │ totalEdges │ description │ status │
├─────────┼────────┼────────────────────┼────────────┼────────────┼─────────────┼─────────────┤
│ 0 │ '1172' │ 'DeliveryCenter' │ '30' │ '77' │ '' │ 'MOUNTED' │
│ 1 │ '1208' │ 'GithubSocial' │ '37700' │ '289003' │ '' │ 'MOUNTED' │
│ 2 │ '1116' │ 'HB_POC' │ '0' │ '0' │ '' │ 'UNMOUNTED' │
│ 3 │ '1601' │ 'Industry' │ '0' │ '0' │ '' │ 'MOUNTED' │
└─────────┴────────┴────────────────────┴────────────┴────────────┴─────────────┴─────────────┘
name of 3rd graph is: HB_POC
Schema
Schema 类的字段:
字段 | 类型 | 说明 |
---|---|---|
name | string | Schema 的名称 |
description | string | Schema 的描述 |
properties | Property[] | Schema 的所有 Property |
totalNodes | string | Schema 的点数量 |
totalEdges | string | Schema 的边数量 |
示例:发送 UQL 语句查询当前图集的点 schema 列表,获取第三个点 schema 的点的数量
import { ConnectionPool, printers } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
// 创建名为 conn 的连接,此部分代码省略
let resp = await conn.uql("show().node_schema()");
printers.DataItem.table(resp.data?.alias("_nodeSchema"));
console.log("number of nodes of 3rd node schema is: " + resp.data?.alias("_nodeSchema").asSchemas()[2].totalNodes);
};
sdkUsage();
输出:
Alias: _nodeSchema Type: RESULT_TYPE_TABLE
┌─────────┬─────────────┬──────────────────┬────────────────────────────────────────────────────────────────────┬────────────┐
│ (index) │ name │ description │ properties │ totalNodes │
├─────────┼─────────────┼──────────────────┼────────────────────────────────────────────────────────────────────┼────────────┤
│ 0 │ 'default' │ 'default schema' │ '[]' │ '0' │
│ 1 │ 'country' │ '' │ '[{"name":"name","type":"string","description":"","lte":"false"}]' │ '23' │
│ 2 │ 'celebrity' │ '' │ '[{"name":"name","type":"string","description":"","lte":"false"}]' │ '78' │
└─────────┴─────────────┴──────────────────┴────────────────────────────────────────────────────────────────────┴────────────┘
number of nodes of 3rd node schema is: 78
Property
Property 类的字段:
字段 | 类型 | 说明 |
---|---|---|
name | string | Property 的名称 |
description | string | Property 的描述 |
schema | string | Property 的 Schema |
type | string | Property 的数据类型 |
lte | string | Property 的 LTE 状态(true、false、creating) |
示例:发送 UQL 语句查询当前图集的点 schema 'account' 的属性列表,获取第四个点属性的 LTE 信息
import { ConnectionPool, printers } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
// 创建名为 conn 的连接,此部分代码省略
let resp = await conn.uql("show().node_property(@account)");
printers.DataItem.table(resp.data?.alias("_nodeProperty"));
console.log("lte of 4th node property is: " + resp.data?.alias("_nodeProperty").asProperties()[3].lte);
};
sdkUsage();
输出:
Alias: _nodeProperty Type: RESULT_TYPE_TABLE
@account
┌─────────┬────────────┬──────────┬─────────┬───────────┬─────────────┐
│ (index) │ name │ type │ lte │ schema │ description │
├─────────┼────────────┼──────────┼─────────┼───────────┼─────────────┤
│ 0 │ 'name' │ 'string' │ 'false' │ 'account' │ '' │
│ 1 │ 'industry' │ 'string' │ 'false' │ 'account' │ '' │
│ 2 │ 'gender' │ 'string' │ 'false' │ 'account' │ '' │
│ 3 │ 'year' │ 'int32' │ 'true' │ 'account' │ '' │
└─────────┴────────────┴──────────┴─────────┴───────────┴─────────────┘
lte of 4th node property is: true
Algo
Algo 类的字段:
字段 | 类型 | 说明 |
---|---|---|
name | string | Algo 的名称 |
param | object | Algo 的参数,包括算法名、描述、参数列表、版本号等信息 |
result_opt | object | Algo 的结果选项 |
clusterId | string | 集群 ID |
示例:发送 UQL 语句查询已安装的算法列表,获取第三个算法的名称及版本
import { ConnectionPool, printers } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
// 创建名为 conn 的连接,此部分代码省略
let resp = await conn.uql("show().algo()");
printers.DataItem.table(resp.data?.alias("_algoList"));
console.log("name of 3rd algo is: " + resp.data?.alias("_algoList").asAlgos()[2].name);
console.log("version of 3rd algo is: " + resp.data?.alias("_algoList").asAlgos()[2].param.version);
};
sdkUsage();
输出:
Alias: _algoList Type: RESULT_TYPE_TABLE
┌─────────┬────────────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────┬────────────────────────────────────────────┐
│ (index) │ name │ param │ detail │
├─────────┼────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┤
│ 0 │ 'celf' │ '{"name":"celf","description":"celf","version":"1.0.0","parameters":...}' │ 'base:\r\n category: Centrality\r\n ...' │
│ 1 │ 'harmonic_centrality' │ '{"name":"harmonic_centrality","description":"harmonic_centrality","version":"1.0.0","parameters":...}' │ 'base:\r\n category: Centrality\r\n ...' │
│ 2 │ 'closeness_centrality' │ '{"name":"closeness centrality","description":"closeness centrality","version":"1.0.1","parameters":...}' │ 'base:\r\n category: Centrality\r\n ...' │
│ 3 │ 'degree' │ '{"name":"degree centrality","description":"degree centrality","version":"1.0.3","parameters":...}' │ 'base:\r\n category: Centrality\r\n ...' │
│ 4 │ 'khop_all' │ '{"name":"khop_all","description":"khop_all","version":"1.0.2","parameters":...}' │ 'base:\r\n category: General\r\n ...' │
└─────────┴────────────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────┴────────────────────────────────────────────┘
name of 3rd algo is: closeness_centrality
version of 3rd algo is: 1.0.1
Table
Table 类的字段:
字段 | 类型 | 说明 |
---|---|---|
name | string | Table 的别名 |
alias | string | Table 的别名 |
headers | string[] | Table 的表头 |
rows | any[][] | Table 的所有行 |
Table 类的方法:
方法 | 类型 | 说明 |
---|---|---|
getHeaders() | string[] | 获取当前 Table 的表头 |
getRows() | any[][] | 获取当前 Table 的所有行 |
toKV() | any[] | 将当前 Table 转换成 KV 列表 |
示例:发送 UQL 语句查询一个表格,获取该表格的表头及第二行数据
import { ConnectionPool, printers } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
// 创建名为 conn 的连接,此部分代码省略
let resp = await conn.uql("find().nodes({@account}) limit 5 return table(nodes.gender, nodes.year) as myTable");
printers.DataItem.table(resp.data?.alias("myTable"));
console.log("headers are: " + resp.data?.alias("myTable").asTable().getHeaders());
console.log("2nd row is: " + resp.data?.alias("myTable").asTable().getRows()[1]);
};
sdkUsage();
输出:
Alias: myTable Type: RESULT_TYPE_TABLE
┌─────────┬──────────────┬────────────┐
│ (index) │ nodes.gender │ nodes.year │
├─────────┼──────────────┼────────────┤
│ 0 │ 'female' │ 1978 │
│ 1 │ 'female' │ 1989 │
│ 2 │ 'male' │ 1982 │
│ 3 │ 'female' │ 2007 │
│ 4 │ 'male' │ 1973 │
└─────────┴──────────────┴────────────┘
headers are: nodes.gender,nodes.year
2nd row is: female,1989
any[][] (ARRAY)
示例:发送 UQL 语句查询一列数组,获取第二个数组的第三个元素
import { ConnectionPool, printers } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
// 创建名为 conn 的连接,此部分代码省略
let resp = await conn.uql("find().nodes({@account}) as nodes group by nodes.industry return collect(nodes.name) as myArray limit 3");
printers.DataItem.array(resp.data?.alias("myArray"));
console.log("3rd element of 2nd array is: " + resp.data?.alias("myArray").asArray()[1][2]);
};
sdkUsage();
输出:
Alias: myArray Type: RESULT_TYPE_ARRAY
[ 'jibber-jabber', 'jo', 'CR.', 'laofung', 'pepe' ]
[ 'Meng', 'HolyC', 'pixiedust', 'Blair_self', 'sadmov' ]
[ 'Unbeliever', 'Ivo', 'Nannobrycon', 'auric', 'Blair_self' ]
3rd element of 2nd array is: pixiedust
AttrAlias
AttrAlias 的字段:
字段 | 类型 | 说明 |
---|---|---|
alias | string | AttrAlias 的别名 |
values | any[] | AttrAlias 的所有行 |
type | PropertyType | AttrAlias 的类型代号 |
type_desc | string | AttrAlias 的类型名称 |
示例:发送 UQL 语句查询一列属性,获取第二个属性
import { ConnectionPool, printers } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
// 创建名为 conn 的连接,此部分代码省略
let resp = await conn.uql("find().nodes({@account}) as nodes return nodes.industry as myAttr limit 5");
printers.DataItem.array(resp.data?.alias("myAttr"));
console.log("2nd attribute is: " + resp.data?.alias("myAttr").asAttrs().values[1]);
};
sdkUsage();
输出:
Alias: myAttr Type: RESULT_TYPE_ATTR
{
alias: 'myAttr',
type: 7,
type_desc: 'string',
values: [ 'Manufacturing', 'Health', 'Other', 'Education', 'Publishing' ]
}
2nd attribute is: Health