Javascript Janusgraph很难理解如何从gremlin查询中操作数据

Javascript Janusgraph很难理解如何从gremlin查询中操作数据,javascript,gremlin,janusgraph,Javascript,Gremlin,Janusgraph,我正试图从一个图中重建通过一个小精灵查询检索到的数据。 确切地说,我很难找到一种有效的方法来处理返回的数据 我正在使用的JanusGraph版本是0.3.1,它运行在Cassandra+ES上,并且配置了ConfiguredGraphFactory。所以我可以动态地创建图形 我使用的是GremlinJavaScript版本3.4.2(3.3.3不能正常工作) 我举了一个小例子来更好地解释我的意思。 本例中的图形是在没有模式的情况下创建的 首先,我将使用以下函数连接到图形: const greml

我正试图从一个图中重建通过一个小精灵查询检索到的数据。 确切地说,我很难找到一种有效的方法来处理返回的数据

我正在使用的JanusGraph版本是0.3.1,它运行在Cassandra+ES上,并且配置了ConfiguredGraphFactory。所以我可以动态地创建图形

我使用的是GremlinJavaScript版本3.4.2(3.3.3不能正常工作)

我举了一个小例子来更好地解释我的意思。 本例中的图形是在没有模式的情况下创建的

首先,我将使用以下函数连接到图形:

const gremlin=require('gremlin');
const{Graph}=gremlin.structure;
const{DriverRemoteConnection}=gremlin.driver;
常量=gremlin.process.statics;
const P=gremlin.process.P;
const GREMLIN_URL=“ws://localhost:8182/GREMLIN”;
const GRAPH_NAME=“graphtest”;
让连接;
函数getTraversal(){
常量图=新图();
connection=newdriverremoteconnection(GREMLIN_URL,{traversalSource:GRAPH_NAME});
返回g=graph.traversal().withRemote(连接);
}
函数closeConnection(){
if(connection&&connection.close)connection.close();
}
然后我将创建一些顶点和边

我们将有一个用户、两个机构和两个“训练有素”的边缘将用户连接到机构:

异步函数createVerticesAndEdges(){ const g=getTraversal(); const userV=wait g.addV('user').property('name','Emily').property('identityId','1234').next(); log('user',userV); const institutionV=wait g.addV('institution').property('name','University of California').property('identityId','CA83').next(); 控制台日志(“机构”,机构v); const institutionV2=wait g.addV('institution')。property('name','University of Illinois')。property('identityId','IL847')。next(); console.log('institution2',institutionV2); const trainedE=wait g.addE('trained')。属性('title','MS in Computer Science')。属性('grade','B')) .from_uuuv().has('identityId','1234')。to('uuuv().has('identityId','CA83'))。next(); 控制台日志(“已培训”,已培训); const trainedE2=等待g.addE('trained')。财产('title','POLICATION')。财产('grade','A')) .from_uuuv().has('identityId','1234')。to('uuuv().has('identityId','IL847'))。next(); 控制台日志(“培训2”,培训2); closeConnection(); } 然后,假设我想检索用户参加的所有培训,我还想知道参加培训的机构的名称

因此,我运行的查询是:

异步函数getUserTrainings(){ const g=getTraversal(); 常数结果=等待g.V() .hasLabel('用户') .has('identityId','1234') .as('u').outE() .hasLabel('trained') .inV() .path() .展开() .其中(P.neq('u')) .toList(); closeConnection(); 控制台日志(结果); } 我得到这个输出:

[ 
    Edge {
    id: { relationId: 'odxqw-3b4-27th-38o'
        },                                                                                                                                                                                     alber@DESKTOP-8CVHP91 MINGW64 ~/Ref    label: 'trained',
    outV: 4288,
    inV: 4200,
    properties: {}
    },
  Vertex { id: 4200, label: 'institution', properties: undefined
    },
  Edge {
    id: { relationId: 'odxco-3b4-27th-3ao'
        },
    label: 'trained',
    outV: 4288,
    inV: 4272,
    properties: {}
    },
  Vertex { id: 4272, label: 'institution', properties: undefined
    }
]
[ 
    Map {
    EnumValue { typeName: 'T', elementName: 'id'
        } => { relationId: 'odxqw-3b4-27th-38o'
        },
    'title' => 'Political Science',
    EnumValue { typeName: 'T', elementName: 'label'
        } => 'trained',
    'grade' => 'A'
    },
  Map {
    'name' => [ 'University of Illinois'
        ],
    EnumValue { typeName: 'T', elementName: 'id'
        } => 4200,
    'identityId' => [ 'IL847'
        ],
    EnumValue { typeName: 'T', elementName: 'label'
        } => 'institution'
    },
  Map {
    EnumValue { typeName: 'T', elementName: 'id'
        } => { relationId: 'odxco-3b4-27th-3ao'
        },
    'title' => 'MS in Computer Science',
    EnumValue { typeName: 'T', elementName: 'label'
        } => 'trained',
    'grade' => 'B'
    },
  Map {
    'name' => [ 'University of California'
        ],
    EnumValue { typeName: 'T', elementName: 'id'
        } => 4272,
    'identityId' => [ 'CA83'
        ],
    EnumValue { typeName: 'T', elementName: 'label'
        } => 'institution'
    }
]
这不是bat,我可以使用顶点ID和边inV来重建关系并返回我想要的数据,但问题是,正如您所看到的,此查询不返回属性。所以这有点没用

但是,通过查看gremlin文档,我找到了valueMap()步骤,因此我可以像这样稍微编辑前面的查询:

异步函数getUserTrainings(){ const g=getTraversal(); 常数结果=等待g.V() .hasLabel('用户') .has('identityId','1234') .as('u').outE() .hasLabel('trained') .inV() .path() .展开() .其中(P.neq('u')) .valueMap(真) .toList(); closeConnection(); 控制台日志(结果); } 要获得此输出,请执行以下操作:

[ 
    Edge {
    id: { relationId: 'odxqw-3b4-27th-38o'
        },                                                                                                                                                                                     alber@DESKTOP-8CVHP91 MINGW64 ~/Ref    label: 'trained',
    outV: 4288,
    inV: 4200,
    properties: {}
    },
  Vertex { id: 4200, label: 'institution', properties: undefined
    },
  Edge {
    id: { relationId: 'odxco-3b4-27th-3ao'
        },
    label: 'trained',
    outV: 4288,
    inV: 4272,
    properties: {}
    },
  Vertex { id: 4272, label: 'institution', properties: undefined
    }
]
[ 
    Map {
    EnumValue { typeName: 'T', elementName: 'id'
        } => { relationId: 'odxqw-3b4-27th-38o'
        },
    'title' => 'Political Science',
    EnumValue { typeName: 'T', elementName: 'label'
        } => 'trained',
    'grade' => 'A'
    },
  Map {
    'name' => [ 'University of Illinois'
        ],
    EnumValue { typeName: 'T', elementName: 'id'
        } => 4200,
    'identityId' => [ 'IL847'
        ],
    EnumValue { typeName: 'T', elementName: 'label'
        } => 'institution'
    },
  Map {
    EnumValue { typeName: 'T', elementName: 'id'
        } => { relationId: 'odxco-3b4-27th-3ao'
        },
    'title' => 'MS in Computer Science',
    EnumValue { typeName: 'T', elementName: 'label'
        } => 'trained',
    'grade' => 'B'
    },
  Map {
    'name' => [ 'University of California'
        ],
    EnumValue { typeName: 'T', elementName: 'id'
        } => 4272,
    'identityId' => [ 'CA83'
        ],
    EnumValue { typeName: 'T', elementName: 'label'
        } => 'institution'
    }
]
所以,除了返回的数据一点也不清楚(我的意思是,每个顶点的“typeName:'T'”是什么意思?),现在我确实得到了属性,但我在边缘上松开了outV和inV,无法以我需要的方式重建数据。(了解哪个顶点由哪个边连接)

我想我可以不使用valueMap()步骤而直接使用第一个查询,然后对于我得到的每个顶点和边,我将使用ID执行另一个查询来检索属性

对于这样一个简单的情况,这是可以的,但我认为对于涉及数百个顶点和边的查询,这并不是很有效


因此,最后一个问题是:从路径重建数据(包括顶点和边属性)的最有效方法是什么?

我将转到我认为是您问题核心的部分,因为我认为这一问题的直接答案可能会澄清其他子问题:

假设我想检索用户参加的所有培训,我还想知道参加培训的机构的名称

我认为需要澄清的一个附带问题是:

每个顶点都相同的“typeName:'T'”是什么


T
是Gremlin中的枚举值,表示图形元素的一些核心结构属性,特别是
label
id

非常感谢您的回答。事实上,我现在才发现,通过使用gremlin库,我可以在从gremlin.process.t导入t的映射中访问这些枚举值。关于遍历的问题,我想对小精灵来说是个新问题。不管怎样,你建议我的遍历真的很好,有没有一种方法也可以用类似的东西得到relationId和vertexId?再次感谢您的时间安排…只需将更多键添加到
project()
,然后为每个键添加一个
by()
调制器,以转换“trai”