Build a pubsub graph from a CSV file

I have a CSV file without headers containing three columns in a one to many relationship, namely
publisher, topic, subscriber

What is the neo4j query to load the CSV and draw the graph showing the relationship between publishers, topics and subscribers.

Pls note that

  1. a topic can only be produced to by one producer.
  2. a topic can be consumed by more than one consumer.

I already made a start but need help.

LOAD CSV FROM 'file:///path/pubsub.tsv' AS line
MERGE (topic:Topic {name: line[1]})
CREATE (:Producer {producer: line[0], topic: line[1]})
CREATE (producer)-[:PUBLISH]->(topic)
CREATE (:Consumer {consumer: line[2], topic: line[1]})
CREATE (consumer)-[:SUBSCRIBE]->(topic)

Thanks

Try this.

I removed the β€˜topic’ properties from the producer and consumer entities. That information is represented by the relationship to the corresponding topic, as such it should not be saved in the producer or consumer nodes.

LOAD CSV FROM 'file:///path/pubsub.tsv' AS line
MERGE (producer:Producer {name: line[0]})
MERGE (topic:Topic {name: line[1]})
MERGE (consumer:Consumer {name: line[2]})
MERGE (producer)-[:PUBLISH]->(topic)
MERGE (consumer)-[:SUBSCRIBE]->(topic)

Thanks @glilienfield
How do I get it to display the graph pls?
I put RETURN producer, topic, consumer at the end but it is still not displaying the graph with relationship. It only shows the nodes.

It looks like there are zero relationships in your database. Can you send me the csv, or is it proprietary?

1 Like

Thanks @glilienfield

Here is a sample CSV content with mock data.

producer-A,topic-A,consumer-A
producer-A,topic-A,consumer-B
producer-A,topic-A,consumer-C
producer-B,topic-B,consumer-D
producer-B,topic-B,consumer-E
producer-C,topic-C,consumer-A
producer-C,topic-C,consumer-C
producer-D,topic-D,consumer-E
producer-D,topic-D,consumer-C

@glilienfield It is working now. I think the problem was with the neo4j interpreter in my zeppelin notebook as same script works in neo4j desktop for MacOS.
Thanks for your assistance

1 Like