本节为您介绍如何在UQL语句中使用Connection
对象的uql()
方法和uqlStream()
方法查询数据库。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
UQL是全面操作嬴图数据库的专用语言。更多嬴图GQL的详细信息,请参阅本文档。
uql()
在当前图集或数据库上执行GQL查询语句并返回查询结果。
参数:
str
:待执行的UQL查询语句。RequestConfig
(可选):配置请求。
返回值:
UltipaResponse
:请求的结果。
# 获取图集miniCircle中@movie下的5个点并打印其名称
response = Conn.uql("find().nodes({@movie}) as n return n{*} limit 5", requestConfig)
nodeList = response.alias("n").asNodes()
for node in nodeList:
print(node.get("name"))
The Shawshank Redemption
Farewell My Concubine
Léon: The Professional
Titanic
Life is Beautiful
更多示例,请参阅嬴图数据与Python类型映射.
uqlStream()
在当前图集或数据库上执行GQL查询语句并以增量形式返回查询结果。在处理大型数据集时,无需立刻加载所有数据至内存中。
Parameters:
str
:待执行的UQL查询语句。UQLResponseStream
:流处理监听器。RequestConfig
(可选):配置请求。
返回值:
None
# 获取图集miniCircle的1步路径
requestConfig = RequestConfig(graphName="miniCircle")
# 定义事件处理函数
def on_start(requestConfig):
print("Stream started.")
def on_data(res, requestConfig):
print("Data received:", res)
def on_end(requestConfig):
print("Stream ended.")
stream = UQLResponseStream()
stream.on("start", on_start)
stream.on("data", on_data)
stream.on("end", on_end)
uql = 'n().e().n() as paths return paths{*}'
result = Conn.uqlStream(uql, stream, requestConfig)
print()
Stream started.
Data received: {'status': <ultipa.types.types.Status object at 0x000001E01762FCA0>, 'items': {'paths': <ultipa.types.types.DataItem object at 0x000001E017635730>}, 'aliases': None, 'req': None, 'statistics': <ultipa.types.types.UltipaStatistics object at 0x000001E017635850>, 'explainPlan': None}
Data received: {'status': <ultipa.types.types.Status object at 0x000001E01762F2B0>, 'items': {'paths': <ultipa.types.types.DataItem object at 0x000001E0189C4DF0>}, 'aliases': None, 'req': None, 'statistics': <ultipa.types.types.UltipaStatistics object at 0x000001E0189C4D60>, 'explainPlan': None}
Stream ended.
完整示例
from ultipa import Connection, UltipaConfig
from ultipa.configuration.RequestConfig import RequestConfig
ultipaConfig = UltipaConfig()
# URI示例: ultipaConfig.hosts = ["mqj4zouys.us-east-1.cloud.ultipa.com:60010"]
ultipaConfig.hosts = ["192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"]
ultipaConfig.username = "<username>"
ultipaConfig.password = "<password>"
Conn = Connection.NewConnection(defaultConfig=ultipaConfig)
# 配置请求
requestConfig = RequestConfig(graphName="amz")
# 获取10个点并打印返回的第一个点的_id和storeName属性值
response = Conn.uql("find().nodes() as n return n{*} limit 10")
nodeList = response.alias("n").asNodes()
print("ID of the 1st node:", nodeList[0].getID())
print("Store name of the 1st node:", nodeList[0].get("storeName"))