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查询
基础教程推荐
猜你喜欢
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
