Dee
Dee

Reputation: 233

Is there a way to download neo4j result as a csv

I am using ne04j community version 1.9.8. I created a query that gives the result in a table format. It has 5 columns (c1-c5) and each column's data will need to be created as a different csv file. Is there any way command that I can use to parse the result into csvs?

I have seen the csv import to neo4j but I need the exact opposite way here. Thanks for your help.

[Edited]

Here is my original query:

START survey=node(1111111)
MATCH surveyQ-[:lastStep]->surveyQStep-[:surveys]->survey-[:questionpanel]->qstn-[:se‌​ctions]->s-[:questions]->q-[:answers]->answers
RETURN ID(survey) as surveyID, ID(qstn) as questionpanelID, ID(s) as sectionID, ID(q) as questionID, ID(answers) as answerID

Upvotes: 1

Views: 250

Answers (2)

cybersam
cybersam

Reputation: 66999

[EDITED]

This answer only applies to newer versions of neo4j that support the WHEN and CASE clauses, not 1.9.8. If upgrading to, say, 2.1.5 is not possible, it should be possible to install a separate instance of neo4j 2.1.5, copy the data over to the new instance, and use this answer on the new instance.

You can use Cypher to generate a comma-delimited result for each column, one at a time. You can then copy the result for each column into a separate file.

Let's suppose your original query ended in RETURN a, b, c, d, e;

To get the a comma-delimited value for all the values in the fourth column (i.e., variable d), you can replace your RETURN clause with the following:

WITH COLLECT(d) AS v
RETURN reduce(s = "", x IN v | s + (CASE WHEN s = "" THEN "" ELSE "," END ) + x);

Based your original query, this is how you'd get the CSV data for the first column:

START survey=node(1111111)
MATCH surveyQ-[:lastStep]->surveyQStep-[:surveys]->survey-[:questionpanel]->qstn-[:se‌​ctions]->s-[:questions]->q-[:answers]->answers
WITH COLLECT(ID(survey)) AS surveyID
RETURN reduce(s = "", x IN surveyID | s + (CASE WHEN s = "" THEN "" ELSE "," END ) + x);

Upvotes: 1

Michael Hunger
Michael Hunger

Reputation: 41676

You can also download the data from a query in the neo4j-browser as CSV if you're in the tabular view and hit the small download icon on the top-right within that table-frame.

Otherwise, if a command-line tool is better suited for you,

check out the -output option of the cypher-import command of the neo4j-shell-tools: https://github.com/jexp/neo4j-shell-tools#cypher-import

Upvotes: 1

Related Questions