DataItem
使用 UltipaResponse 的 get()
、alias()
方法时将获得 DataItem 类。
DataItem 具有以下方法:
方法 | 类型 | 含义 |
---|---|---|
asNodes() | List[Node] | 将 NODE 类型的 DataItem 转为 List[Node] |
asFirstNodes() | Node | 取出 NODE 类型的 DataItem 中的第一个 Node,相当于 asNodes()[0] |
asEdges()] | List[Edge] | 将 EDGE 类型的 DataItem 转为 List[Edge] |
asFirstEdges() | Edge | 取出 EDGE 类型的 DataItem 中的第一个 Edge,相当于 asEdges()[0] |
asPaths() | List[Path] | 将 PATH 类型的 DataItem 转为 List[Path] |
asGraphs() | List[UQLGraph] | 将默认别名 _graph 的 DataItem 转为 List[UQLGraph] |
asSchemas() | List[UQLSchema] | 将默认别名 _nodeSchema 或 _edgeSchema 的 DataItem 转为 List[UQLSchema] |
asProperties() | List[UQLProperty] | 将默认别名 _nodeProperty 或 _edgeProperty 的 DataItem 转为 List[UQLProperty] |
asAlgos() | List[UQLAlgos] | 将默认别名 _algoList 的 DataItem 转为 List[UQLAlgos] |
asTable() | Table | 将 TABLE 类型的 DataItem 转为 Table |
asArray() | ArrayAlias | 将 ARRAY 类型的 DataItem 转为 ArrayAlias |
asAttr() | AttrAlias | 将 ATTR 类型的 DataItem 转为 AttrAlias |
也可以使用方法
asTable()
将默认别名 _graph、_nodeSchema、_edgeSchema 等的 DataItem 转为 Table。
Node
Node 类的字段:
字段 | 类型 | 说明 |
---|---|---|
id | str | Node 的 ID |
uuid | int | Node 的 UUID |
schema | str | Node 的 Schema |
values | Dict | Node 的自定义属性 |
Node 类的方法:
方法 | 类型 | 说明 |
---|---|---|
getID() | str | 获取当前 Node 的 ID |
getUUID() | int | 获取当前 Node 的 UUID |
getSchema() | str | 获取当前 Node 的 Schema |
getValues() | Dict | 获取当前 Node 的 Values(自定义属性) |
get(propertyName: str) | 获取当前 Node 的某个自定义属性 | |
set(propertyName: str, value) | 设置当前 Node 的某个自定义属性,属性名不存在时将则添加该键值对 |
可通过
ULTIPA.Node(values: Dict, schema: str = None, id: str = None, uuid: int = None)
方法来构造 Node 对象
示例:发送 UQL 语句查询一列点,获取第二个点的 ID,将第一个点的 rating 改为 8 后再将该点结构化输出
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("find().nodes({@movie}) as nodes return nodes{*} limit 5")
req.Print()
print("ID of 2nd node is:", req.alias("nodes").asNodes()[1].getID())
req.alias("nodes").asFirstNodes().set("rating",8)
print("1st node with new rating value:")
print(req.alias("nodes").asFirstNodes().toJSON(True))
部分输出:
+--------------------------------------------------------+
| Alias: nodes AliasType: NODE Schema: movie |
+------------------------+------+--------+--------+------+
| id | uuid | schema | rating | year |
+------------------------+------+--------+--------+------+
| ULTIPA80000000000003E9 | 1001 | movie | 9 | 1994 |
| ULTIPA80000000000003EA | 1002 | movie | 7 | 1993 |
| ULTIPA80000000000003EB | 1003 | movie | 6 | 1998 |
| ULTIPA80000000000003EC | 1004 | movie | 9 | 1994 |
| ULTIPA80000000000003ED | 1005 | movie | 9 | 1997 |
+------------------------+------+--------+--------+------+
ID of 2nd node is: ULTIPA80000000000003EA
1st node with new rating value:
{
"id": "ULTIPA80000000000003E9",
"schema": "movie",
"uuid": 1001,
"values": {
"rating": 8,
"year": 1994
}
}
Edge
Edge 类的字段:
字段 | 类型 | 说明 |
---|---|---|
uuid | int | Edge 的 UUID |
from_uuid | int | Edge 起点的 UUID |
to_uuid | int | Edge 终点的 UUID |
from | str | Edge 起点的 ID |
to | str | Edge 终点的 ID |
schema | str | Edge 的 Schema |
values | Dict | Edge 的自定义属性 |
Edge 类的方法:
方法 | 类型 | 说明 |
---|---|---|
getUUID() | int | 获取当前 Edge 的 UUID |
getSchema() | str | 获取当前 Edge 的 Schema |
getFrom() | str | 获取当前 Edge 的起点的 ID |
getFromUUID() | int | 获取当前 Edge 的起点的 UUID |
getTo() | str | 获取当前 Edge 的终点的 ID |
getToUUID() | int | 获取当前 Edge 的终点的 UUID |
getValues() | Dict | 获取当前 Edge 的 Values(自定义属性) |
get(propertyName: str) | 获取当前 Edge 的某个自定义属性 | |
set(propertyName: str, value) | 设置当前 Edge 的某个自定义属性,属性名不存在时将则添加该键值对 |
可通过
ULTIPA.Edge(values: Dict, from_id: str = None, from_uuid: int = None, to_id: str = None, to_uuid: int = None, schema: str = None, uuid: int = None)
方法来构造 Edge 对象
示例:发送 UQL 语句查询一列边,获取第二个边的起点的 ID,为第一个边添加属性 time 并设置为 2022-04-13 09:23:24 后再将该边结构化输出
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("find().edges({@review}) as edges return edges{*} limit 5")
req.Print()
print("ID of start node of 2nd edge is:", req.alias("edges").asEdges()[1].getFrom())
req.alias("edges").asFirstEdges().set("time","2022-04-13 09:23:24")
print("1st edge with new property:")
print(req.alias("edges").asFirstEdges().toJSON(True))
部分输出:
+-----------------------------------------------------------------------------------------------+
| Alias: edges AliasType: EDGE Schema: review |
+------+-----------+---------+------------------------+------------------------+--------+-------+
| uuid | from_uuid | to_uuid | from_id | to_id | schema | value |
+------+-----------+---------+------------------------+------------------------+--------+-------+
| 4776 | 1001 | 10001 | ULTIPA80000000000003E9 | ULTIPA8000000000002711 | review | 9 |
| 4777 | 1002 | 10001 | ULTIPA80000000000003EA | ULTIPA8000000000002711 | review | 8 |
| 4778 | 1003 | 10001 | ULTIPA80000000000003EB | ULTIPA8000000000002711 | review | 8 |
| 4779 | 1004 | 10001 | ULTIPA80000000000003EC | ULTIPA8000000000002711 | review | 9 |
| 4780 | 1005 | 10001 | ULTIPA80000000000003ED | ULTIPA8000000000002711 | review | 7 |
+------+-----------+---------+------------------------+------------------------+--------+-------+
ID of start node of 2nd edge is: ULTIPA80000000000003EA
1st edge with new property:
{
"from_id": "ULTIPA80000000000003E9",
"from_uuid": 1001,
"schema": "review",
"to_id": "ULTIPA8000000000002711",
"to_uuid": 10001,
"uuid": 4776,
"values": {
"time": "2022-04-13 09:23:24",
"value": 9
}
}
Path
Path 类的字段:
字段 | 类型 | 说明 |
---|---|---|
nodes | List[Node] | Path 中的 Node 列表 |
edges | List[Edge] | Path 中的 Edge 列表 |
nodeSchemas | Dict[str, Schema] | Path 中的点 Schema 的字典 |
edgeSchemas | Dict[str, Schema] | Path 中的边 Schema 的字典 |
Path 类的方法:
方法 | 类型 | 说明 |
---|---|---|
length() | int | 获取当前 Path 的长度,即 Edge 的数量 |
getNodes() | List[Node] | 获取当前 Path 的 Node 列表 |
getEdges() | List[Edge] | 获取当前 Path 的 Edge 列表 |
可通过
ULTIPA.Path(nodes: List[Node], edges: List[Edge], nodeSchemas, edgeSchemas)
方法来构造 Path 对象
示例:发送 UQL 语句查询一列路径,获取第一个路径的第二个点,并进行结构化输出
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("n().e()[2].n() as paths return paths{*} limit 3")
req.Print()
print("the 2nd node in the 1st path:")
print(req.alias("paths").asPaths()[0].getNodes()[1].toJSON(True))
部分输出:
+-------------------------------------------+
| Alias: paths AliasType: PATH |
+-------------------------------------------+
| data |
+-------------------------------------------+
| (1001) <- [1475] -(20) - [1350] -> (1019) |
| (1001) <- [1363] -(24) - [1353] -> (1045) |
| (1001) <- [1645] -(22) - [1374] -> (1042) |
+-------------------------------------------+
the 2nd node in the 1st path:
{
"id": "ULTIPA8000000000000014",
"schema": "account",
"uuid": 20,
"values": {
"gender": "female",
"industry": "Construction",
"name": "Meng",
"year": 1982
}
}
UQLGraph
UQLGraph 类的字段:
字段 | 类型 | 说明 |
---|---|---|
id | str | Graph 的 ID |
name | str | Graph 的名称 |
description | str | Graph 的描述 |
totalNodes | str | Graph 的点数量 |
totalEdges | str | Graph 的边数量 |
status | str | Graph 的状态(MOUNTED、UNMOUNTED) |
示例:发送 UQL 语句查询图集列表,获取第三个图集的名称
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("show().graph()")
req.Print()
print("name of 3rd graph is:", req.alias("_graph").asGraphs()[2].name)
部分输出:
+--------------------------------------------------------------------------------------------+
| Alias: _graph AliasType: TABLE |
+------+---------------+------------+------------+-------------------------------+-----------+
| id | name | totalNodes | totalEdges | description | status |
+------+---------------+------------+------------+-------------------------------+-----------+
| 1046 | amz | 403393 | 3387374 | | MOUNTED |
| 0 | default | 111 | 274 | System default graph! | MOUNTED |
| 1332 | miniCircle | 303 | 1961 | | MOUNTED |
| 1024 | newTest | 14 | 7 | rename test as newTest | MOUNTED |
+------+---------------+------------+------------+-------------------------------+-----------+
name of 3rd graph is: miniCircle
UQLSchema
UQLSchema 类的字段:
字段 | 类型 | 说明 |
---|---|---|
name | str | Schema 的名称 |
description | str | Schema 的描述 |
properties | List[SchemaProperty] | Schema 的所有 Property |
total | int | Schema 的点/边数量 |
type | str | Schema 的点/边类型 |
示例:发送 UQL 语句查询当前图集的点 schema 列表,获取第三个点 schema 的点的数量
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("show().node_schema()")
req.Print()
print("number of nodes of 3rd node schema is:", req.alias("_nodeSchema").asSchemas()[2].total)
部分输出:
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Alias: _nodeSchema AliasType: TABLE |
+----------+----------------+-----------------------------------------------------------------------------------------------------------------------------------+------------+
| name | description | properties | totalNodes |
+----------+----------------+-----------------------------------------------------------------------------------------------------------------------------------+------------+
| default | default schema | [{"name":"name","type":"string","description":"","lte":"false"},{"name":"age","type":"int32","description":"","lte":"false"}] | 10 |
| customer | bank customer | [{"name":"name","type":"string","description":"","lte":"false"},{"name":"level","type":"int32","description":"","lte":"false"}] | 3 |
| card | bank card | [{"name":"level","type":"int32","description":"","lte":"false"},{"name":"balance","type":"float","description":"","lte":"false"}] | 1 |
+----------+----------------+-----------------------------------------------------------------------------------------------------------------------------------+------------+
number of nodes of 3rd node schema is: 1
UQLProperty
UQLProperty 类的字段:
字段 | 类型 | 说明 |
---|---|---|
name | str | Property 的名称 |
description | str | Property 的描述 |
schema | str | Property 的 Schema |
type | str | Property 的数据类型 |
lte | bool | Property 的 LTE 状态 |
示例:发送 UQL 语句查询当前图集的点属性列表,获取第三个点属性的 LTE 信息
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("show().node_property()")
req.Print()
print("lte of 3rd node property is:", req.alias("_nodeProperty").asProperties()[2].lte)
部分输出:
+------------------------------------------------------+
| Alias: _nodeProperty AliasType: TABLE |
+---------+--------+-------+----------+----------------+
| name | type | lte | schema | description |
+---------+--------+-------+----------+----------------+
| name | string | false | default | |
| age | int32 | false | default | |
| name | string | false | customer | customer name |
| level | int32 | true | customer | customer level |
| level | int32 | false | card | card level |
| balance | float | true | card | card balance |
+---------+--------+-------+----------+----------------+
lte of 3rd node property is: False
UQLAlgos
UQLAlgos 类的字段:
字段 | 类型 | 说明 |
---|---|---|
name | str | Algo 的名称 |
description | str | Algo 的描述 |
version | str | Algo 的版本 |
result_opt | str | Algo 的结果选项 |
parameters | Dict | Algo 的参数,包括算法名、描述、参数列表、版本号等信息 |
write_to_stats_parameters | Dict | Algo 的写状态的参数 |
write_to_db_parameters | Dict | Algo 的写库的参数 |
write_to_file_parameters | Dict | Algo 的写文件的参数 |
示例:发送 UQL 语句查询已安装的算法列表,获取第三个算法的名称及版本
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("show().algo()")
req.Print()
print("name of 3rd algorithm is:", req.alias("_algoList").asAlgos()[2].name)
print("version of 3rd algorithm is:", req.alias("_algoList").asAlgos()[2].version)
部分输出:
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Alias: _algoList AliasType: TABLE |
+------------------------+-----------------------------------+---------+-------------------------+---------------------------+-------------------------+--------------------------+------------+
| name | description | version | parameters | write_to_stats_parameters | write_to_db_parameters | write_to_file_parameters | result_opt |
+------------------------+-----------------------------------+---------+-------------------------+---------------------------+-------------------------+--------------------------+------------+
| Harmonic Centrality | harmonic centrality | 1.0.0 | {'ids': ...} | None | None | None | 27 |
| Page Rank | page rank | 1.0.1 | {'init_value': ...} | None | {'property': ...} | {'filename': ...} | 27 |
| Betweenness Centrality | betweenness centrality, normalize | 1.0.1 | {'sample_size': ...} | None | {'property': ...} | {'filename': ...} | 27 |
+------------------------+-----------------------------------+---------+-------------------------+---------------------------+-------------------------+--------------------------+------------+
name of 3rd algorithm is: Betweenness Centrality
version of 3rd algorithm is: 1.0.1
Table
Table 类的字段:
字段 | 类型 | 说明 |
---|---|---|
name | str | Table 的别名 |
headers | List[Dict] | Table 的表头 |
rows | List[List] | Table 的所有行 |
Table 类的方法:
方法 | 类型 | 说明 |
---|---|---|
getName() | str | 获取当前 Table 的别名 |
getHeaders() | List[Dict] | 获取当前 Table 的表头列表 |
getRows() | List[List] | 获取当前 Table 的所有行的列表 |
可通过
ULTIPA.Table(table_name: str, headers: List[dict], table_rows: List[List])
方法来构造 Table 对象
示例:发送 UQL 语句查询一个表格,获取该表格的整个表头以及第二行数据
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("find().nodes({@account}) limit 5 return table(nodes.gender, nodes.year) as myTable")
req.Print()
print("header is:", req.alias("myTable").asTable().getHeaders())
print("2nd row is:", req.alias("myTable").asTable().getRows()[1])
部分输出:
+---------------------------------+
| Alias: myTable AliasType: TABLE |
+-----------------+---------------+
| nodes.gender | nodes.year |
+-----------------+---------------+
| female | 1978 |
| female | 1989 |
| male | 1982 |
| female | 2007 |
| male | 1973 |
+-----------------+---------------+
header is: [{'property_name': 'nodes.gender', 'property_type': 'string'}, {'property_name': 'nodes.year', 'property_type': 'int32'}]
2nd row is: ['female', 1989]
ArrayAlias
ArrayAlias 类的字段:
字段 | 类型 | 说明 |
---|---|---|
alias | str | ArrayAlias 的别名 |
elements | List[List] | ArrayAlias 的所有行 |
可通过
ULTIPA.ArrayAlias(alias: str, elements)
方法来构造 ArrayAlias 对象
示例:发送 UQL 语句查询一列数组,获取第二个数组的第三个元素
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("find().nodes({@account}) as nodes group by nodes.industry return collect(nodes.name) as myArray limit 3")
req.Print()
print("3rd element of 2nd array is:", req.alias("myArray").asArray().elements[1][2])
部分输出:
+-------------------------------------------------------------+
| Alias: myArray AliasType: ARRAY |
+-------------------------------------------------------------+
| collectNames |
+-------------------------------------------------------------+
| ["jibber-jabber", "jo", "CR.", "laofung", "pepe"] |
| ["Meng", "HolyC", "pixiedust", "sadmov"] |
| ["Unbeliever", "Ivo", "Nannobrycon", "auric", "Blair_self"] |
+-------------------------------------------------------------+
3rd element of 2nd array is: pixiedust
AttrAlias
AttrAlias 的字段:
字段 | 类型 | 说明 |
---|---|---|
name | str | AttrAlias 的别名 |
rows | List[str] | AttrAlias 的所有航 |
type | str | AttrAlias 的类型 |
可通过
ULTIPA.AttrAlias(alias: str, values: List[str], type: str = None)
方法来构造 AttrAlias 对象
示例:发送 UQL 语句查询一列属性,获取第二个属性
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("find().nodes({@account}) as nodes return nodes.industry as myAttr limit 5")
req.Print()
print("2nd attribute is:", req.alias("myAttr").asAttr().rows[1])
部分输出:
+----------------------------+
| Alias: myAttr AliasType: ATTR |
+----------------------------+
| myAttr |
+----------------------------+
| Manufacturing |
| Health |
| Other |
| Education |
| Publishing |
+----------------------------+
2nd attribute is: Health