cancel
Showing results for 
Search instead for 
Did you mean: 

Load CSV into Neo4j and create nodes out of each row

steve5
Node

I have a CSV source and I want to build 2 things:

  1. Load the CSV to my Neo4j graph and turn every row to a node.
  2. Build relationship between nodes based on a cosine similarity(above some threshold alpha).

Here is what I have already done for (1):

WITH "https://drive.google.com/u/0/uc?id=1FRi5YmWNQJZ2xeTKNO4b12xYGOTWk1jL&export=download" AS data_url

LOAD CSV WITH HEADERS FROM data_url AS requests

But it returns me an error "Query cannot conclude with LOAD CSV (must be RETURN or an update clause)"

Should I transform the data to be in a long format (with data_key and data_value columns) and use the following:

// For each request (request_id), collect it's attributes into key-value pairs
WITH requests.request_id AS request_id,
    COLLECT([requests.data_key, requests.data_value]) AS keyValuePairs
WITH request_id,
     apoc.map.fromPairs(keyValuePairs) AS map

// Each request converts to a node with it's attributes:
MERGE (r:Requests {request_id:request_id})
SET r += map

// Show all nodes:
MATCH (n) RETURN n

Please advise.

2 REPLIES 2

ameyasoft
Graph Maven

I do not see any column names with .data_key and .data_value in the .csv file you referenced. The file has very long column names.

Reiner
Graph Buddy

LOAD CSV returns only the imported data in the output variable.
You need at least a MERGE or CREATE to create nodes. But your suggestion seems a bit too complicated.

The following example shows the minimum needed to merge nodes. But as @ameyasoft mentioned, your column names are very long and since they contain periods and minus signs, they cannot be used directly as property names in your node. It is better to shorten them beforehand and remove the characters that are not allowed. Moreover, the first column should also be given a title or removed if it is not needed.

(You can use these special characters in property names if you encapsulate them with `, but this must then also be used in this way in all subsequent queries and would be too laborious for me).

LOAD CSV WITH HEADERS FROM data_url AS line
MERGE (r:Request { request_id:line.request_id })
SET r = line
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.