简体   繁体   English

Gremlin Python返回空图

[英]Gremlin Python returning empty graph

I've started playing around with gremlin-python wrapper to interact with my gremlin server. 我开始玩gremlin-python包装器来与我的gremlin服务器进行交互。

I did the following steps: 我做了以下步骤:

./bin/gremlin.sh

Once the Gremlin console opens up, I loaded configurations using: 一旦Gremlin控制台打开,我使用以下命令加载配置:

graph = JanusGraphFactory.open('conf/gremlin-server/janusgraph-cassandra-es.properties')
g = graph.traversal()
saturn = g.V().has('name', 'saturn')

And the above set of codes in gremlin shell works fine, and I can see verteces listed down, but when I try to do same in python I get an empty graph. gremlin shell中的上面一组代码工作得很好,我可以看到列出的脊椎,但是当我尝试在python中做同样的事情时,我得到一个空图。 The following is my code for python: 以下是我的python代码:

graph = Graph()
g = graph.traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))
print(g)

It returns : graphtraversalsource[graph[empty]] 它返回: graphtraversalsource [graph [empty]]

Why am I getting empty graph? 为什么我得到空图? As far as I feel, it is unable to connect to same Graph source. 据我所知,它无法连接到相同的Graph源。 Is there somthing I'm missing? 有什么我想念的吗?

Note that in: 请注意:

JanusGraphFactory.open('conf/gremlin-server/janusgraph-cassandra-es.properties')

the config filename provided is one used to start gremlin server. 提供的配置文件名是用于启动gremlin服务器的文件名。

Any help is really appreciated. 任何帮助都非常感谢。

Thanks 谢谢

The reason you are seeing graph[empty] is because that's the actual string representation of the Python graph object -- see the code here . 你看到graph [empty]的原因是因为这是Python图形对象的实际字符串表示 - 请参阅此处代码 The graph may actually contain data though, so it would be better if it was something like graph[remote] or graph[] instead. 图形实际上可能包含数据,因此如果它像graph [remote]graph []那样会更好。 I've opened up an issue to address this. 我已经开辟了一个问题来解决这个问题

Out of the box, JanusGraph isn't configured for Python. 开箱即用,JanusGraph未配置Python。 You can find docs on how do this in the Apache TinkerPop docs . 您可以在Apache TinkerPop文档中找到有关如何执行此操作的文档 First install gremlin-python . 首先安装gremlin-python Here's the command assuming you're using JanusGraph 0.1.1 which uses TinkerPop 3.2.3: 假设您正在使用使用TinkerPop 3.2.3的JanusGraph 0.1.1,这是命令:

bin/gremlin-server.sh -i org.apache.tinkerpop gremlin-python 3.2.3

Next modify the conf/gremlin-server/gremlin-server.yaml to add the gremlin-python script engine: 接下来修改conf/gremlin-server/gremlin-server.yaml以添加gremlin-python脚本引擎:

scriptEngines: {
  gremlin-groovy: {
    imports: [java.lang.Math],
    staticImports: [java.lang.Math.PI],
    scripts: [scripts/empty-sample.groovy]},
  gremlin-jython: {},
  gremlin-python: {}
}

To use Gremlin Python, you need to go through a Gremlin Server, so start the JanusGraph pre-packaged distribution : 要使用Gremlin Python,您需要浏览Gremlin Server,因此启动JanusGraph 预打包发行版

bin/janusgraph.sh start

From the Gremlin Console: 从Gremlin控制台:

gremlin> graph = JanusGraphFactory.open('conf/janusgraph-cassandra-es.properties')
==>standardjanusgraph[cassandrathrift:[127.0.0.1]]
gremlin> GraphOfTheGodsFactory.load(graph)
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[standardjanusgraph[cassandrathrift:[127.0.0.1]], standard]
gremlin> g.V().count()
14:51:58 WARN  org.janusgraph.graphdb.transaction.StandardJanusGraphTx  - Query requires iterating over all vertices [()]. For better performance, use indexes
==>12

Install the Gremlin-Python driver, again matching on the TinkerPop version: 安装Gremlin-Python驱动程序,再次匹配TinkerPop版本:

pip install gremlinpython==3.2.3

From the Python 3 shell: 从Python 3 shell:

>>> 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()
>>> g = graph.traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))
>>> print(graph)
graph[empty]
>>> print(g)
graphtraversalsource[graph[empty]]
>>> g.V().count().next()
12
>>> g.addV('god').property('name', 'mars').property('age', 3500).next()
v[4280]
>>> g.V().count().next()
13

Keep in mind when you are working in the Python shell, the graph traversals are not automatically iterated, so you need to make sure to iterate the traversal with iterate() or next() or toList() . 请记住,当您在Python shell中工作时,图形遍历不会自动迭代,因此您需要确保使用iterate()next()toList() iterate()

Your local "g" in the Gremlin Console is an embedded instance of a graph. Gremlin控制台中的本地“g”是图形的嵌入式实例。 It therefore "contains" something and is not empty. 因此它“包含”某些东西而不是空的。 For your "g" in Python, it is "empty" in the sense that on its own there are no vertices/edges that within it - the vertices/edges are in the remote graph on Gremlin Server that it reflects. 对于Python中的“g”,它是“空的”,因为它本身没有顶点/边缘 - 顶点/边缘位于它反映的Gremlin Server上的远程图形中。 I assume that if you were to do a gV().count() in python you would get the same vertex count back as you would if you did the same in java. 我假设如果你在python中做一个gV().count()你会得到与你在java中做同样的顶点计数。 If not, then there is some other problem, but do not expect a "remote" graph instance to show vertex/edges of any sort (unless a day comes where gremlin-python is written as a Gremlin virtual machine that has it's own Python native graph databases attached to it - in such a case, "g" would be embedded and thus own vertices/edges and would likely no longer print as "empty"). 如果没有,那么还有一些其他问题,但是不要指望“远程”图形实例显示任何排序的顶点/边缘(除非有一天gremlin-python被写为具有它自己的Python原生的Gremlin虚拟机)附加到它的图形数据库 - 在这种情况下,“g”将被嵌入并因此拥有顶点/边缘并且可能不再打印为“空”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM