如何使用python Rest api在neo4j中获取cypher查询的响应

时间:2015-01-08 12:59:45

标签: python neo4j cypher

我使用Python访问neo4j并创建节点。 在创建节点之前,我想检查它是否存在。我运行这个查询:

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

使用请求库的方法:

           r.text

我得到一个字符串,带有我的POST请求的响应。 我的问题是,如果有一个更优雅的"使用python和rest api检查是否存在具有特定名称的现有节点的方法。

这是我的代码:

   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)

由于 迪米瑞斯

1 个答案:

答案 0 :(得分:5)

我认为你可能比你需要的更努力。有一个名为py2neo的库,可以更简单地完成您想做的事情。如果您使用它,您可以获得实际对象而不是原始JSON,这可能更容易处理:

来自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.