GIE 的 Gremlin 支持

本文档将为您提供逐步指导,说明如何将您的Gremlin应用程序连接到GIE前端服务,该服务提供与官方Tinkerpop服务类似的功能。

您的第一步是获取GIE前端服务的端点:

  • 按照说明在K8s集群中部署GIE

  • 按照说明在本地机器上启动GIE。

通过Python SDK连接

GIE 可以轻松通过 Tinkerpop 的 Gremlin-Python 连接到已加载的图数据。

首先安装依赖项:

pip3 install gremlinpython

然后连接到服务并运行查询:

import sys
from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

graph = Graph()
gremlin_endpoint = # the GIE Frontend service endpoint you've obtained
remoteConn = DriverRemoteConnection('ws://' + gremlin_endpoint + '/gremlin','g')
g = graph.traversal().withRemote(remoteConn)

res = g.V().count().next()
assert res == 6

提示

更简单的选择是使用interactive对象通过GraphScope的python SDK提交Gremlin查询,这是一个封装了Tinkerpop的Gremlin-Python的包装器,会自动获取端点。

在大规模数据处理场景中,通常需要对返回数据进行流式处理以避免因处理海量数据导致的内存溢出(OOM)问题,这种方式能带来内存高效利用、持续处理、增量分析、降低延迟、可扩展性和资源优化等优势。它使您能够高效处理分析海量数据,同时降低内存相关风险。以下示例将指导您如何通过Python SDK以流式方式收集结果。

from queue import Queue
from gremlin_python.driver.client import Client

graph_url = # the GIE Frontend service endpoint you've obtained
client = Client(graph_url, "g")

ret = []
q = client.submit('g.V()')
while True:
try:
   ret.extend(q.next())
except StopIteration:
   break

print(ret)

此外,以下是一些可用于在服务器端配置流式传输大小的参数。

# interactive_engine/compiler/src/main/resources/conf/gremlin-server.yaml
...
# total num of streaming batch size returned by compiler service
resultIterationBatchSize: 64
...

通过Java SDK连接

关于在Java语言中使用Gremlin,请参阅Gremlin-Java

以下是一个示例,指导您如何通过Java SDK以流式方式收集结果。

Cluster cluster = Cluster.build()
         .addContactPoint("localhost") // use your host ip
         .port(8182) // use your port
         .create();
Client client = cluster.connect();
ResultSet resultSet = client.submit("g.V()"); // use your query
Iterator<Result> results = resultSet.iterator();
while(results.hasNext()) {
   display(results.next()); // display each result in your way
}
client.close();
cluster.close();

通过Gremlin控制台连接

  1. 下载Gremlin控制台并解压到本地目录。

    # 如果找不到指定版本(3.6.4),请尝试访问https://dlcdn.apache.org
    # 下载可用版本
    curl -LO https://dlcdn.apache.org/tinkerpop/3.6.4/apache-tinkerpop-gremlin-console-3.6.4-bin.zip && \
    unzip apache-tinkerpop-gremlin-console-3.6.4-bin.zip && \
    cd apache-tinkerpop-gremlin-console-3.6.4
    
  2. 在Gremlin控制台目录中,将conf/remote.yaml文件中的hostsport修改为GIE前端服务端点,如下所示:

hosts: [your_endpoint_address]
port: [your_endpoint_port]
serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { serializeResultToString: true }}
  1. 打开Gremlin控制台

    chmod +x bin/gremlin.sh
    bin/gremlin.sh
    
  2. gremlin> 提示符下,输入以下命令以连接到 GraphScope 会话并切换到远程模式,这样所有后续的 Gremlin 查询将自动发送到远程连接。

    gremlin> :remote connect tinkerpop.server conf/remote.yaml
    gremlin> :remote console
    gremlin> g.V().count()
    ==> 6
    gremlin>
    
  3. 您现在可以通过Python SDK或Gremlin控制台提交任何Gremlin查询。

  4. 完成后,输入以下命令退出Gremlin控制台。

gremlin> :exit