【问题标题】:Gremlin-python: How to use user-defined-steps to reuse traversal logicGremlin-python:如何使用用户定义的步骤来重用遍历逻辑
【发布时间】:2020-11-04 10:27:01
【问题描述】:

我正在尝试简化以下遍历:

operation_dict['output_schema'] = g.V(operation_id).outE('uses').inV()\
.project('id','label','attribute_id', 'attribute_name', 'dataType')\
.by(T.id).by(T.label).by('attribute_id').by('attribute_name').by('dataType').toList()

由于我想重用投影遍历,我想从遍历中提取它,如下面的sn-p:

def extract_attribute(x):
  return g.V(x).project('id','label','attribute_id', 'attribute_name', 'dataType')\
  .by(T.id).by(T.label).by('attribute_id').by('attribute_name').by('dataType')


operation_dict['input_schema'] = g.V(operation_id).inE('follows').outV().outE('uses').inV()\
    .map(lambda x: extract_attribute(x)).toList()

如何在 Gremlin for Python 中做到这一点?我尝试了 Lambda 功能,但到目前为止没有成功。

【问题讨论】:

    标签: gremlin tinkerpop gremlinpython


    【解决方案1】:

    有几种方法可以做到这一点,但这里有一种方法符合您的尝试:

    >>> def c(x):
    ...     return __.project('x').by(x)
    ... 
    >>> g.V().map(c('name')).toList()
    [{'x': 'marko'}, {'x': 'vadas'}, {'x': 'lop'}, {'x': 'josh'}, {'x': 'ripple'}, {'x': 'peter'}]
    

    您只需要在您的extract_attribute() 函数中生成一个anonymous child traversal。另一种更高级的重用遍历逻辑的方法是构建custom Gremlin DSL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      相关资源
      最近更新 更多