3

I'm using neo4jdb-python packages to query the Neo4j database. For example, considering the below code

import neo4j
connection = neo4j.connect("http://localhost:7474")
cursor = connection.cursor()
for i in cursor.execute("MATCH a RETURN a LIMIT 1"):
    print i 

But the output is in the form of a tuple. i.e.

({u'text': u'Stoyanov, S., Hoogveld, B., Kirschner, P.A., (2010). Mapping Major Changes to Education and Training in 2025, in JRC Technical Note JRC59079., Publications Office of the European Union: Luxembourg.', u'identifier': u'reference/lak/226'},)

How do I get the output in csv format.This is possible with the web view of neo4j. and the output is like,

"{""text"":""Stoyanov, S., Hoogveld, B., Kirschner, P.A., (2010). Mapping Major Changes to Education and Training in 2025, in JRC Technical Note JRC59079., Publications Office of the European Union: Luxembourg."",""identifier"":""reference/lak/226""}"

However I want to do it via a client program as I need to embed it into another program. If it is not possible with neo4jdb-python, then what other options are available.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • you want the keys as headers and the values as columns yes? – Padraic Cunningham Nov 08 '14 at 11:15
  • @Padraic to be more specific I want the output as "{""text"":""Stoyanov, S., Hoogveld, B., Kirschner, P.A., (2010). Mapping Major Changes to Education and Training in 2025, in JRC Technical Note JRC59079., Publications Office of the European Union: Luxembourg."",""identifier"":""reference/lak/226""}" – Bhargav Rao Nov 08 '14 at 11:48
  • So you want json? Where is the csv coming into play? – Nicole White Nov 08 '14 at 17:29
  • @NicoleWhite When you export as "CSV" from the Neo4j web view, you get that as output. The JSON O/P is quite weird (and more than 1000 characters for this particular query which is 10 times the size of CSV.) – Bhargav Rao Nov 08 '14 at 18:28
  • When I export a csv from the browser, I get a csv, not json. Which button are you pressing? Can you provide a screen shot? – Nicole White Nov 08 '14 at 19:19

2 Answers2

6

The Neo4j Server returns only JSON, as Mark Needham mentions in his answer.

Hence any code to do convert it to CSV must be on the client side. This can be done using the csv module. Note that neo4jdb-python package is compatible with Python2.7 only.

A minimal code for obtaining the data is

import neo4j
connection = neo4j.connect("http://localhost:7474")
cursor = connection.cursor()
data = list(cursor.execute("MATCH a RETURN a LIMIT 1")

Note that as mentioned in the question the returned values are in the form of tuples. The minimal code to create the csv file is

with open("test.csv","w") as csvfile:
    writer = csv.writer(csvfile,delimiter = ',',quotechar = '"',quoting = csv.QUOTE_ALL)
    writer.writerow(t[0].keys())
    for i in t:
        writer.writerow(['{"%s":"%s"}'%(k,v) for k,v in i.iteritems()])

The explanation for the code is simple, open a file. Using csv.writer, create a writer object. Write the header first using writerow. Finally loop through the dictionary and write the rows.

The output obtained is

"{""text"":""Stoyanov, S., Hoogveld, B., Kirschner, P.A., (2010). Mapping Major Changes to Education and Training in 2025, in JRC Technical Note JRC59079., Publications Office of the European Union: Luxembourg.""}","{""identifier"":""reference/lak/226""}"

which is similar to that obtained using the exportable.coffee script.

Pang
  • 9,564
  • 146
  • 81
  • 122
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
3

That CSV isn't actually coming from a particular API - it's being translated into CSV format on the client side.

The appropriate code is in exportable.coffee if you want to take a look:

    $scope.exportCSV = (data) ->
      return unless data
      csv = new CSV.Serializer()
      csv.columns(data.columns())
      for row in data.rows()
        csv.append(row)

And that refers to CSV.coffee. I guess you should be able to do something similar in Python perhaps using json.dumps like this:

> import json
> t = ({u'text': u'Stoyanov, S., Hoogveld, B., Kirschner, P.A., (2010). Mapping Major Changes to Education and Training in 2025, in JRC Technical Note JRC59079., Publications Office of the European Union: Luxembourg.', u'identifier': u'reference/lak/226'},)
> json.dumps(t)
 '[{"text": "Stoyanov, S., Hoogveld, B., Kirschner, P.A., (2010). Mapping Major Changes to Education and Training in 2025, in JRC Technical Note JRC59079., Publications Office of the European Union: Luxembourg.", "identifier": "reference/lak/226"}]'
Community
  • 1
  • 1
Mark Needham
  • 2,098
  • 17
  • 19
  • Thanks, But is there a direct way to export as CSV atleast in Java or any other language? – Bhargav Rao Nov 09 '14 at 14:47
  • No I don't think so. Neo4j server only returns JSON AFAIK. It shouldn't be too hard to translate that to CSV though or? – Mark Needham Nov 09 '14 at 18:15
  • 2
    Just use opencsv and return what you get from the database via the node properties that you want to export to json. See here for some code: https://github.com/jexp/neo4j-shell-tools/blob/master/src/main/java/org/neo4j/shell/tools/imp/format/CsvFormat.java#L39 – Michael Hunger Nov 09 '14 at 19:52