平台:windows10
neo4j下载安装(较为简单,不做赘述)参考:
(https://blog.csdn.net/qq_41510429/article/details/103922515)
(注:neo4j是用Java语言编写的图形数据库,运行时需要启动JVM进程,因此,需安装JAVA SE的JDK,且注意安装版本与所要安装neo4j版本的对应,不然后面启动neo4可能报错)
数据导入:
一、通过csv文件导入:
1、待导入数据文件置于neo4j安装路径下(如下所示):
2、导入实体:
load csv from "file:/index_wh.csv" as line
create(n:IndexWh{id:line[0],name:line[2],code:line[1],displayname:line[3],description:line[4]})
return n
3、导入关系:
LOAD CSV FROM "file:/index_rel.csv" AS row
MATCH (p:IndexWh),(o:IndexWh)
WHERE p.id = row[0] and o.id=row[1]
CREATE (p)-[r:IndexRel{type:row[2]}]->(o);
二、链接RDMS关系数据库导入
前提:
1、apoc 配置
(1)下载对应版本的apoc jar包(版本不对会报错!)
从github apoc下载对应版本的apoc jar包,并放到 $NEO4J_HOME/plugins 目录下,如下所示:
(2)修改 neo4j.conf 配置文件
在 neo4j.conf 最后一行添加
dbms.security.procedures.unrestricted=apoc.*
apoc.import.file.enabled=true
(3)重启neo4j,打开http://localhost:7474/browser/,输入
return apoc.version()
返回apoc版本号,说明配置成功
2、数据库驱动配置(以mysql和postgresql为例):
下载对应驱动(mysql),并放到 $NEO4J_HOME/plugins 目录下
关系数据库连接实现:
1、连接MySql:
(1)连接MySql:
CALL apoc.load.driver(“com.mysql.cj.jdbc.Driver”);
(2)查询:
查看数据个数
with "jdbc:mysql://localhost:3306/gz_db?serverTimezone=GMT&user=root&password=123456" as url
CALL apoc.load.jdbc(url, "planning_class_big") YIELD row
RETURN count(*);
(3)查看数据详情
with "jdbc:mysql://localhost:3306/gz_db?serverTimezone=GMT&user=root&password=123456" as url,
"select id, name,keywords from planning_class_big" as sql
CALL apoc.load.jdbc(url, sql, []) YIELD row
RETURN row LIMIT 10;
(4)导入表数据到neo4j:
CALL apoc.load.jdbc(
'jdbc:mysql://localhost:3306/gz_db?user=root&password=123456&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai',
'select * from planning_class_big'
)YIELD row
CREATE(p:PlanningBig{id:toInteger(row.id),name:row.name,keywords:row.keywords})
2、连接postgresql:
(1)数据库连接
CALL apoc.load.driver("org.postgresql.Driver");
(2)查看数据个数示例(同mysql):
with "jdbc:{jdbcUrl}?serverTimezone=GMT&user={usr}&password={password}" as url
CALL apoc.load.jdbc(url, "tablename") YIELD row
RETURN count(*);
(3)导入数据到neo4j创建实体:
with "jdbc:{jdbcUrl}?serverTimezone=GMT&user={usr}&password={password}" as url
CALL apoc.load.jdbc(url, "topiccatalog") YIELD row
CREATE(t:TopicCatalog{id:toInteger(row.id),name:row.name,type:row.type})
(4)导入数据到neo4j创建关系:
以上图所示为例,content1/2中内容为相关节点id号,rela为节点间关系,则将表中关系导入图数据库语句为:
with "jdbc:{jdbcUrl}?serverTimezone=GMT&user={usr}&password={password}" as url,"select * from table" as sql
CALL apoc.load.jdbc(url, sql,[]) YIELD row
MATCH (p:Label1),(o:Label2)
WHERE p.id = row.content1 and o.id=row.content2
CREATE (p)-[r:RelData{type:row.rela}]->(o);