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]})

 hive数据迁移到neo4j操作

1. 下载与hive版本一致的jar包,如(hadoop-common-2.7.3.jar,hive-exec-1.2.1.jar,hive-jdbc-1.2.1.jar,hive-metastore-1.2.1.jar,hive-service-1.2.1.jar,httpclient-4.4.jar,httpcore-4.4.jar,libfb303-0.9.2.jar,libthrift-0.9.3.jar)

2. 将jar包放入到plugin 目录中

3. 重启neo4j,./neo4j restart
4. 加载驱动,call apoc.load.driver('org.apache.hive.jdbc.HiveDriver')
5. 执行查询 call apoc.load.jdbc('jdbc:hive2://ip:1000/database;user=hdfs;password=hdfs','select name,age from user')  YIELD row
RETURN row.name, row.age;

注意

.1.hive 查询的时候需要将字段查出来,例如:select name,age from user ,如果使用 select * from user 则返回的数据为null
2.hive 表中数据若是太大可能需要查询很久才能,会将数据全部查询出来才才开始进行图的构建。

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值