本节为您介绍如何在GQL语句中使用Connection
对象的gql()
方法和gqlStream()
方法查询数据库。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
GQL(图查询语言)是符合ISO标准的图数据库查询语言。更多GQL的详细信息,请参阅本文档。
gql()
在当前图集或数据库上执行GQL查询语句并返回查询结果。
参数:
str
:待执行的GQL查询语句。RequestConfig
(可选):配置请求。
返回值:
UltipaResponse
:请求的结果。
# 获取图集miniCircle中movie下的5个点并打印其名称
response = Conn.gql("MATCH (n:movie) 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类型映射。
gqlStream()
在当前图集或数据库上执行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)
gql = 'MATCH p = ()-[]-() RETURN p'
result = Conn.gqlStream(gql, stream, requestConfig)
print()
Stream started.
Data received: {'status': <ultipa.types.types.Status object at 0x00000239B87E9FD0>, 'items': {'p': <ultipa.types.types.DataItem object at 0x00000239B87F1790>}, 'aliases': None, 'req': None, 'statistics': <ultipa.types.types.UltipaStatistics object at 0x00000239B87F1760>, '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.gql("MATCH (n) RETURN n LIMIT 10", requestConfig)
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"))