5

I know, we can export neo4j database to CSV file by clicking a ready button. After we implement this cypher query:

Match (n)
return n 

But this query gives us all the properties as 1 rows.

My question is: which Cypher query to implement, before exporting to CSV file to give us all the properties as in separate columns, even if the nodes do not have the same properties,

For example:

node(0) has: name, age.
node(1) has: name, age.
node(2) has: name, address.
node(3) has: name, phone No.
node(4) has: name, age.
node(5) has: name, DoB.

I need the result to be as:

name      age      address      phone No      DoB
Tom       22
Smith     18
Lee                 123abc
Perry                            01234
Sara      40
Tom                                          11/11/2000

Not as:

n
Tom, 22
Smith, 18
Lee, 123abc
Perry, 01234
Sara, 40
Tom, 11/11/2000
Charlotte Skardon
  • 6,220
  • 2
  • 31
  • 42
nana
  • 79
  • 5

3 Answers3

3

To truly get the format you want, you need to explicitly state every column in your Cypher

MATCH (n) RETURN n.name as name, n.age as age, n.address as address, n.'phone No' as 'phone no', n.DoB as DoB

A simpler alternative would be to export the properties as a map, and then just set them when loading. Without apoc though, setting the labels again also needs to be explicit.

MATCH (n) RETURN PROPERTIES(n) as props, LABELS(n) as labels
-----
LOAD CSV ... as csv
// without apoc
CREATE(n)
SET n=csv.props
// or with apoc
CALL apoc.create.node(csv.labels, csv.props) YIELD node
Tezra
  • 8,463
  • 3
  • 31
  • 68
  • Yes this solution ideal when you have a small dataset and you know in advanced what properties that you have, but in case of a large dataset how we get all properties without losing any,, – nana Nov 22 '18 at 07:37
  • @nana In Cypher, you have to explicitly name every column you are returning. Depending on why you need the csv in this format, It would probably be easier to `RETURN PROPERTIES(n)` and than re-parse the output csv to the format you want with a short java/python script. – Tezra Nov 22 '18 at 08:00
  • In [LOAD CSV ... as csv] do we need to write the path that we want to store the csv file in, if so how because I tried many format gave me errors – nana Nov 22 '18 at 09:56
  • @nana That is a seprate cypher, that assumes you are exporting to import to a new db. https://neo4j.com/docs/developer-manual/current/cypher/clauses/load-csv/ If you aren't importing, ignore what is below the --- – Tezra Nov 22 '18 at 16:52
0

One way is to use apoc to export it as CSV.

CALL apoc.export.csv.query("MATCH (n) return n", "/tmp/results.csv", {})

Sample result looks like this, which you can transform to a structure you like using other tools like jq:

enter image description here

Bajal
  • 5,487
  • 3
  • 20
  • 25
0

step 1: get a list of all unique properties:

MATCH (n)
UNWIND keys(n) AS allProps
RETURN COLLECT(DISTINCT allProps) AS distinctProps

which yields ["name", "age", "address", "phoneNo", "DoB"] in this example

step 2: use a text editor to turn turn that list into CQL:

  • replace ", with ,
  • replace " with n.
  • remove [ and ]

which yields n.name, n.age, n.address, n.phoneNo, n.DoB in this example

step 3: use that result for your query:

MATCH (n)
RETURN n.name, n.age, n.address, n.phoneNo, n.DoB

From what I've read it appears that it's not possible to do this all in 1 step but if anyone knows a way I'd LOVE to hear about it

HyperActive
  • 1,129
  • 12
  • 12