简体   繁体   English

使用 py2neo 将 JSON 导入 NEO4J

[英]Import JSON to NEO4J using py2neo

I am trying to import a JSON file obtained thru an API request to StackOverflow to NEO4J.我正在尝试将通过 API 请求获得的 JSON 文件导入到 StackOverflow 到 NEO4J。 I have been following this tutorial .我一直在关注本教程 However, I get errors like the following while trying to execute the query:但是,在尝试执行查询时出现如下错误:

  File "/Users/ahmedov/anaconda/lib/python2.7/site-packages/py2neo/cypher/core.py", line 306, in commit
    return self.post(self.__commit or self.__begin_commit)

  File "/Users/ahmedov/anaconda/lib/python2.7/site-packages/py2neo/cypher/core.py", line 261, in post
    raise self.error_class.hydrate(error)

  File "/Users/ahmedov/anaconda/lib/python2.7/site-packages/py2neo/cypher/error/core.py", line 54, in hydrate
    error_cls = getattr(error_module, title)

AttributeError: 'module' object has no attribute 'SyntaxError'

I am using the following code:我正在使用以下代码:

import os
import requests
from py2neo import neo4j
from py2neo import Graph

from py2neo import Path, authenticate
# set up authentication parameters
authenticate("localhost:7474", "neo4j", "neo4j")

# connect to authenticated graph database
#graph = Graph("http://localhost:7474/db/data/")

# Connect to graph and add constraints.
neo4jUrl = os.environ.get('NEO4J_URL',"http://localhost:7474/db/data/")
graph = neo4j.Graph(neo4jUrl)

# Connect to graph and add constraints.
#neo4jUrl = os.environ.get('NEO4J_URL',"http://localhost:7474/db/data/")
#graph = neo4j.GraphDatabaseService(neo4jUrl)

# Add uniqueness constraints.
graph.cypher.execute("CREATE CONSTRAINT ON (q:Question) ASSERT q.id IS UNIQUE;")
# Build URL.
apiUrl ="https://api.stackexchange.com/2.2/questions?pagesize=100&order=desc&sort=creation&tagged=neo4j&site=stackoverflow&filter=!5-i6Zw8Y)4W7vpy91PMYsKM-k9yzEsSC1_Uxlf"
# Send GET request.
json = requests.get(apiUrl, headers = {"accept":"application/json"}).json()

# Build query.
query = """
UNWIND data.items as q
MERGE (question:Question {id:q.question_id}) ON CREATE
  SET question.title = q.title, question.share_link = q.share_link, question.favorite_count = q.favorite_count

MERGE (owner:User {id:q.owner.user_id}) ON CREATE SET owner.display_name = q.owner.display_name
MERGE (owner)-[:ASKED]->(question)

FOREACH (tagName IN q.tags | MERGE (tag:Tag {name:tagName}) MERGE (question)-[:TAGGED]->(tag))
FOREACH (a IN q.answers |
   MERGE (question)<-[:ANSWERS]-(answer:Answer {id:a.answer_id})
   MERGE (answerer:User {id:a.owner.user_id}) ON CREATE SET answerer.display_name = a.owner.display_name
   MERGE (answer)<-[:PROVIDED]-(answerer)
)

"""
statement = "MERGE (n:Person {name:{N}}) RETURN n"
results = graph.cypher.run(query,json=json)

tx = graph.cypher.begin()

def add_names(*names):
    for name in names:
        tx.append(statement, {"N": name})
    tx.process()

add_names("Homer", "Marge", "Bart", "Lisa", "Maggie")
add_names("Peter", "Lois", "Chris", "Meg", "Stewie")
tx.append(query,)
tx.commit()
# Send Cypher query.

The problem originates from the following line:问题源于以下行:

    results = graph.cypher.run(query,json=json)

I had to change the above line to adjust it to the newer py2neo api.我不得不更改上面的行以将其调整为更新的 py2neo api。 The original line looked like this:原始行如下所示:

neo4j.CypherQuery(graph, query).run(json=json)

So basically, I need to find a way to tell neo4j that I need to process the JSON file using the given query.所以基本上,我需要找到一种方法来告诉 neo4j 我需要使用给定的查询处理 JSON 文件。 I tried to read the documentary and searching the web with no luck.我试图阅读纪录片并在网上搜索,但没有运气。 Any help would be appreciated.任何帮助将不胜感激。

A couple of things to make your script work :使您的脚本工作的几件事:

from py2neo import neo4j is not a valid dependency anymore from py2neo import neo4j不再是有效的依赖项

In your query, you pass a json map as parameter but you don't use the parameters syntax in the query, I added WITH {json} as data in the beginning of the query.在您的查询中,您将 json 映射作为参数传递,但您没有在查询中使用参数语法,我在查询的开头添加了WITH {json} as data

Added secure=False for the connection为连接添加了secure=False

The last tx.append(query,) is not needed. tx.append(query,)最后的tx.append(query,)

Working script :工作脚本:

import os
import requests
#from py2neo import neo4j
from py2neo import Graph
from py2neo import Path, authenticate
# set up authentication parameters
authenticate("localhost:7474", "neo4j", "neo4j")

# connect to authenticated graph database
#graph = Graph("http://localhost:7474/db/data/")

# Connect to graph and add constraints.
neo4jUrl = os.environ.get('NEO4J_URL',"http://localhost:7474/db/data/")
graph = Graph(neo4jUrl,secure=False)

# Connect to graph and add constraints.
#neo4jUrl = os.environ.get('NEO4J_URL',"http://localhost:7474/db/data/")
#graph = neo4j.GraphDatabaseService(neo4jUrl)

# Add uniqueness constraints.
graph.run("CREATE CONSTRAINT ON (q:Question) ASSERT q.id IS UNIQUE;")
# Build URL.
apiUrl ="https://api.stackexchange.com/2.2/questions?pagesize=100&order=desc&sort=creation&tagged=neo4j&site=stackoverflow&filter=!5-i6Zw8Y)4W7vpy91PMYsKM-k9yzEsSC1_Uxlf"
# Send GET request.
json = requests.get(apiUrl, headers = {"accept":"application/json"}).json()

#print(json);

# Build query.
query = """
WITH {json} as data
UNWIND data.items as q
MERGE (question:Question {id:q.question_id}) ON CREATE
  SET question.title = q.title, question.share_link = q.share_link, question.favorite_count = q.favorite_count

MERGE (owner:User {id:q.owner.user_id}) ON CREATE SET owner.display_name = q.owner.display_name
MERGE (owner)-[:ASKED]->(question)

FOREACH (tagName IN q.tags | MERGE (tag:Tag {name:tagName}) MERGE (question)-[:TAGGED]->(tag))
FOREACH (a IN q.answers |
   MERGE (question)<-[:ANSWERS]-(answer:Answer {id:a.answer_id})
   MERGE (answerer:User {id:a.owner.user_id}) ON CREATE SET answerer.display_name = a.owner.display_name
   MERGE (answer)<-[:PROVIDED]-(answerer)
)

"""
statement = "MERGE (n:Person {name:{N}}) RETURN n"
results = graph.run(query,json=json)

tx = graph.begin()

def add_names(*names):
    for name in names:
        tx.append(statement, {"N": name})
    tx.process()

add_names("Homer", "Marge", "Bart", "Lisa", "Maggie")
add_names("Peter", "Lois", "Chris", "Meg", "Stewie")
#tx.append(query,)
tx.commit()

Result :结果:

在此处输入图片说明

Using a recent version of py2neo, the syntax changes a little bit.使用最新版本的 py2neo,语法略有变化。 Rather than use {json} , is necessary to use $json as explained below:而不是使用{json} ,有必要使用$json ,如下所述:

from py2neo import Graph

graph = Graph(NEO4J_URI, user=NEO4J_USER, password=NEO4j_PASSWORD)

json = ['22644198319', '15383242341']

result = graph.run(
    """WITH $json as data
    UNWIND data as id
    MATCH (c:Person {id: id}) RETURN c.name""", 
    json=json
)
data_frame_result = result.to_data_frame()
print(data_frame_result)

Making these changes in the @christophe-willemsen's example, the code can run in the newest versions.在@christophe-willemsen 的示例中进行这些更改后,代码可以在最新版本中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM