0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CSVファイルから一発で親ノードと子ノードを作成しリレーションを張るcypherクエリ

0
Last updated at Posted at 2023-10-11

はじめに

こんにちは。ponponnsanです。最近neo4jでグラフデータベースを触る機会があり、cypherクエリを作成する機会があったので、書いておきます。

Neo4j Desktop

CSVファイルから親ノードと子ノードを作成しリレーションを張る

CSVファイルはローカルの

C:\Users\user_name\.Neo4jDesktop\relate-data\dbmss\dbms-12345678\import

に入っているものとします。

LOAD CSV WITH HEADERS FROM 'file:///example.csv' AS row
WITH row
LIMIT 300
MERGE (parent:FileName {name: row.file_name, label: row.labels})
MERGE (child:path  {URL: row.url, label: row.labels})
MERGE (parent)-[:PATH {path: row.path}]->(child)

python経由

def send_urls(csv_file_path):
    cypher_query = """
    WITH $data AS data
    UNWIND data AS row
    MERGE (parent:FileName {name: row.file_name, label: row.labels})
    MERGE (child:path  {URL: row.url, label: row.labels})
    MERGE (parent)-[:PATH {path: row.path}]->(child)
    """

    with open(csv_file_path, mode="r", encoding="utf-8") as file:
        csv_reader = csv.DictReader(file)
        with driver.session() as session:
            for row in csv_reader:
                print(row)
                session.run(cypher_query, data=row)

ほかの役立つ記事

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?