代码之家  ›  专栏  ›  技术社区  ›  Jonas Stensved

使用tinkerpop3在同一查询中输出垂直点和相邻顶点

  •  2
  • Jonas Stensved  · 技术社区  · 8 年前

    我对gremlin是新手,我想知道如何使用Azure Cosmos DB和GraphSON在相同的结果中获得一篇文章以及作者和附件。

    我的图表如下所示:

    [User] <- (edge: author) - [Article] - (edge: attachments) -> [File1, File2]
    

    我试图获取的内容类似于以下伪代码:

    {
    article: {...},
    author: [{author1}],
    attachment: [{file1}, {file2}]
    }
    

    我目前的尝试:

    g.V().hasLabel('article').as('article').out('author', 'attachments').as('author','attachments').select('article', 'author', 'attachments')
    

    如何编写查询以获得不同的值?

    1 回复  |  直到 8 年前
        1
  •  4
  •   stephen mallette    8 年前

    当询问有关Gremlin的问题时,以如下形式提供一些示例数据总是很有帮助的:

    g.addV('user').property('name','jim').as('jim').
      addV('user').property('name','alice').as('alice').
      addV('user').property('name','bill').as('bill').
      addV('article').property('title','Gremlin for Beginners').as('article').
      addV('file').property('file','/files/a.png').as('a').
      addV('file').property('file','/files/b.png').as('b').
      addE('authoredBy').from('article').to('jim').
      addE('authoredBy').from('article').to('alice').
      addE('authoredBy').from('article').to('bill').
      addE('attaches').from('article').to('a').
      addE('attaches').from('article').to('b').iterate()
    

    请注意,我将边缘标签名称修改为更像动词的名称,以便它们与类似名词的顶点标签更好地区分开来。它倾向于与边缘方向一致,如: article --authoredBy-> user

    无论如何,你的问题最容易用 project() step

    gremlin> g.V().has('article','title','Gremlin for Beginners').
    ......1>   project('article','authors','attachments').
    ......2>     by().
    ......3>     by(out('authoredBy').fold()).
    ......4>     by(out('attaches').fold())
    ==>[article:v[6],authors:[v[0],v[2],v[4]],attachments:[v[10],v[8]]]
    

    fold() by() 步骤-这将强制内部遍历的完整迭代,并将其放入列表中。如果你错过了这一步,你只会得到一个结果(即第一个)。

    valueMap() 接下来是结果,这样你可以更好地看到上面顶点中包含的属性。

    gremlin> g.V().has('article','title','Gremlin for Beginners').
    ......1>   project('article','authors','attachments').
    ......2>     by(valueMap()).
    ......3>     by(out('authoredBy').valueMap().fold()).
    ......4>     by(out('attaches').valueMap().fold()).next()
    ==>article={title=[Gremlin for Beginners]}
    ==>authors=[{name=[jim]}, {name=[alice]}, {name=[bill]}]
    ==>attachments=[{file=[/files/b.png]}, {file=[/files/a.png]}]