前提:這個方法是用python連接neo4j再循環(huán)執(zhí)行cql語句來實現(xiàn)的,適合1w條記錄以內(nèi)的數(shù)據(jù),太大了就比較慢,平均1s執(zhí)行30條語句左右。
主要的不同就在于cql語句的使用
準(zhǔn)備工作
連接數(shù)據(jù)庫:
from py2neo import Graph
import pandas as pd
graph=Graph('http://localhost:7474',auth=('neo4j','密碼'))
讀取節(jié)點表和關(guān)系表,該文件是存儲好的csv文件
node_df=pd.read_csv('./neo4j_test/節(jié)點表.csv')
edge_df=pd.read_csv('./neo4j_test/關(guān)系表.csv')
中文是替代的文件名字,不是源文件名文章來源:http://www.zghlxwxcb.cn/news/detail-576084.html
追加屬性
追加節(jié)點屬性
for i in range(len(node_df)):
id=node_df['id'][i] # 唯一標(biāo)識id
age=node_df['age'][i] # 新增的屬性和值
cql=f"match (a:Person{{ID:{id}}}) set a.age={age}" # 新增了年齡屬性
graph.run(cql)
追加關(guān)系屬性
替換cql為對應(yīng)的語句:文章來源地址http://www.zghlxwxcb.cn/news/detail-576084.html
f"match (a:Person{{ID:{id}}})-[r:friends]->(b:Person{{ID:{id}}}) set r.year={year}"
追加記錄
批量追加節(jié)點
for i in range(len(node_df)):
code=node_df['ecode'][i]
name=node_df['ename'][i]
cap=node_df['ecap'][i]
province=node_df['province'][i]
cql=f"merge (a:Company{{code:{code},name:'{name}',cap:'{cap}',province:'{province}'}})"
graph.run(cql)
批量追加關(guān)系
for i in range(len(edge_df)):
scode=edge_df['scode'][i]
ecode=edge_df['ecode'][i]
pct=edge_df['pct'][i]
year=edge_df['notice_year'][i]
relation=edge_df['relation'][i]
cql=f"match (s:Company{{code:{scode}}}) match (e:Company{{code:{ecode}}}) merge (s)-[r:Supply{{pct:'{pct}',year:'{year}',relation:'{relation}'}}]->(e) "
graph.run(cql)
到了這里,關(guān)于neo4j批量追加屬性、節(jié)點、關(guān)系的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!