Gremlin查询不均匀的结果问题

时间:2017-10-27 06:04:08

标签: gremlin

假设我有3名学生(A,B,C)并且有一个重要的科目和标记,但是当我以不均匀的方式查询结果时。

数据

A - >数学 - > 77

B - >历史 - > 70

C - >科学 - > 97

查询

g.V('Class').has('name',within('A','B','C'))

结果

{"student_name":['A','B','C'], "major_subject":['Math','Science','History'], "marks":[70,77,97]}

根据学生的姓名,查询数据库显示的数据不符合规定。

1 个答案:

答案 0 :(得分:0)

我认为您的图表看起来像这样:

g = TinkerGraph.open().traversal()
g.addV('student').property('name', 'A').
    addE('scored').to(addV('subject').property('name', 'Math')).
      property('mark', 77).
  addV('student').property('name', 'B').
    addE('scored').to(addV('subject').property('name', 'History')).
      property('mark', 70).
  addV('student').property('name', 'C').
    addE('scored').to(addV('subject').property('name', 'Science')).
      property('mark', 97).iterate()

现在收集数据的最简单方法是:

gremlin> g.V().has('student', 'name', within('A', 'B', 'C')).as('student').
           outE('scored').as('mark').inV().as('major').
           select('student','major','mark').
             by('name').
             by('name').
             by('mark')
==>[student:A,major:Math,mark:77]
==>[student:B,major:History,mark:70]
==>[student:C,major:Science,mark:97]

但如果您真的依赖于问题中显示的格式,您可以这样做:

gremlin> g.V().has('student', 'name', within('A', 'B', 'C')).
           store('student').by('name').
           outE('scored').store('mark').by('mark').
           inV().store('major').by('name').
           cap('student','major','mark')
==>[major:[Math,History,Science],student:[A,B,C],mark:[77,70,97]]

如果您希望按标记排序上限结果,则需要混合使用2个查询:

gremlin> g.V().has('student', 'name', within('A', 'B', 'C')).as('a').
           outE('scored').as('b').
           order().
             by('mark').
           inV().as('c').
           select('a','c','b').
             by('name').
             by('name').
             by('mark').
           aggregate('student').by(select('a')).
           aggregate('major').by(select('b')).
           aggregate('mark').by(select('c')).
           cap('student','major','mark')
==>[major:[History,Math,Science],student:[B,A,C],mark:[70,77,97]]

按照输入顺序排序:

gremlin> input = ['C', 'B', 'A']; []
gremlin> g.V().has('student', 'name', within(input)).as('a').
           order().
             by {input.indexOf(it.value('name'))}.
           outE('scored').as('b').
           inV().as('c').
           select('a','c','b').
             by('name').
             by('name').
             by('mark').
           aggregate('student').by(select('a')).
           aggregate('major').by(select('b')).
           aggregate('mark').by(select('c')).
           cap('student','major','mark')
==>[major:[97,70,77],student:[C,B,A],mark:[Science,History,Math]]
相关问题