cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Connected graphs from a dual relationship

I have a csv file containing producers and consumers of some topics. e.g
Producer,Topic,Consumer
PRODUCER-1,TOPIC-A,CONSUMER-1
PRODUCER-2,TOPIC-B,CONSUMER-2
CONSUMER-2,TOPIC-F,CONSUMER-5

It's loaded in zeppelin notebook as a dataframe and accessed via neo4j context via neo4j interpreter.
val pubSubDf = spark.read.option("header", "true") // Use first line of all files as header
.format("com.databricks.spark.csv")
.schema(topicSchema)
.load("/data/pubsub.tsv")

val consumer = ("Consumer", Seq("Consumer")) // Consumer node, with Consumer property
val consumes = ("Consumes", Seq.empty)
val topic = ("Topic", Seq("Topic")) // Topic node, with Topic property
val producer = ("Producer", Seq("Producer"))
val produces = ("Produces", Seq.empty)
Neo4jDataFrame.mergeEdgeList(sc, pubSubDf, consumer, consumes, topic)
Neo4jDataFrame.mergeEdgeList(sc, pubSubDf, producer, produces, topic)

MATCH (consumer:Consumer{Consumer: '${Consumer}'})-[consumes:Consumes]->(topic:Topic), (producer:Producer)-[produces:Produces]->(topic) RETURN consumer, consumes, topic, producer, produces

How do I capture a relationship where a consumer is also a producer to another topic pls? I want one graph that includes all nodes including TOPIC-B<--CONSUMER-2--->TOPIC-F . At the moment, CONSUMER-2 is dangling.
Thanks

7 REPLIES 7

ameyasoft
Graph Maven
Try this:

MATCH (consumer:Consumer{Consumer: '${Consumer}'})-[]->(topic:Topic), (producer:Producer)-[produces:Produces]->(topic) 
RETURN consumer, topic, producer

No, that didn't work

Try adding a WHERE condition:
WHERE producer.Producer = consumer.Consumer

Thanks @ameyasoft. That didn't work either.

I think this should work but I am not getting any output

MATCH (consumer:Consumer)-[consumes:Consumes]->(topic:Topic),
 (producer:Producer)-[produces:Produces]->(topic)
MATCH (producer:Producer),(consumer:Consumer)
WHERE producer.Producer = consumer.Consumer and id(producer) < id(consumer)
WITH [producer,consumer] as ns
CALL apoc.refactor.mergeNodes(ns) YIELD node
RETURN node

Any clue as to what I am doing wrong please?

You're going to have to change your model a bit.

Whether a node is a producer or a consumer is a function of its action (relationships) between other nodes, it's not necessarily inherent in the node itself.

So instead of using :Consumer and :Producer labeled nodes, think about what kind of label makes sense for both, and use that instead. :Entity? :Customer? :Process? Figure that out, then use that for the label. Likewise, use a more general property name for the identifier, like name, id, or similar. That way, regardless of whether you're creating a :PRODUCES or a :CONSUMES relationship from the node, its label doesn't change, so you can freely address and match to that node in either case.

Do I really have to change the model?
In order to make what I am trying to achieve clearer, I am sharing a mock image of the desired relationship.
Thanks