Azure cosmosdb 如何使用Gremlin查询将所有节点作为一个层次树来获取?
以下是用节点和边表示的复杂族成员关系Azure cosmosdb 如何使用Gremlin查询将所有节点作为一个层次树来获取?,azure-cosmosdb,gremlin,Azure Cosmosdb,Gremlin,以下是用节点和边表示的复杂族成员关系 表示为person节点的成员 婚姻节点表示两个个人节点之间的婚姻 的儿子和女儿将一个人连接到婚姻节点 root\u person是第一代 john和Brenda是第二代 mark是第三代 mary是第四代 约翰有前妻和前妻 mark和他的前妻mark\u前妻是表亲。这是因为马克的父亲和前妻是兄弟姐妹 我的要求是以层次树格式获取所有节点。从“根”人开始,他的儿子约翰、妻子、女儿布伦达和她的丈夫应该被归还。然后是下一代等等 请回答我的需求是否可以通过此模型实现,
person
节点的成员婚姻
节点表示两个个人
节点之间的婚姻的儿子和女儿将一个人连接到婚姻节点
root\u person
是第一代john
和Brenda
是第二代mark
是第三代mary
是第四代约翰
有前妻和前妻mark
和他的前妻mark\u前妻
是表亲。这是因为马克的父亲和前妻是兄弟姐妹键
和值
的JSON就是开始。然后,我可以解析查询返回的JSON,以更容易理解和打印的自定义格式创建父级和子级的层次结构树
g.V().drop()
g.E().drop()
g.addV('person').property(id, 'root_person').property('name', 'Root Person').property('title', 'male')
g.addV('person').property(id, 'root_person_wife').property('name', 'root_person_wife').property('title', 'female')
g.addV('person').property(id, 'john').property('name', 'john').property('title', 'male')
g.addV('person').property(id, 'john_wife').property('name', 'john_wife').property('title', 'female')
g.addV('person').property(id, 'john_ex_wife').property('name', 'john_ex_wife').property('title', 'female')
g.addV('person').property(id, 'brenda').property('name', 'brenda').property('title', 'female')
g.addV('person').property(id, 'brenda_husband').property('name', 'brenda_husband').property('title', 'male')
g.addV('person').property(id, 'mark').property('name', 'mark').property('title', 'male')
g.addV('person').property(id, 'mark_ex_wife').property('name', 'mark_ex_wife').property('title', 'female')
g.addV('person').property(id, 'mark_wife').property('name', 'mark_wife').property('title', 'female')
g.addV('person').property(id, 'mary').property('name', 'mary').property('title', 'female')
g.addV('person').property(id, 'mary_husband').property('name', 'mary Husband').property('title', 'male')
g.addV('marriage').property(id, 'root_person-root_person_wife').property('marriage_name', 'root_person-root_person_wife')
g.addV('marriage').property(id, 'john-john_wife').property('marriage_name', 'john-john_wife')
g.addV('marriage').property(id, 'john-john_ex_wife').property('marriage_name', 'john-john_ex_wife')
g.addV('marriage').property(id, 'brenda-brenda_husband').property('marriage_name', 'brenda-brenda_husband')
g.addV('marriage').property(id, 'mark-mark_ex_wife').property('marriage_name', 'mark-mark_ex_wife')
g.addV('marriage').property(id, 'mark-mark_wife').property('marriage_name', 'mark-mark_wife')
g.addV('marriage').property(id, 'mary-mary_husband').property('marriage_name', 'mary-mary_husband')
g.V('root_person').addE('married_in').to(g.V('root_person-root_person_wife'))
g.V('root_person_wife').addE('married_in').to(g.V('root_person-root_person_wife'))
g.V('john').addE('married_in').to(g.V('john-john_wife'))
g.V('john_wife').addE('married_in').to(g.V('john-john_wife'))
g.V('john').addE('married_in').to(g.V('john-john_ex_wife'))
g.V('john_ex_wife').addE('married_in').to(g.V('john-john_ex_wife'))
g.V('brenda').addE('married_in').to(g.V('brenda-brenda_husband'))
g.V('brenda_husband').addE('married_in').to(g.V('brenda-brenda_husband'))
g.V('mark').addE('married_in').to(g.V('mark_#mark_wife'))
g.V('mark_wife').addE('married_in').to(g.V('mark-mark_wife'))
g.V('mark').addE('married_in').to(g.V('mark-mark_ex_wife'))
g.V('mark_ex_wife').addE('married_in').to(g.V('mark-mark_ex_wife'))
g.V('mary').addE('married_in').to(g.V('mary-mary_husband'))
g.V('mary_husband').addE('married_in').to(g.V('mary-mary_husband'))
g.V('john').addE('son_of').to(g.V('root_person-root_person_wife')).property('son_of', 'john--root_person-root_person_wife')
g.V('brenda').addE('daughter_of').to(g.V('root_person-root_person_wife')).property('daugter_of', 'brenda--root_person-root_person_wife')
g.V('mark').addE('son_of').to(g.V('john-john_wife')).property('son_of', 'mark--john-john_wife')
g.V('mark_ex_wife').addE('daughter_of').to(g.V('brenda-brenda_husband')).property('daugter_of', 'mark_ex_wife--brenda-brenda_husband')
g.V('mary').addE('daughter_of').to(g.V('mark-mark_wife')).property('daugter_of', 'mary--mark-mark_wife') }
更新:
我在我的cosmosdb
queryexplorer中尝试了以下查询,但它没有引入所有节点
g.V('root_person').repeat(out('married_in')).emit().repeat(__.in('son_of', 'daughter_of')).emit().tree()
查询生成了以下json。你会注意到它在第二代时就停止了
[
{
"root_person": {
"key": {
"id": "root_person",
"label": "person",
"type": "vertex",
"properties": {
"name": [
{
"id": "f1e8327e-add4-454b-bbb2-6f076a29ea49",
"value": "Root Person"
}
],
"title": [
{
"id": "42fc9c2b-b613-4011-9a0a-183d907a96ac",
"value": "male"
}
]
}
},
"value": {
"root_person-root_person_wife": {
"key": {
"id": "root_person-root_person_wife",
"label": "marriage",
"type": "vertex",
"properties": {
"marriage_name": [
{
"id": "30f428a9-e657-4284-b59a-b836d9f04887",
"value": "root_person-root_person_wife"
}
]
}
},
"value": {
"john": {
"key": {
"id": "john",
"label": "person",
"type": "vertex",
"properties": {
"name": [
{
"id": "64e4653d-6298-479d-ba0f-eb3a9ede2383",
"value": "john"
}
],
"title": [
{
"id": "b99dacb3-e38d-4da8-b087-42860d2b28c0",
"value": "male"
}
]
}
},
"value": {}
},
"brenda": {
"key": {
"id": "brenda",
"label": "person",
"type": "vertex",
"properties": {
"name": [
{
"id": "1d88e8a5-7340-4b6d-a2b9-8b3c325e7bf6",
"value": "brenda"
}
],
"title": [
{
"id": "9b856caa-0b84-413c-8229-8e2c1e0cb0d3",
"value": "female"
}
]
}
},
"value": {}
}
}
}
}
}
}
]
树将表示存储在图形中的相同层次中的顶点。因此,配偶不会出现在树中的同一级别。我可能错了,但我想你想要的是每一代人的名单
gremlin> g.V("root_person").
union(identity(),
out("married_in").in("married_in")).dedup().
aggregate("x").
group("m").
by(constant(-1)).
by(id).
repeat(out("married_in").
union(__.in("married_in"),
__.in("daughter_of","son_of").
union(identity(),
out("married_in").in("married_in"))).dedup().
where(without("x")).
aggregate("x").
group("m").
by(loops()).
by(id)).
cap("m").unfold().
order(local).
by(keys).
select(values)
==>[root_person,root_person_wife]
==>[brenda,brenda_husband,john,john_wife,john_ex_wife]
==>[mark_ex_wife,mark,mark_wife]
==>[mary,mary_husband]
请修复创建示例图的脚本。如果要通过
V('mark')
访问顶点,必须将其id
设置为mark
,而不仅仅是名为id
的属性(例如addV('person')。属性(id,'mark')
)。接下来,确保事情是一致的。在edge creation语句中,您指的是从未创建过的元素。修复这些问题可能会解决整个问题。我修复了脚本并更新了原始帖子。有一个婚姻
顶点缺失。我还更改了id
。但这没有任何区别。查询在第二代时停止,如您在结果中所看到的。我不知道如何以bredth first样式遍历每个节点并生成所有节点。我正在努力理解上述查询将如何执行。我使用的是CosmosDB DBExplorer,直接在浏览器中运行查询。DBExplorer只支持所述的几个步骤。我不得不使用您在另一个版本中建议的查询来获取所有节点和顶点。Neo4J cypher查询是否遵循函数式语言方法(如Gremlin语言)?有太多的资源可以学习Neo4J,所以考虑一下对DAG进行查询以在Neo4J中生成树。你推荐什么?Cypher是纯声明性的,但是你可以使用Gremlin而不是Neo4J。