概述
1、简介
本篇文章演示将Mysql数据导入Neo4j的两种方式:第一种:编写python脚本;第二种:使用Cypher语言的Load CSV功能
2、必要条件
1、安装docker,这里提供安装教程 https://blog.csdn.net/wangyan_z/article/details/95966896?spm=1001.2014.3001.5501
2、docker安装Neo4j,用户名和密码在如下命令里的NEO4J_AUT自定义
docker run -d --name neo4j -p 7474:7474 -p 7687:7687 -v /home/neo4j/data:/data -v /home/neo4j/logs:/logs -v /home/neo4j/conf:/var/lib/neo4j/conf -v /home/neo4j/import:/var/lib/neo4j/import -e NEO4J_AUTH=neo4j/test neo4j
3、Python脚本,以两个实体一个关系为例【简单脚本,本地使用还需修改】
# coding:utf8
# @Time : 2021/4/16 15:09
# @File : mysql2neo4j.py
# @Return: 从mysql 抽取数据到neo4j
import pymysql
import pandas as pd
from neo4j import GraphDatabase
# Neo4j图数据库连接
uri = "neo4j://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "test"))
# Mysql数据库连接
conn = pymysql.connect(host = 'localhost',user = 'root',passwd = 'root',db = 'db',port=3306)
cursor = conn.cursor()
def create_entity_node(tx,tabel_name):
sql = 'select * from {}'.format(tabel_name)
dw = pd.read_sql(sql, conn)
col_name = dw.columns
node_cls = tabel_name
name_d = {}
for inx, row in dw.iterrows():
neo4j_sql = "CREATE (n:%s { " % node_cls
for p_name in col_name:
name_d[p_name] = row[p_name]
neo4j_sql = neo4j_sql + p_name + ":" + "'" + row[p_name] + "',"
neo4j_sql = neo4j_sql[:-1] + "}) RETURN n"
print(neo4j_sql)
# 逐行写入
tx.run(neo4j_sql)
def create_relation(tx, head_table, relation_table, tail_table):
create_entity_node(tx, head_table)
create_entity_node(tx, tail_table)
sql = 'select * from {}'.format(relation_table)
dw = pd.read_sql(sql, conn)
col_name = dw.columns
begin_node_cls = head_table
#实体表映射到关系表的字段
begin_relation_line = '单位编码'
end_relation_line = '规划编号'
end_node_cls = tail_table
relation_name = relation_table
for inx,row in dw.iterrows():
neo4j_sql = "MATCH (a: %s { %s:" % (begin_node_cls, begin_relation_line)
neo4j_sql += "'" + row[begin_node_cls] + "' }), (b: %s { %s:" % (end_node_cls, end_relation_line)
neo4j_sql += "'" + row[end_node_cls] + "'}) "
neo4j_sql += "MERGE (a)-[:%s {" % relation_name
for p_name in col_name:
if row[p_name]!=None:
neo4j_sql = neo4j_sql + p_name + ":" +"'" + row[p_name] + "',"
neo4j_sql = neo4j_sql[:-1] + "}] ->(b)"
print(neo4j_sql)
# 逐行写入
tx.run(neo4j_sql)
# example
with driver.session() as session:
session.write_transaction(create_relation, '单位', "拟制五年规划", '五年规划') #这里传参为create_relation方法的三个参数
driver.close()
4、使用Cypher语言的Load CSV功能
首先,需要进入Neo4j容器【命令:docker exec -it 容器id bash】找到conf/neo4j.conf文件,在里面添加配置dbms.directories.import=import【使得在import目录下上传的csv文件有效】
其次,从Mysql数据库导出表数据【CSV格式】,上传至Neo4j容器的import路径下
最后,在Neo4j的浏览器端【http://localhost:7474/browser/】登录之后,执行CSV命令
// example
LOAD CSV FROM "file:///actors.csv" AS line CREATE(a:actors{id:line.personId,name:line.name,type:line.type}) //line.表头字段
最后
以上就是结实发箍为你收集整理的将Mysql数据导入Neo4j的两种方式的全部内容,希望文章能够帮你解决将Mysql数据导入Neo4j的两种方式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复