3

Recently I have been using Neo4j for a project for a course I'm following. I've figured out how to use APOC to export my database to .csv.

However, the exported .csv file does not include the relationship ID. I use the following call to export the database:

CALL apoc.export.csv.all("export.csv",{})

I can also use the following Cypher query to obtain the relationship ID's:

MATCH ()-[r]-() RETURN ID(r)

There exists an apoc.export.csv.query() but I cannot think of a query that contains the information that is contained in the apoc.export.csv.all call and also contains the relationship ID.

I could potentially request the ID's separately and put them into export.csv using a Python script, but I'm not sure whether the order of the relationships would be the same for the separate calls/queries.

Any help is greatly appreciated!

  • does the header of the "export.csv" contain `"_start","_end","_type"`? You need to have the actual relationship ID? – Dave Bennett May 26 '17 at 13:03
  • Hey Dave, thanks for the quick response, yes the header contains `_start, _end, _type`. It does contain `_id` for the Nodes, so I was wondering why it does not do that for the relationships. And yes I would like to have the actual relationship ID belonging to those 3 attributes :) – Koen Verhaegh May 26 '17 at 13:10
  • 2
    Keep in mind the relationship id will not be very useful for you, as when you import you have no control over which internal IDs Neo4j will assign to the newly created nodes and relationships. The reason the node _ids are exported is so you'll have a unique key to use to identify the nodes for the sake of connecting them back up via the relationships, but once everything is imported and connected those _ids probably won't be very useful. – InverseFalcon May 26 '17 at 15:45

1 Answers1

1

I could not see a way to do it by changing the config. But if you really want the output with the relationship ID you can clone the apoc repo and make a few changes to apoc/export/csv/CsvFormat.java. It is a pretty quick way to add the relationship id to the output.

You need to add the ID to the header. I appended , "_id:id" in the lines below...

List<String> relHeader = generateHeader(relPropTypes, config.useTypes(), "_start:id", "_end:id", "_type:label", "_id:id");

List<String> header = generateHeader(relPropTypes, config.useTypes(), "_start:id", "_end:id", "_type:label", "_id:id");

And you need to change a few lines in writeRels(SubGraph graph, CSVWriter out, Reporter reporter, Map<String, Class> relPropTypes, int cols, int offset)

I added...

row[offset+3]=String.valueOf(rel.getId());

And changed the offset from 3 to 4 in the following line

collectProps(relPropTypes.keySet(), rel, reporter, row, 4 + offset);

It produced a result that looks like this...

"_id","_labels","name","_start","_end","_type","_id","a_lot"
"0",":Node","A",,,,,
"1",":Node","B",,,,,
"2",":Node","C",,,,,
,,,"0","1","REL","0",""
,,,"0","2","LIKES","1","true"

I used 3.2 community for the test. I have no idea what further ramifications the changes may or may not have on the overall function of the apoc.export.* collection.

Dave Bennett
  • 10,996
  • 3
  • 30
  • 41