neo4j在线增量批量插入三元组
程序员文章站
2024-01-16 11:00:04
...
1.python代码生成words.csv、relations_daixiang.csv relations_yongxiang.csv relations_xiangguan.csv 注意:每种关系单独导入!
# 生成map{word:id}
map = {} # 词和他的id对应词典 注意id不要为数字 不然会错乱!!!
for tuplei in tuples:
if not map.get(tuplei[0], 0):
map[tuplei[0]] = tuplei[0]
if not map.get(tuplei[1], 0):
map[tuplei[1]] = tuplei[1]
logger.debug('map{word:id}已生成')
# 生成 words[[name]...]和cwords.csv
words = []
for key in map.keys():
words.append([map.get(key)])
logger.debug('words[[name]...]已生成')
with open("cwords.csv", "w", newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["name"]) # 先写入columns_name
writer.writerows(words) # 写入多行用writerows
logger.debug('cwords.csv已生成')
# 生成 relations[[from_name, type, to_name]...]和crelations.csv
relations_daixiang = []
relations_yongxiang = []
relations_xiangguan = []
for tuplei in tuples:
if tuplei[2] == "代项":
relations_daixiang.append([tuplei[0], tuplei[2], tuplei[1]])
if tuplei[2] == "用项":
relations_yongxiang.append([tuplei[0], tuplei[2], tuplei[1]])
if tuplei[2] == "相关":
relations_xiangguan.append([tuplei[0], tuplei[2], tuplei[1]])
logger.debug('relations[[from_name, type, to_name]...]已生成')
with open("crelations_daixiang.csv", "w", newline='', encoding='utf-8') as csvfile1:
writer = csv.writer(csvfile1)
writer.writerow(["from_name", "type", "to_name"])
writer.writerows(relations_daixiang)
with open("crelations_yongxiang.csv", "w", newline='', encoding='utf-8') as csvfile2:
writer = csv.writer(csvfile2)
writer.writerow(["from_name", "type", "to_name"])
writer.writerows(relations_yongxiang)
with open("crelations_xiangguan.csv", "w", newline='', encoding='utf-8') as csvfile3:
writer = csv.writer(csvfile3)
writer.writerow(["from_name", "type", "to_name"])
writer.writerows(relations_xiangguan)
logger.debug('crelations.csv已生成')
2.插入节点
LOAD CSV WITH HEADERS FROM "file:///cwords.csv" AS line
MERGE (w:word{name:line.name})
3.插入关系
LOAD CSV WITH HEADERS FROM "file:///crelations_daixiang.csv" AS line
match (a:word{name:line.from_name}),(b:word{name:line.to_name})
merge (a)-[r:代项]->(b)
LOAD CSV WITH HEADERS FROM "file:///crelations_yongxiang.csv" AS line
match (a:word{name:line.from_name}),(b:word{name:line.to_name})
merge (a)-[r:用项]->(b)
LOAD CSV WITH HEADERS FROM "file:///crelations_xiangguan.csv" AS line
match (a:word{name:line.from_name}),(b:word{name:line.to_name})
merge (a)-[r:相关]->(b)
推荐阅读