uql()
方法及相关类:
def uql(self,
uql: str,
requestConfig: ULTIPA_REQUEST.RequestConfig = ULTIPA_REQUEST.RequestConfig()
) -> ULTIPA_RESPONSE.UltipaResponse
使用 uql()
进行请求时会返回结果类 UltipaResponse。
UltipaResponse 的字段:
字段 | 类型 | 说明 |
---|---|---|
aliases | List[ResultAlias] | return 返回的数据的别名和类型 |
items | Dict | return 返回的数据,每个别名对应的 DataItem 类 |
explainPlan | ExplainPlan | UQL 语句解析树 |
req | ReturnReq | UQL 请求的信息 |
status | Status | Ultipa Server 返回的执行状态 |
statistics | UltipaStatistics | 执行的统计信息,包含执行时间、影响点边数等 |
UltipaResponse 的方法:
方法 | 类型 | 说明 |
---|---|---|
Print() | 将查询结果以表格的形式输出 | |
toJson(pretty: bool) | str | 将查询结果转为 json 格式的字符串,可指定是否进行结构化处理(换行及缩进,默认不进行处理) |
toDict() | Dict | 将查询结果转为 dict 类型 |
get(index: int) | DataItem | 按下标获取查询结果中的某一个返回值(DataItem 类) |
alias(alias: str) | DataItem | 按别名获取查询结果中的某一个返回值(DataItem 类) |
返回值的别名类型有 NODE、EDGE、PATH 等,使用 DataItem 类的相应的方法可以将这些类型转为相应的类,详见下一章《返回值的结构化》。
Print()
示例:发送语句 show().grpah()
,将结果以表格的形式输出
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("show().graph()")
req.Print()
输出:
+----------------+
| STATUS |
+------+---------+
| Code | Message |
+------+---------+
| 0 | |
+------+---------+
+------------------------------------------------------+
| Statistics |
+--------------+--------------+------------+-----------+
| edgeAffected | nodeAffected | engineCost | totalCost |
+--------------+--------------+------------+-----------+
| 0 | 0 | 0 | 0 |
+--------------+--------------+------------+-----------+
+--------------------------------------------------------------------------------------------+
| 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 |
+------+---------------+------------+------------+-------------------------------+-----------+
toJson()
示例:发送语句 find().nodes(1) as nodes return nodes
,将结果转为 json 后进行结构化输出
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("find().nodes(1) as nodes return nodes")
print(req.toJSON(True))
输出:
{
"aliases": [
{
"alias": "nodes",
"result_type": "NODE"
}
],
"explainPlan": [],
"items": {
"nodes": {
"alias": "nodes",
"data": [
{
"id": "",
"schema": "account",
"uuid": 1,
"values": {}
}
],
"type": "NODE"
}
},
"req": null,
"statistics": {
"edgeAffected": 0,
"engineCost": 0,
"nodeAffected": 0,
"totalCost": 0
},
"status": {
"clusterInfo": {
"leader": null,
"raftPeers": [],
"redirect": ""
},
"code": 0,
"message": ""
}
}
toDict()
示例:发送语句 find().nodes(1) as nodes return nodes
,将结果转为 dict 后进行输出
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("find().nodes(1) as nodes return nodes")
print(req.toDict())
输出:
{'aliases': [], 'explainPlan': [], 'items': {}, 'req': None, 'statistics': {'edgeAffected': 0, 'engineCost': 0, 'nodeAffected': 0, 'totalCost': 0}, 'status': {'clusterInfo': {'leader': None, 'raftPeers': [], 'redirect': ''}, 'code': 0, 'message': ''}}
get()
示例:发送语句 find().nodes({@account}) as nodes return nodes.name, nodes.gender limit 5
,将第一个返回值转为 json 类型后进行结构化输出
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("find().nodes({@customer}) as nodes return nodes.nickname, nodes.gender limit 5")
print(req.get(0).toJSON(True))
输出:
{
"alias": "nodes.nickname",
"data": {
"name": "nodes.nickname",
"rows": [
"jo",
"jibber-jabber",
"HolyC",
"pixiedust",
"tommy"
],
"type": "string"
},
"type": "ATTR"
}
alias()
示例:发送语句 find().nodes({@account}) as nodes return nodes.name, nodes.gender limit 5
,将第二个返回值转为 json 类型后进行结构化输出
from ultipa import Connection, UltipaConfig
# 创建名为 conn 的连接并使用 default 图集,此部分代码省略
req = conn.uql("find().nodes({@customer}) as nodes return nodes.nickname, nodes.gender limit 5")
print(req.alias("nodes.gender").toJSON(True))
输出:
{
"alias": "nodes.gender",
"data": {
"name": "nodes.gender",
"rows": [
"female",
"female",
"male",
"female",
"male"
],
"type": "string"
},
"type": "ATTR"
}