如何使用 python Rest api 在 neo4j 中获取密码查询的响应
How to get the response of a cypher query in neo4j using python Rest api
我正在使用 Python 访问 neo4j 并创建节点。
在创建节点之前,我想检查它是否存在。我运行此查询:
"query" : "match (PPnode:Node) return PPnode"
并使用requests库的方法:
r.text
我得到一个字符串,其中包含我的 POST 请求的响应。
我的问题是是否有更多 "elegant" 方法来检查是否存在具有特定名称的现有节点,使用 python 和其余 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)
谢谢
季米特里斯
我认为你在这里工作可能比你需要的更努力。有一个名为 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.
我正在使用 Python 访问 neo4j 并创建节点。 在创建节点之前,我想检查它是否存在。我运行此查询:
"query" : "match (PPnode:Node) return PPnode"
并使用requests库的方法:
r.text
我得到一个字符串,其中包含我的 POST 请求的响应。 我的问题是是否有更多 "elegant" 方法来检查是否存在具有特定名称的现有节点,使用 python 和其余 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)
谢谢 季米特里斯
我认为你在这里工作可能比你需要的更努力。有一个名为 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.