【知识图谱构建】从Mysql读取数据批量导入到Neo4j图数据库中

一 连接Mysql数据库,读取数据


ReadMysql2.py 代码如下:

注意:填写自己的数据库名字和密码!!!

# -*- coding: utf-8 -*-
"""
Created on  2020/3/21
@author: GaoRongxuan
"""

import pymysql

def read_mysql(sql):
    '''
       从mysql数据库中读取数据
       :param sql: sql查询语句
       :return: rows 查询结果
       '''
    #打开数据库连接
    dbconn = pymysql.connect(
        host="localhost",
        database=' ',
        user='root',
        password=' '
    )

    #创建游标对象
    cur=dbconn.cursor()
    '''
        sql = 'select * from graph'
    '''
    #
    # 执行sql语句
    cur.execute(sql)
    # 获取查询内容
    rows = cur.fetchall()
    print(rows)

    # 关闭数据库连接
    cur.close()
    dbconn.close()
    return rows
'''
sql = 'select * from graph'
read_mysql(sql)
'''

二 将数据导入到Neo4j中

这是我的数据库哦,请按照自己的实体、属性修改!!!
【知识图谱构建】从Mysql读取数据批量导入到Neo4j图数据库中_第1张图片

# -*- coding:utf-8 -*-
# 从MySQL数据库中的types节点导入到neo4j
from py2neo import Graph, Node, Relationship
from ReadMysql2 import read_mysql

def get_nodes():
   name_type = dict()
   
   rows = read_mysql("SELECT distinct subject, subject_type FROM graph")

   for row in rows:
      subject = process_string(row[0])
      subject_type = process_string(row[1])
      name_type[subject] = subject_type 

   rows = read_mysql("SELECT distinct value, value_type FROM graph")
   
   for row in rows:
      subject = process_string(row[0])
      subject_type = process_string(row[1])
      name_type[subject] = subject_type 

   return name_type

def process_string(s):
    if s is None:
        return ""
    return  s.replace("-", "").replace("…", "").replace(" ", "");

# 连接Neo4j

graph = Graph('bolt://localhost', username='neo4j', password='123456')
# 清除节点
graph.delete_all()

########################## 插入节点到Neo4j中 ##################################
node_name_type = get_nodes()
nodes = dict()
     
for (node_name, node_type) in  node_name_type.items(): 
   node = Node(node_type, name=node_name)
   nodes[node_name] = node
   # print ke_node
   graph.create(node)

 
triples = read_mysql("SELECT subject, property, value FROM graph")

for triple in triples:
    # print network_row
    # 获取节点
    subject_node = nodes[process_string(triple[0])]
    object_node = nodes[process_string(triple[2])]
    relation_type = process_string(triple[1])
    properties = dict()
    
    print (subject_node)
    print (relation_type)
    print (object_node)
  
    print ("")
    # 关系创建
    subject_relation_object = Relationship(subject_node, relation_type, object_node, **properties)
    graph.create(subject_relation_object)

print ('关系创建完成')

成功构建如下:
【知识图谱构建】从Mysql读取数据批量导入到Neo4j图数据库中_第2张图片

你可能感兴趣的:(知识图谱与Neo4j图数据库,知识图谱)