首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gremlin Python project By子句

Gremlin Python project By子句
EN

Stack Overflow用户
提问于 2017-11-20 22:01:27
回答 1查看 1.2K关注 0票数 2

在Datastax企业图(5.1版本)上运行一个图形,运行在Cassandra存储上。尝试运行一个查询以获取ID和属性。在Gremlin控制台,我可以这样做:

代码语言:javascript
复制
gremlin> g.V(1).project("v", "properties").by().by(valueMap())
==>[v:v[1],properties:[name:[marko],age:[29]]]

如何翻译仍然使用Python的valueMap调用。我知道我可以通过会话执行直接运行查询,如下所示。

代码语言:javascript
复制
session.execute_graph("g.V().has(\"Node_Name\",\"A\").project(\"v\", \"properties\").by().by(valueMap())",{"name":graph_name})

下面是我的设置代码。

代码语言:javascript
复制
from dse.cluster import Cluster, EXEC_PROFILE_GRAPH_DEFAULT
from dse_graph import DseGraph
from dse.cluster import GraphExecutionProfile, EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT
from dse.graph import GraphOptions
from gremlin_python.process.traversal import T
from gremlin_python.process.traversal import Order
from gremlin_python.process.traversal import Cardinality
from gremlin_python.process.traversal import Column
from gremlin_python.process.traversal import Direction
from gremlin_python.process.traversal import Operator
from gremlin_python.process.traversal import P
from gremlin_python.process.traversal import Pop
from gremlin_python.process.traversal import Scope
from gremlin_python.process.traversal import Barrier
graph_name = "TEST"
graph_ip = ["127.0.0.1"]
graph_port = 9042
schema = """
schema.edgeLabel("Group").create();
schema.propertyKey("Version").Text().create();
schema.edgeLabel("Group").properties("Version").add()
schema.vertexLabel("Example").create();
schema.edgeLabel("Group").connection("Example", "Example").add()
schema.propertyKey("Node_Name").Text().create();
schema.vertexLabel("Example").properties("Node_Name").add()
schema.vertexLabel("Example").index("exampleByName").secondary().by("Node_Name").add();
"""
profile = GraphExecutionProfile(
    graph_options=GraphOptions(graph_name=graph_name))
client = Cluster(
    contact_points=graph_ip, port=graph_port,
    execution_profiles={EXEC_PROFILE_GRAPH_DEFAULT: profile}
)
graph_name = graph_name
session = client.connect()
graph = DseGraph.traversal_source(session)

# force the schema to be clean
session.execute_graph(
    "system.graph(name).ifExists().drop();",
    {'name': graph_name},
    execution_profile=EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT
)
session.execute_graph(
    "system.graph(name).ifNotExists().create();",
    {'name': graph_name},
    execution_profile=EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT
)
session.execute_graph(schema)
session.shutdown()
session = client.connect()
graph = DseGraph.traversal_source(session)

更新:

我想我还没有把问题说清楚。它在python中而不是gremlin控制台中。因此,运行像graph.V().has("Node_Name","A").project("v","properties").by().by(valueMap()).toList()这样的代码将给出以下结果。如何在仍处于GLV级别的情况下执行gremlin查询,而不降至Gremlin的文本序列化查询?

代码语言:javascript
复制
Traceback (most recent call last):
  File "graph_test.py", line 79, in <module>
    graph.V().has("Node_Name","A").project("v", "properties").by().by(valueMap()).toList()
NameError: name 'valueMap' is not defined
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-21 11:48:28

我可能不完全理解你的问题,但你似乎在很大程度上有了答案。这是最后一行代码:

代码语言:javascript
复制
graph = DseGraph.traversal_source(session)

很可能应该写成:

代码语言:javascript
复制
g = DseGraph.traversal_source(session)

traversal_source(session)的返回值是一个TraversalSource,而不是一个Graph实例,按照惯例,TinkerPop倾向于引用像g这样的变量。一旦你有了一个TraversalSource,你就可以写你的格林了。

代码语言:javascript
复制
g = DseGraph.traversal_source(session)
g.V().has("Node_Name","A").project("v", "properties").by().by(valueMap()).toList()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47401842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档