4

I am using Python to access neo4j and create nodes. Before I create the node I want to check if it exists. I run this query:

          "query" : "match (PPnode:Node) return PPnode"

And using the method of requests library:

           r.text

I get a string, with the response of my POST request. My question is if there is a more "elegant" way to check if there are existing nodes with a specific name using python and rest api.

This is my code:

   import requests
   import json
   import csv

  headers = {'content-type': 'application/json'}
  url = "http://localhost:7474/db/data/cypher"


  fparts = open('FOC.csv')
  csv_pseudo = csv.reader(fparts)


  for row in csv_pseudo:

   # query to check if node exists
   checkNode = {"query" : "match (PPnode:Node) return PPnode"}
   mkr =requests.post(url, data=json.dumps(checkNode), headers=headers)

Thanks Dimitris

user1919
  • 3,818
  • 17
  • 62
  • 97

1 Answers1

5

I think you might be working harder here than you need to. There's a library called py2neo which will do what you want to do much more simply. If you were using it, you could get actual objects back instead of raw JSON, which might be easier to deal with:

From the documentation on how to run Cypher queries:

from py2neo import Graph
graph = Graph("http://nifty-site:1138/db/data/")
results = graph.cypher.execute("match (PPnode:Node) return PPnode")

for r in results:
    # get the node you return in your query
    ppNode = r[0]
    # get the properties of your node
    props = ppNode.get_properties()
    # Do nifty stuff with properties, not JSON.
FrobberOfBits
  • 17,634
  • 4
  • 52
  • 86
  • Is there something similar for PHP? – user1919 Jan 14 '15 at 12:55
  • I am facing difficulties understanding how to install this php library. I never used composer before. Can you please suggest me a good tutorial? I use MAMP to establish my localserver in a windows8 OS. I installed composer and then a composer.json file but I don't know what to do after. – user1919 Jan 14 '15 at 14:51
  • Found it. I run in the command prompt: "composer install" and I got the dependencies. Everyting works fine now! – user1919 Jan 14 '15 at 15:09
  • Doesn't work with version 3, make sure you use version 2 – loopasam Aug 15 '16 at 13:33