将数据从 Neo4j 导出到 CSV 的最佳方法是什么,我在链接https://github.com/sroycode/neo4j-import中使用 CSV 导入器将数据从 csv 导入到 neo4j 。
我对数据执行了一些操作,我想将查询结果返回到 csv 中,任何人都可以建议我解决方案。
我正在使用 neo4j 1.9.3 和 java 1.6
将数据从 Neo4j 导出到 CSV 的最佳方法是什么,我在链接https://github.com/sroycode/neo4j-import中使用 CSV 导入器将数据从 csv 导入到 neo4j 。
我对数据执行了一些操作,我想将查询结果返回到 csv 中,任何人都可以建议我解决方案。
我正在使用 neo4j 1.9.3 和 java 1.6
这样做的一个简短片段Groovy
:
@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()
当然opencsv
也可以在纯Java环境中使用。
由于您已经有了 id,您可以使用 cypher 以块(5 个)的形式获取 id 及其 fwd 关系,csv 分隔符为“|”
测试使用 neo4j-shell 语法:neo4j-shell < infile > outfile
节点的 infile 看起来像
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?;
....
reln 的 infile 看起来像
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?;
....
要在 Java API 中执行相同的操作,只需在 for 循环中运行相同的操作。