【问题标题】:Gremlin Python project By clauseGremlin Python 项目 By 子句
【发布时间】:2017-11-20 22:01:27
【问题描述】:

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

gremlin> g.V(1).project("v", "properties").by().by(valueMap())
==>[v:v[1],properties:[name:[marko],age:[29]]]

如何翻译仍使用 Python GraphTraversal API 的 valueMap 调用。我知道我可以像这样通过 Session Execution 运行直接查询。

session.execute_graph("g.V().has(\"Node_Name\",\"A\").project(\"v\", \"properties\").by().by(valueMap())",{"name":graph_name})

下面是我的设置代码。

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-Server 的文本序列化查询?

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

【问题讨论】:

    标签: graph gremlin tinkerpop3 datastax-enterprise-graph gremlinpython


    【解决方案1】:

    我可能不完全理解您的问题,但您似乎在很大程度上已经找到了答案。最后一行代码:

    graph = DseGraph.traversal_source(session)
    

    应该写成:

    g = DseGraph.traversal_source(session)
    

    traversal_source(session) 的返回值是TraversalSource 而不是Graph 实例,按照惯例,TinkerPop 倾向于将这样的变量称为g。一旦你有了TraversalSource,你就可以写你的 Gremlin。

    g = DseGraph.traversal_source(session)
    g.V().has("Node_Name","A").project("v", "properties").by().by(valueMap()).toList()
    

    【讨论】:

    • 我想我的问题还没有说清楚。它在 Python 中,我不能像在常规 Gremlin-Console 中那样直接将 valueMap 放在 by() 子句中。
    • 这个链接应该有帮助:tinkerpop.apache.org/docs/current/reference/…
    • 你为什么这么说?
    • 因为“by(valueMap())”不是有效的python代码。并且在 python 中没有找到太多关于 Gremlin 的文档/示例。大多数是 Groovy 语法中的 Gremlin。
    • 好吧,如果您有兴趣,我们做出了设计决定,让 Gremlin 代码在所有语言变体中都显示为“Gremlin”。通过这种方式,无论您使用 Java、Python 还是 C# 或其他任何东西,Gremlin 通常看起来总是一样的。这并不总是可能的,但 TinkerPop 可能会改变该语言可能惯用的规则。在 python 的情况下,这意味着方法命名为 valueMap() 而不是 value_map()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2011-09-07
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    相关资源
    最近更新 更多