从 Python 连接
Gremlin 遍历可以使用 Gremlin-Python 构建,就像在 Gremlin-Java 或 Gremlin-Groovy 中一样。有关 Gremlin 的介绍和进一步资源的链接,请参阅 Gremlin 查询语言。
重要
一些 Gremlin 步骤和谓词名称在 Python 中是保留字。这些名称在 Gremlin-Python 中只需在后面加上 _,例如,in() 变为 in_(),not() 变为 not_(),依此类推。受此影响的其他名称有:all、and、as、from、global、is、list、or 和 set。
JanusGraph 和 Gremlin-Python 入门
Gremlin-Python 入门
- 安装 Gremlin-Python
pip install gremlinpython==3.7.3 -
创建一个文本文件
gremlinexample.py并添加以下导入from gremlin_python import statics from gremlin_python.structure.graph import Graph from gremlin_python.process.graph_traversal import __ from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection -
创建
GraphTraversalSource,它是所有 Gremlin 遍历的基础from gremlin_python.process.anonymous_traversal import traversal connection = DriverRemoteConnection('ws://:8182/gremlin', 'g') # The connection should be closed on shut down to close open connections with connection.close() g = traversal().withRemote(connection) # Reuse 'g' across the application -
执行一个简单的遍历
hercules_age = g.V().has('name', 'hercules').values('age').next() print(f'Hercules is {hercules_age} years old.')next()是一个终止步骤,它将遍历提交到 Gremlin Server 并返回单个结果。
JanusGraph 特有的类型和谓词
JanusGraph 包含一些不属于 Apache TinkerPop 的类型和 谓词,因此 Gremlin-Python 也不支持它们。