查询生成器和需求的依赖链

时间:2017-06-12 12:46:51

标签: neo4j cypher

我正在设置一个图形结构,其中包含变形金刚“需要”和“生成”一个或多个Kafka主题。我可以定义图形结构,但我想要一些查询帮助。

我想查询:创建特定主题所需的变换器和主题链,例如在下面的示例中,需要哪些变换器来生成Topic3。我希望

Ingest1->Topic7->T1->Topic1->T2->Topic3

下面的第一个答案不太正确,因为它没有考虑需求和产生的交替方向。 一个正确的查询到某个深度就像是

MATCH (topic:Topic{name:"topic-3"})
<-[:produces]- (tr1)  -[:requires]->(tp1) 
<-[:produces]- (tr2)  -[:requires]->(tp2) 
<-[:produces]- (tr3)  
return [topic,tr1,tp1,tr2,tp2,tr3] as List

所以看来我正在寻找能够重复配对产生/需要顶点的东西。

以下是我正在玩的一些数据。

CREATE (DB1:Database {backbone: true, name:"postgres db 1"})
CREATE (Ingest1:Ingest {backbone: true, name: "ingest-1"})

CREATE (KV1:KV {name: "key-value store 1"})
CREATE (KV2:KV {name: "key-value store 2"})
CREATE (KV1)-[:requires]->(DB1)
CREATE (KV2)-[:requires]->(DB1)
CREATE (Topic1:Topic {name: "topic-1", partitions:100})
CREATE (Topic2:Topic {name: "topic-2", partitions:100})
CREATE (Topic3:Topic {name: "topic-3", partitions:100})
CREATE (Topic4:Topic {name: "topic-4", partitions:100})
CREATE (Topic5:Topic {name: "topic-5", partitions:100})
CREATE (Topic6:Topic {name: "topic-6", partitions:100})
CREATE (Topic7:Topic {name: "topic-7", partitions:100})
CREATE (Topic8:Topic {name: "topic-8", partitions:100})
CREATE (T2:Transformer {name: "T2"})
CREATE (T1:Transformer {name: "T1"})
CREATE (T3:Transformer {name: "T3"})
CREATE (T4:Transformer {name: "T4"})
CREATE (T5:Transformer {name: "T5"})
CREATE (T6:Transformer {name: "T6"})
CREATE (T7:Transformer {name: "T7"})
CREATE (T8:Transformer {name: "T8"})
CREATE (T9:Transformer {name: "T9"})
CREATE (T4)-[:requires]->(Topic3)
CREATE (T5)-[:requires]->(Topic3)
CREATE (T2)-[:produces]->(Topic3)
CREATE (T2)-[:produces]->(Topic4)
CREATE (T2)-[:produces]->(KV1)
CREATE (T2)-[:requires]->(Topic1)
CREATE (T4)-[:produces]->(Topic5)

CREATE (T2)-[:requires]->(Topic2)
CREATE (T1)-[:produces]->(Topic1)
CREATE (T1)-[:requires]->(Topic7)
CREATE (T3)-[:produces]->(Topic2)
CREATE (T3)-[:requires]->(Topic8)
CREATE (Ingest1)-[:produces]->(Topic7)
CREATE (Ingest1)-[:produces]->(Topic8);

1 个答案:

答案 0 :(得分:1)

这样的事情怎么样?

// find the transformer from the selected topic
MATCH (topic3:Topic {name: "topic-3"})<-[produces]-(transformer:Transformer)

// find the path(s) back from the transformer to the ingest
MATCH p=(transformer)-[:produces|requires*]-(i:Ingest)

// put the names in a collection from topic3 back to ingest
WITH reduce(chain = [topic3.name], n in nodes(p) | chain + n.name) as chain

// return the collection in the desired order
RETURN reverse(chain)

它也可以简化为

MATCH p=(topic3:Topic {name: "topic-3"})-[:produces|requires*]-(i:Ingest)
WITH reduce(chain = [topic3.name], n in nodes(p) | chain + n.name) as chain
RETURN reverse(chain)