赞
踩
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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。