2016-07-13 15 views
1

StackOverflowへのAPIリクエストで取得したJSONファイルをNEO4Jにインポートしようとしています。私はこれに従っていますtutorial。私は次のコードを使用していpy2neoを使用してJSONをNEO4Jにインポート

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' 

:クエリを実行しようとしている間しかし、私は以下のようにエラーが出

results = graph.cypher.run(query,json=json) 
:問題は次の行から発信

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. 

新しいpy2neo apiに調整するために上記の行を変更しなければなりませんでした。元の行はこのように見えた:

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

だから、基本的に、私は特定のクエリを使用してJSONファイルを処理する必要があることのNeo4j伝える方法を見つける必要があります。私はドキュメンタリーを読んで、運がないとウェブを検索しようとしました。どんな助けもありがとう。

答えて

4

スクリプトを動作させるために物事のカップル:

from py2neo import neo4jはもう

クエリで有効な依存関係はありませんが、あなたはパラメータとしてJSONマップを渡すが、あなたがパラメータの構文を使用していませんクエリ、私はWITH {json} as dataをクエリの先頭に追加しました。最後tx.append(query,)が必要とされていない接続

ため

追加secure=False

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() 

結果:スクリプトの作業

で ファイル "/Users/ahmedov/neo4j.py"、14行、:

enter image description here

+0

を私はまだいくつかのエラーを取得しますgraph =グラフ(neo4jUrl、secure = False) TypeError:__new __()に予期しないキーワード引数 'secure'があります secure = falseを削除した場合、 ファイルの "/Users/ahmedov/neo4j.py"、行21、 graph.run( "CREATE CONSTRAINT ON(q:Question)ASSERT q.id "UNIQUE;") AttributeError: 'Graph'オブジェクトに 'run'属性がありません – Ahmedov

+1

py2neoバージョンの不一致があるようですが、 'pip install py2neo --upgrade'を実行できますか? –

+0

ありがとう!ついにそれが働いた。今度はjavaドライバで同様の問題を解決する時間です:) – Ahmedov

関連する問題