Monica Heddneck
Monica Heddneck

Reputation: 3135

Concatenating nodes from a query into a single line for export to csv in Neo4J using Cypher

I have a neo4J graph that represents a chess tournament.

Say I run this:

MATCH (c:ChessMatch {m_id: 1"})-[:PLAYED]-(p:Player) RETURN *

This gives me the results of the two players who played in a chess match.

The graph looks like this:

enter image description here

And the properties are something like this:

|--------------|------------------|
| (ChessMatch) |                  |
| m_id         | 1                |
| date         | 1969-05-02       |
| comments     | epic battle      |
|--------------|------------------|
| (player)     |                  |
| p_id         | 1                |
| name         | Roy Lopez        |
|--------------|------------------|
| (player)     |                  |
| p_id         | 2                |
| name         | Aron Nimzowitsch |
|--------------|------------------|

I'd like to export this data to a csv, which would look like this:

| m_id | date       | comments    | p_id_A | name_A    | p_id_B | name_B           |
|------|------------|-------------|--------|-----------|--------|------------------|
| 1    | 1969-05-02 | epic battle | 1      | Roy Lopez | 2      | Aron Nimzowitsch |

Googling around, surprisingly, I didn't find any solid answers. The best I could think of is so just use py2neo and pull down all the data as separate tables and merge in Pandas, but this seems uninspiring. Any ideas on how to do in cypher would be greatly illuminating.

Upvotes: 1

Views: 60

Answers (1)

Tom Geudens
Tom Geudens

Reputation: 2666

APOC has a procedure for that :

apoc.export.csv.query 

Check https://neo4j-contrib.github.io/neo4j-apoc-procedures/index32.html#_export_import for more details. Note that you'll have to add the following to neo4j.conf :

apoc.export.file.enabled=true

Hope this helps.

Regards, Tom

Upvotes: 1

Related Questions