自学内容网 自学内容网

python实现rdbms和neo4j的转换

一、连接neo4j

  • 下载依赖库
pip install py2neo
  • 连接neo4j
from py2neo import Graph
graph = Graph("bolt://localhost:7687", auth=("neo4j","neo4j"))

二、rdbms转换到neo4j

import pymysql
from py2neo import Graph

def pymain:
#连接Mysql
conn = pymysql.connect(
host="localhost",
user="test",
password="test",
port=3309,
database="test_db"
)
cursor = conn.cursor()
cursor.execute("select deptno,employee from employees")
rows = cursor.fetchall()
for row in rows:
deptno,employee = row
insert_data2neo4j(deptno,employee)

def insert_data2neo4j(deptno,employee):
#连接neo4j
graph = Graph("bolt://localhost:7687",auth=("neo4j","neo4j"))
graph.run(
"MERGE (d:deptno {name: $deptno})"
"MERGE (e:employee {name: $employee})"
"MERGE (e)-[:BELONGS_TO]->(d)",
deptno=deptno,
employee=employee
)

if __name__ == '__main__':
pymain()

三、常见报错

<一>、ValueError: The following settings are not supported

  • 这是因为py2neo版本更新,不支持下述写法
graph = Graph(host="",user="",password="")

应改为

graph = Graph("bolt://localhost:7687", auth=("neo4j","neo4j"))

原文地址:https://blog.csdn.net/nzbing/article/details/136043133

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!