Export Data to csv files from Neo4j pragmatically

Lina picture Lina · Apr 3, 2017 · Viewed 8.4k times · Source

I need your advice on neo4j export things…. I used ” apoc.export.cypher.query” to export my nodes and relationships to csv files , however the exported contents are cypher statements. This is a sample exported csv file :

begin
CREATE (:`ITEM`:`UNIQUE IMPORT LABEL` {`product_number`:5305, `bi_key`:1910, `mean_review_score`:“4.42”, `review_count`:“24", `site_availability_bitmask`:“2129759”, `UNIQUE IMPORT ID`:117});
CREATE (:`ITEM`:`UNIQUE IMPORT LABEL` {`product_number`:7123, `bi_key`:2261, `UNIQUE IMPORT ID`:121});
CREATE (:`ITEM`:`UNIQUE IMPORT LABEL` {`product_number`:7436, `bi_key`:2330, `mean_review_score`:“4.41", `review_count`:“117”, `site_availability_bitmask`:“2113295", `UNIQUE IMPORT ID`:125});
CREATE (:`ITEM`:`UNIQUE IMPORT LABEL` {`product_number`:7697, `bi_key`:2382, `UNIQUE IMPORT ID`:130});
CREATE (:`ITEM`:`UNIQUE IMPORT LABEL` {`product_number`:7743, `bi_key`:2388, `mean_review_score`:“4.33”, `review_count`:“18", `site_availability_bitmask`:“2113295”, `UNIQUE IMPORT ID`:133});
commit
begin
CREATE INDEX ON :`ITEM`(`product_number`);
CREATE CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT node.`UNIQUE IMPORT ID` IS UNIQUE;
commit
schema await
begin
MATCH (n:`UNIQUE IMPORT LABEL`)  WITH n LIMIT 10 REMOVE n:`UNIQUE IMPORT LABEL` REMOVE n.`UNIQUE IMPORT ID`;
commit
begin
DROP CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT node.`UNIQUE IMPORT ID` IS UNIQUE;
commit

But this is not what I want , is there anyway to export graph content directly to csv files with csv formats? Or I should rely on parsing the file above ... or using neo4j jdbc driver to execute cypher and write the result in csv ? I need to do this pragmatically … I mean it is a web application from which users should be able to extract graph content to csv formats

Answer

William Lyon picture William Lyon · Apr 3, 2017

Instead of using apoc.export.cypher.query you can use apoc.export.csv.query to write the results to a CSV file. For example:

CALL apoc.export.csv.query("MATCH (u:User)-[r:RATED]->(m:Movie) RETURN u.name, r.rating, m.title LIMIT 10", "results.csv", {})

More info in the docs here