-
Notifications
You must be signed in to change notification settings - Fork 2
/
neo4j_yelp.py
48 lines (41 loc) · 2.06 KB
/
neo4j_yelp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from py2neo import Graph
import config
uri = "{}://{}:{}@{}:{}".format(config.PROTO, config.USER, config.PASSWORD, config.HOSTNAME, config.PORT)
graph = Graph(uri)
print("[INFO] Clearing graph of any existing data")
graph.evaluate("MATCH (n) DETACH DELETE n")
print("[INFO] Asserting schema")
graph.evaluate("CALL apoc.schema.assert({Category:['name']},{Business:['id'],User:['id'],Review:['id']})")
print("[INFO] Loading businesses")
graph.evaluate('CALL apoc.periodic.iterate("'
'CALL apoc.load.json(\'file:///business.json\') YIELD value RETURN value '
'"," '
'MERGE (b:Business{id:value.business_id}) '
'SET b += apoc.map.clean(value, [\'business_id\',\'categories\',\'postal_code\'],[]) '
'WITH b,value.categories as categories '
'UNWIND categories as category '
'MERGE (c:Category{id:category}) '
'MERGE (b)-[:IN_CATEGORY]->(c)"'
',{batchSize: 10000, iterateList: true});')
print("[INFO] Loading users")
graph.evaluate('CALL apoc.periodic.iterate("'
'CALL apoc.load.json(\'file:///user.json\') '
'YIELD value RETURN value '
'"," '
'MERGE (u:User{id:value.user_id}) '
'SET u += apoc.map.clean(value, [\'friends\',\'user_id\'],[0]) '
'WITH u,value.friends as friends '
'UNWIND friends as friend '
'MERGE (u1:User{id:friend}) '
'MERGE (u)-[:FRIEND]-(u1) '
'",{batchSize: 100, iterateList: true});')
print("[INFO] Loading reviews")
graph.evaluate('CALL apoc.periodic.iterate("'
'CALL apoc.load.json(\'file:///review.json\') '
'YIELD value RETURN value '
'"," '
'MERGE (b:Business{id:value.business_id}) '
'MERGE (u:User{id:value.user_id}) '
'MERGE (u)-[r:REVIEWS]->(b) '
'SET r += apoc.map.clean(value, [\'business_id\',\'user_id\',\'review_id\'],[0])'
'",{batchSize: 10000, iterateList: true});')