当前位置:   article > 正文

代码实例 | python链接neo4j导入数据实例_python导入数据到neo4j

python导入数据到neo4j
数据遍历导入neo4j
import json
from py2neo import Graph, Node, Relationship 

## py2neo连接neo4j导入数据
class ImportData:
    def __init__(self):
        self.input_path = './文档-关键词/map.json'
        
    def load_data(self, path):
        ## 读json文档数据
        f_input = open(path, 'r', encoding='utf-8', errors='ignore')
        data = json.loads(f_input.read())["data"]
        f_input.close()
        return data
    
    def link_import(self, data):
        """链接neo4j,导入数据
        """
        # 连接图库
        # graph = Graph("http://192.168.0.21:12273", username="neo4j", password='test')
        graph = Graph("http://localhost:7474", username="neo4j", password='123')

        for content in data:
            keywords = content["keywords"] # 文档关键词
            name = content["name"] # 文档名
            labels = content["labels"][0]  # 文档类型,取labels字段中的第一个元素
            
#             # 查询文档*节点是否存在,不存在则create创建
#             m = 'match (n{name:"'+name+'"}) return n'
#             g_m = graph.run(m).data()
#             if len(g_m) == 0:
#                 m1 = 'merge (n:'+labels+'{name:"'+name+'"}) return n'
#                 g_m1 = graph.run(m1)
#             for key in keywords:
#                 # 查询关键词*节点是否存在,不存在则create创建
#                 m2 = 'match (n{name:"'+key+'"}) return n'
#                 g_m2 = graph.run(m2).data()
#                 if len(g_m2) == 0:
#                     m3 = 'merge (n:文档关键词{name:"'+key+'"}) return n'
#                     g_m3 = graph.run(m3)
#                 # 查询(文档*关键词)关系是否存在,不存在则create创建
#                 r1 = 'match (n{name:"'+name+'"}),(m{name:"'+key+'"}) match (n)-[r:包含]->(m) return r'
#                 g_r1 = graph.run(r1).data()
#                 if len(g_r1) == 0:
#                     r2 = 'match (n{name:"'+name+'"}),(m{name:"'+key+'"}) create (n)-[r:包含]->(m) return n, r, m'
#                     g_r2 = graph.run(r2)

            print(keywords, name, labels)
if __name__=='__main__':
    ImportData = ImportData()
    input_path = ImportData.input_path
    data = ImportData.load_data(input_path)
	#print(data)
    res = ImportData.link_import(data)
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/161243
推荐阅读
相关标签
  

闽ICP备14008679号