How to execute a Gremlin query in Python(如何在Python中执行Gremlin查询)
本文介绍了如何在Python中执行Gremlin查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中,我正在尝试使用Gremlin连接并执行查询。 在这里,我可以使用gremlin连接到海王星数据库,只使用Print语句获取顶点计数:Print(G.V().has(";Sys.tenantID&Quot;,";hLWmgcH61m0bnaI9KpUj6z";).count().next())
但读取文件、存储在变量中并传入gremlin查询不起作用
代码
from __future__ import print_function # Python 2/3 compatibility
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()
remoteConn = DriverRemoteConnection('wss://<URL>:<port>/gremlin','g')
g = graph.traversal().withRemote(remoteConn)
with open ('Orgid1.txt','r') as file:
# orgstr = file.readlines()
# orgstr = [orgstr.rstrip() for line in orgstr]
for line in file:
print(g.V().has("system.tenantId", line.rstrip()).count().next())
# print(neptune)
#print(g.V().count())
#print(g.V().has("system.tenantId", "xyz123").count().next())
remoteConn.close()
添加更多详细信息:
这是我使用的代码:
输入:
with open ('Orgid1.txt','r') as file:
for line in file:
print(g.V().has("system.tenantId", line.rstrip()).out().count().next())
输出:
$ python3 gremlinexample.py
0
如果我直接打印它,它是有效的。 输入:
print(line.rstrip())
输出:
$ python3 gremlinexample.py
0
xyz123
ps:在名为Orgid1.txt的输入文件中存在xyz123
推荐答案
您需要验证来自文件的文本是否与所需的属性键值完全匹配。我创建了一个简单文件,如下所示:
$ cat values.txt
AUS
LHR
SEA
并将代码的基本结构与航线数据一起使用:
g = graph.traversal().withRemote(connection)
with open ('values.txt','r') as file:
for line in file:
print(g.V().has('code', line.rstrip()).out().count().next())
并且运行正常
93
221
122
这篇关于如何在Python中执行Gremlin查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何在Python中执行Gremlin查询


基础教程推荐
猜你喜欢
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 包装空间模型 2022-01-01