exporting data from Neo4j to csv using java

后端 未结 2 507
半阙折子戏
半阙折子戏 2021-01-07 15:33

what is the best methodology to export data from Neo4j to CSV, I had imported data from csv to neo4j using CSV importer in the link https://github.com/sroycode/neo4j-import.

相关标签:
2条回答
  • 2021-01-07 15:46

    A short snippet in Groovy doing 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 be used in a pure Java environment as well.

    0 讨论(0)
  • 2021-01-07 16:02

    As you already have the ids, you can get the ids and their fwd relationships in chunks (of 5) using cypher, the csv separator is '|'

    To test use 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 thing in the Java API , just run the same in a for loop.

    0 讨论(0)
提交回复
热议问题