【问题标题】:How to form inner SubQuery in Gremlin Server (Titan 1.0)?如何在 Gremlin Server (Titan 1.0) 中形成内部子查询?
【发布时间】:2016-09-23 20:53:38
【问题描述】:

我正在使用以下查询:

g.V(741440).outE('Notification').order().by('PostedDateLong', decr).range(0,1).as('notificationInfo').match(
    __.as('notificationInfo').inV().as('postInfo'),
).select('notificationInfo','postInfo')

它给出以下结果:

{

"requestId": "9846447c-4217-4103-ac2e-de3536a3c62a",
"status": {
    "message": "",
    "code": ​200,
    "attributes": { }
},
"result": {
    "data": [
        {
            "notificationInfo": {
                "id": "c0zs-fw3k-347p-g2g0",
                "label": "Notification",
                "type": "edge",
                "inVLabel": "Comment",
                "outVLabel": "User",
                "inV": ​749664,
                "outV": ​741440,
                "properties": {
                    "ParentPostId": "823488",
                    "PostedDate": "2016-05-26T02:35:52.3889982Z",
                    "PostedDateLong": ​635998269523889982,
                    "Type": "CommentedOnPostNotification",
                    "NotificationInitiatedByVertexId": "1540312"
                }
            },
            "postInfo": {
                "id": ​749664,
                "label": "Comment",
                "type": "vertex",
                "properties": {
                    "PostImage": [
                        {
                            "id": "amto-g2g0-2wat",
                            "value": ""
                        }
                    ],
                    "PostedByUser": [
                        {
                            "id": "am18-g2g0-2txh",
                            "value": "orbitpage@gmail.com"
                        }
                    ],
                    "PostedTime": [
                        {
                            "id": "amfg-g2g0-2upx",
                            "value": "2016-05-26T02:35:39.1489483Z"
                        }
                    ],
                    "PostMessage": [
                        {
                            "id": "aln0-g2g0-2t51",
                            "value": "hi"
                        }
                    ]
                }
            }
        }
    ],
    "meta": { }
}

}

我也想在响应中获取顶点“NotificationInitiatedByVertexId”(边缘属性)的信息。 为此,我尝试了以下查询:

g.V(741440).outE('Notification').order().by('PostedDateLong', decr).range(0,2).as('notificationInfo').match(
  __.as('notificationInfo').inV().as('postInfo'),
  g.V(1540312).next().as('notificationByUser')
).select('notificationInfo','postInfo','notificationByUser')

注意:我在子查询中直接尝试使用顶点 ID,因为我不知道如何从查询本身的边缘属性中动态获取值。

它给出了错误。我尝试了很多,但找不到任何解决方案。

【问题讨论】:

  • 您可以将.range(0, 1) 替换为.limit(1)。另外,您能否更具体地说明您遇到的错误类型?你能提供一个最小的图表和预期的输出吗?

标签: graph titan gremlin tinkerpop3 gremlin-server


【解决方案1】:

我假设您将 Titan 生成的标识符存储在名为 NotificationInitiatedByVertexId 的边缘属性中。如果是这样,请考虑以下内容,即使第一部分并没有真正回答您的问题。我认为您不应该在边缘存储顶点标识符。您的图形模型应该明确地跟踪NotificationInitiatedBy 与边的关系,并且通过将顶点的标识符存储在边本身上,您可以绕过它。此外,如果您必须以某种方式迁移数据,则不会保留 id(Titan 会生成新的),并且尝试对其进行排序将是一团糟。

即使这不是 Titan 生成的标识符和您创建的逻辑标识符,我仍然认为我会调整您的图形模式并将 Notification 提升为顶点。然后,您的 Gremlin 遍历将更轻松。

现在,假设您不更改它,那么我看不出有理由不只是在同一个请求中发出两个查询,然后将结果组合到一个数据结构中。您只需要使用顶点 ID 进行查找,这将非常快速且成本低廉:

edgeStuff = g.V(741440).outE('Notification').
              order().by('PostedDateLong', decr).range(0,1).as('notificationInfo').
              ...  // whatever logic you have
              select('notificationInfo','postInfo').next()
vertexStuff = g.V(edgeStuff.get('notificationInfo').value('NotificationInitiatedByVertexId')).next()
[notificationInitiatedBy: vertexStuff, notification: edgeStuff]

【讨论】:

  • 我现在为通知创建了新的顶点。谢谢:)
猜你喜欢
  • 2016-09-15
  • 2016-10-26
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多