Export data from Neo4j to csv using java

which is the best methodology for exporting data from Neo4j to CSV, I imported csv data to neo4j using the CSV importer into the link https://github.com/sroycode/neo4j-import .

I performed some operations on the data, I want to return the results of the query to csv, can anyone suggest a solution for me.

iam using neo4j 1.9.3 and java 1.6

0
source share
2 answers

A short snippet in Groovy does this:

 @Grab(group="org.neo4j", module="neo4j-cypher", version="1.9") @Grab(group='net.sf.opencsv', module='opencsv', version='2.3') import org.neo4j.kernel.EmbeddedGraphDatabase import org.neo4j.cypher.javacompat.ExecutionEngine import au.com.bytecode.opencsv.CSVWriter assert args, "specify location of graph.db and cypher statement" def db = new org.neo4j.kernel.EmbeddedGraphDatabase(args[0]) def ee = new ExecutionEngine(db) def result = ee.execute(args[1]) def columns = result.columns() System.out.withWriter { writer -> CSVWriter csv = new CSVWriter(writer) csv.writeNext(columns as String[]) for (def row in result) { def values = columns.collect {row[it]} csv.writeNext(values as String[]) } } db.shutdown() 

Of course, opencsv can opencsv be used in a clean Java environment.

+3
source

Since you already have identifiers, you can get the identifiers and their fwd relationships in pieces (out of 5) with cypher, the csv delimiter is '|'

To test the use of neo4j-shell Syntax: neo4j-shell <infile> outfile

infile for node looks like

 START n=node(1,2,3,4,5) return ID(n),n.name?,n.property?; START n=node(6,7,8,9,10) return ID(n),n.name?,n.property?; .... 

infile for reln looks like

 START n=node(1,2,3,4,5) MATCH n-[r]->m RETURN ID(n),ID(m),TYPE(r),r.someprop?; START n=node(6,7,8,9,10) MATCH n-[r]->m RETURN ID(n),ID(m),TYPE(r),r.someprop?; .... 

To do the same in the Java API, just run it in a for loop.

+1
source

Source: https://habr.com/ru/post/973554/


All Articles