Keeping Cool
Keeping Cool

Reputation: 65

Loading Data from CSV to Neo4j

I have data that looks like

Bob             Joe           Nathan

4              5               4
3              6               111
3              3               3
111            3               4
5              2

But I have about 300 columns of 'Names' and the numbers are 'Attributes' in a CSV. How would I go about loading all this data into neo4j without typing in all the 'Names' and 'Attributes' and linking them to eachother? Thanks in Advance.

Upvotes: 2

Views: 962

Answers (1)

Bruno Peres
Bruno Peres

Reputation: 16355

I don't know if this is the more elegant solution, but I have a tip to you.

First you should use a tool like Transpose CSV Tool. Using this tool you can input a CSV file like:

Bob,Joe,Nathan
4,5,4
3,6,111
3,3,3
111,3,4
5,2,

And the output will be:

Bob,4,3,3,111,5
Joe,5,6,3,3,2
Nathan,4,111,3,4,

After, you can put the CSV file into the import directory and use LOAD CSV like this:

LOAD CSV FROM "file:///convertedcsv.csv" AS line
WITH line[0] as name, filter(l IN line where l is not null) AS line
UNWIND line[1..] as number
WITH name, collect(toInteger(number)) as numbers
CREATE ({name:name,numbers:numbers})

In the first WITH the above query store the name of each line in a variable called name and remove all nulls of the entire line, saving the result into a variable called line. After, UNWIND gets the numbers starting from index 1 (because index 0 is the name) and save each number into number. Each number is collected into numbers and converted to integers.

The resultant nodes are:

MATCH (node) RETURN node as nodes

╒═══════════════════════════════════════╕
│"nodes"                                │
╞═══════════════════════════════════════╡
│{"name":"Bob","numbers":[4,3,3,111,5]} │
├───────────────────────────────────────┤
│{"name":"Nathan","numbers":[4,111,3,4]}│
├───────────────────────────────────────┤
│{"name":"Joe","numbers":[5,6,3,3,2]}   │
└───────────────────────────────────────┘

Upvotes: 1

Related Questions