通过不使用cosmos DB gremlin API的预计值进行分组

时间:2020-07-20 10:37:59

标签: azure azure-cosmosdb gremlin azure-cosmosdb-gremlinapi

嗨,我正在处理下图graph model of the current scenario

我正在从学校顶点遍历到主题,游戏和爱好顶点

所以我写了下面的查询,向我提供了所有学生的详细信息

g.V().hasLabel("school").as('school').out().hasLabel('class').out().hasLabel('student')
.project('student_name','student_details').by('name').by(project('student_description',"reads","plays","havehobbies")
.by('description')
.by(__.outE('reads').inV().values('subject_name').limit(5).dedup().fold())
.by(__.outE("plays").inV().values('game_name').limit(5).dedup().fold())
.by(__.outE('have_hobby').inV().values('hobby_name').dedup().limit(5).fold()).dedup().fold()).dedup()

输出:

[
{
            "student_name": "santhosh kurnapally",
            "student_details": [
                {
                    "student_description": "very good student with high IQ",
                    "reads": [
                        "maths",
                        "science",
                        "social"
                    ],
                    "plays": [
                        "cricket",
                        "football"
                    ],
                    "havehobbies": [
                        "news paper",
                        "tv watching",
                        "cycling"
                    ]
                }
            ]
        },
        {
            "student_name": "santhosh kurnapally",
            "student_details": [
               {
                    "student_description": "very bad student with low IQ",
                    "reads": [
                        "maths",
                        "science"
                    ],
                    "plays": [
                        "cricket",
                        "football"
                    ],
                    "havehobbies": [
                        "news paper",
                        "tv watching"
                    ]
                }
            ]
        },
        {
            "student_name": "neerja goswami",
            "student_details": [
               {
                    "student_description": "very good student with very high IQ",
                    "reads": [
                        "maths",
                        "science",
            "english"
                    ],
                    "plays": [
                        "throw ball",
                        "carroms"
                    ],
                    "havehobbies": [
                        "news paper",
                        "quilling"
                    ]
                }
            ]
        }
]

与上面的输出一样,“ student_name”正在重复,并且希望按“ student_name”进行分组


g.V().hasLabel("school").as('school').out().hasLabel('class').out().hasLabel('student')
.project('student_name','student_details').by('name').by(project('student_description',"reads","plays","havehobbies")
.by('description')
.by(__.outE('reads').inV().values('subject_name').limit(5).dedup().fold())
.by(__.outE("plays").inV().values('game_name').limit(5).dedup().fold())
.by(__.outE('have_hobby').inV().values('hobby_name').dedup().limit(5).fold()).dedup().fold()).dedup()
.group().by('student_name')

但上面的Query抛出如下错误


ActivityId : 6fa77108-39e3-4f77-ba09-2a2e6ca80d9a
ExceptionType : GraphCompileException
ExceptionMessage :
        Gremlin Query Compilation Error: Column reference R_17["student_name"] cannot be located in the raw records in the current execution pipeline.

请让我知道如何达到上述要求 或达到上述要求的任何其他选择 一个可以是“我可以在学生顶点上分组,然后稍后进行投影”

在这种情况下,我的输出可以如下

[
{
            "santhosh kurnapally": [
                {
                    "student_description": ["very good student with high IQ","very bad student with low IQ"]
                    "reads": [
                        "maths",
                        "science",
                        "social"
                    ],
                    "plays": [
                        "cricket",
                        "football"
                    ],
                    "havehobbies": [
                        "news paper",
                        "tv watching",
                        "cycling"
                    ]
                }
            ]
        },
        {
            "neerja goswami": [
               {
                    "student_description": ["very good student with very high IQ"],
                    "reads": [
                        "maths",
                        "science",
            "english"
                    ],
                    "plays": [
                        "throw ball",
                        "carroms"
                    ],
                    "havehobbies": [
                        "news paper",
                        "quilling"
                    ]
                }
            ]
        }
]

注意:输出结果可能不准确,但关键点是根据投影值进行分组,或者对分组后的顶点(或按其他方式进行分组)或其他任何替代方法进行遍历,并对其进行遍历。

请让我知道与此相同或任何其他替代方法查询的可能性

1 个答案:

答案 0 :(得分:0)

您应该使用select步骤

g.V().hasLabel('School').as('school').
  out().hasLabel('Class').out().
  hasLabel('Student').
  project('student_name', 'student_details').
    by('name').
    by(project('student_description', 'reads', 'plays', 'havehobbies').
        by('description').
        by(__.outE('reads').inV().
          values('subject_name').limit(5).dedup().
          fold()).
        by(__.outE('plays').inV().values('game_name').
          limit(5).dedup().fold()).
        by(__.outE('have_hobby').inV().values('hobby_name').
          dedup().limit(5).fold()).
      dedup().fold()).
  dedup().
  group().
    by(select('student_name'))

示例:https://gremlify.com/vfnvlkfvybc