NEO4J 图数据库使用APOC数据导入
Neo4j 数据导入
一、安装与部署
直接在官网下载安装包安装,解压即可。
二、下载相应的jar包
apoc 包下载链接: https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases
1.sqlserver 数据导入neo4j的jar包
apoc-3.4.0.1-all.jar mssql-jdbc-6.2.2.jre8.jar sqljdbc4-4.0.jar
2.mysql 数据导入neo4j的jar包
apoc-3.3.0.1-all.jar mysql-connector-java-8.0.8-dmr.jar
3.将对应jar包放在安装目录plugins文件目录里,然后conf目录里的neo4j.conf的后面加上
- dbms.security.procedures.unrestricted=apoc.*
-
- apoc.import.file.enabled=true
apoc.export.file.enabled=true
4.restart neo4j,运行return apoc.version(),若有版本号,则成功。
三、导数据
- import org.neo4j.driver.v1.*;
- public class Connect{
- public static void main(String[] args){
- Driver driver = GraphDatabase.driver("bolt://localhost:7687",AuthTokens.basic("neo4j","neo4j"));
- Session session = driver.session();
- String cypher="create constraint on (n:ITEM) ASSERT n.itemid is unique"; //创建唯一索引,这样可以更快的导入数据
- Session.run(cypher);
- cypher="CALL apoc.periodic.iterate(\"CALL apoc.load.jdbc('jdbc:sqlserver://localhost;username=name;password=word;database=db;characterEncoding=utf-8',\\\"SELECT * FROM TABLE1\\\")\",\"MERGE(n:ITEM{itemid:row.mitemid}) with * MERGE(m:ITEM{itemid:row.itemid}) with * create p=(n)-[r:rel{rels:row.rels}]->(m)\",{batchSize:10000,iterateList:true})"; //连接sqlserve数据库和设计创建neo4j图数据库数据模型
- Session.run(cypher);
- session.close();
- driver.close();
- }
- }
mysql数据库类似,不再赘述。
补充:1.使用neo4j-import导入数据的命令
neo4j-admin import --nodes:item "nodes.csv" --relationships:rel "rel_header.csv,rel.csv" --ignore-missing-nodes
2.apoc 导出命令
call apoc.export.cypher.query( "MATCH (p1:Person)-[r:KNOWS]->(p2:Person) RETURN p1,r,p2", "/tmp/friendships.cypher", {format:'plain',cypherFormat:'updateStructure'})`参考: http://neo4j-contrib.github.io/neo4j-apoc-procedures/#_export_import
call apoc.export.cypher.query("match (n:lable) where not (n)--() and n.properties = '400' return distinct(n)","C://User/Desktop/test",{format:'plain',cypherFormat:'create'})
3.不用解压也能导数据load csv
load csv from "file:/twitter-2010.txt.gz" as line fieldterminator ' ' with toInt(line[0]) as id,toInt(line[1]) as id1 return id,id1 limit 10
using periodic commit 1000 load csv from "file:/twitter-2010.txt.gz" as line fieldterminator ' ' create (item:ITEM{id:line[0],item:line[1]})
原文地址:https://blog.csdn.net/FFFSSSFFF6/article/details/80711202