Neo4J入门笔记[2]---导出数据为CSV
Posted 大象无形,大音希声
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Neo4J入门笔记[2]---导出数据为CSV相关的知识,希望对你有一定的参考价值。
在上一节《Neo4J入门笔记[1]—安装以及Cypher基本语法》 笔者主要分享了Neo4J的安装已经常用的命令。这个章节笔者分享从Neo4j里面导出数据到CSV。一个简单的方法,就是调用下面的存储过程。
CALL apoc.export.csv.all("database-all-data.csv", )
但是如果没有对Neo4j的默认配置进行修改的话,其会报出下面的错误。
Failed to invoke procedure `apoc.export.csv.all`: Caused by: java.lang.RuntimeException: Export to files not enabled, please set apoc.export.file.enabled=true in your apoc.conf
解决方法就是,在apoc.conf的配置文件里面加入下面的配置,并重新启动
apoc.export.file.enabled=true
再次执行 CALL apoc.export.csv.all("database-all-data.csv", )
,见下图,执行成功。
导出的文件在Neo4j的根目录下import文件夹能找见,比如笔者的电脑: D:\\software\\26.neo4j\\neo4j-community-4.4.12\\import
下面是导出的数据:
如果要查询特定的数据集,比如下面的数据集:
CREATE (TheMatrix:Movie title:'The Matrix', released:1999, tagline:'Welcome to the Real World')
CREATE (Keanu:Person name:'Keanu Reeves', born:1964)
CREATE (Carrie:Person name:'Carrie-Anne Moss', born:1967)
CREATE (Laurence:Person name:'Laurence Fishburne', born:1961)
CREATE (Hugo:Person name:'Hugo Weaving', born:1960)
CREATE (LillyW:Person name:'Lilly Wachowski', born:1967)
CREATE (LanaW:Person name:'Lana Wachowski', born:1965)
CREATE (JoelS:Person name:'Joel Silver', born:1952)
CREATE
(Keanu)-[:ACTED_IN roles:['Neo']]->(TheMatrix),
(Carrie)-[:ACTED_IN roles:['Trinity']]->(TheMatrix),
(Laurence)-[:ACTED_IN roles:['Morpheus']]->(TheMatrix),
(Hugo)-[:ACTED_IN roles:['Agent Smith']]->(TheMatrix),
(LillyW)-[:DIRECTED]->(TheMatrix),
(LanaW)-[:DIRECTED]->(TheMatrix),
(JoelS)-[:PRODUCED]->(TheMatrix);
查询关系
MATCH (person:Person)-[actedIn:ACTED_IN]->(movie:Movie)
return *
如何把上面的关系导出到CSV文件呢?
MATCH (person:Person)-[actedIn:ACTED_IN]->(movie:Movie)
WITH collect(DISTINCT person) AS people, collect(DISTINCT movie) AS movies, collect(actedIn) AS actedInRels
CALL apoc.export.csv.data(people + movies, actedInRels, "movies-actedIn.csv", )
YIELD file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
RETURN file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
导出后,CSV文件如下:
参考文档
https://neo4j.com/labs/apoc/4.1/export/csv/
https://neo4j.com/blog/export-csv-from-neo4j-curl-cypher-jq/#:~:text=Here%E2%80%99s%20how%20you%20do%20it%3A%20You%20can%20already,of%20a%20Cypher%20query%20to%20a%20CSV%20file.
以上是关于Neo4J入门笔记[2]---导出数据为CSV的主要内容,如果未能解决你的问题,请参考以下文章
Neo4J入门笔记[2]---Neo4J GDS 图数据科学库