neo4j中导入csv格式的三元组数据

csv数据格式:

cpp 复制代码
head_entity,relation,tail_entity
02.02类以外的脂肪乳化制品,包括混合的和(或)调味的脂肪乳化制品,允许添加,β-胡萝卜素
02.02类以外的脂肪乳化制品,包括混合的和(或)调味的脂肪乳化制品,允许添加,丁基羟基茴香醚
02.02类以外的脂肪乳化制品,包括混合的和(或)调味的脂肪乳化制品,允许添加,丙二醇脂肪酸酯
02.02类以外的脂肪乳化制品,包括混合的和(或)调味的脂肪乳化制品,允许添加,二丁基羟基甲苯
02.02类以外的脂肪乳化制品,包括混合的和(或)调味的脂肪乳化制品,允许添加,双乙酰酒石酸单双甘油酯
02.02类以外的脂肪乳化制品,包括混合的和(或)调味的脂肪乳化制品,允许添加,司盘类[包括山梨醇酐单月桂酸酯

python代码

cpp 复制代码
from neo4j import GraphDatabase
import pandas as pd

# 1. 读取三元组数据
csv_path = "aaa.csv" # cvs文件路径
df = pd.read_csv(csv_path)

# 2. 连接 Neo4j
uri = "bolt://localhost:7687"  # 本地部署
username = "neo4j"
password = "****"  # ⚠️ 替换为你自己的neo4j密码

driver = GraphDatabase.driver(uri, auth=(username, password))

# 3. 执行 Cypher 创建节点和关系
def create_graph(tx, head, relation, tail):
    query = (
        "MERGE (h:Entity {name: $head}) "
        "MERGE (t:Entity {name: $tail}) "
        "MERGE (h)-[r:`" + relation + "`]->(t)"
    )
    tx.run(query, head=head, tail=tail)

with driver.session() as session:
    for idx, row in df.iterrows():
        head = str(row["head_entity"])
        relation = str(row["relation"])
        tail = str(row["tail_entity"])
        session.write_transaction(create_graph, head, relation, tail)

driver.close()
print("数据导入完成 ✅")
相关推荐
AustinCyy9 小时前
【环境配置】Neo4j Community Windows 安装教程
windows·neo4j
萧鼎9 小时前
深度探索 Py2neo:用 Python 玩转图数据库 Neo4j
数据库·python·neo4j
背太阳的牧羊人9 小时前
Cypher 是 Neo4j 专用的查询语言
neo4j
lishaoan7714 小时前
使用tensorflow的线性回归的例子(九)
tensorflow·线性回归·neo4j
背太阳的牧羊人14 小时前
Neo4j 的向量搜索(Neo4jVector)和常见的向量数据库(比如 Milvus、Qdrant)之间的区别与联系
数据库·neo4j·milvus
隆里卡那唔1 天前
在dify中通过http请求neo4j时为什么需要将localhost变为host.docker.internal
http·docker·neo4j
疯子的模样1 天前
Docker 安装 Neo4j 保姆级教程
docker·容器·neo4j
晋丑丑12 天前
从 0 到 1 构建 Graph RAG 系统:本地图谱 + 通义千问落地实践
前端·后端·python·neo4j
Lightning_201713 天前
Neo4j.5.X社区版创建数据库和切换数据库
数据库·oracle·neo4j
南城尽相思14 天前
Neo4j常见语句-merge
neo4j