Home > OS >  Is there a way to import a csv into Neo4j using foreach or unwind?
Is there a way to import a csv into Neo4j using foreach or unwind?

Time:12-02

I am using the following .csv file for Neo4j import. There are 202 rackets. The numbers below racketX are the rating the user has given that racket.

.csv file

I want to create the relationships among the users and the rating they have given to each racket. This is my current approach:

LOAD CSV WITH HEADERS FROM 'http://spreding.online/racket-recommendation-system/data/formattedFiles/formattedUsers.csv' AS row
WITH row 
WHERE row.username IS NOT NULL
MERGE (u:User {
        username: row.username, 
        height_m: toInteger(row.height), 
        weight_kg: toInteger(row.weight)
    }) 

WITH row, u, range(3, 204) as indexes
MATCH (r:Racket)
UNWIND r as racket
UNWIND indexes as i
MERGE (u)-[:RATES {rating:toInteger(row[i])}]->(racket)

I get a "cannot access a map" error. Can you help me?

CodePudding user response:

I would break down the load into multiple steps.

Load the users.

LOAD CSV WITH HEADERS FROM 'http://spreding.online/racket-recommendation-system/data/formattedFiles/formattedUsers.csv' AS row
WITH row 
WHERE row.username IS NOT NULL
MERGE (u:User {
        username: row.username, 
        height_m: toInteger(row.height), 
        weight_kg: toInteger(row.weight)
    }) 

Load the rackets.

UNWIND RANGE(1,202) as idx
CREATE (:Racket {racketNumber:"racket" idx}) 

Load the relationships.

LOAD CSV WITH HEADERS FROM 'http://spreding.online/racket-recommendation-system/data/formattedFiles/formattedUsers.csv' AS row
UNWIND RANGE (1,202) as idx
MATCH (u:User {username:row.username})
MATCH (r:Racket {racketNumber:"racket" idx})
MERGE (u)-[:RATES {rating:toInteger(row["racket" idx])}]->(r)
  • Related