-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneo_insert.py
More file actions
45 lines (34 loc) · 1.41 KB
/
neo_insert.py
File metadata and controls
45 lines (34 loc) · 1.41 KB
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
import argparse
import sys
from config import *
from neo4j.v1 import GraphDatabase, basic_auth
def parse_args():
parser = argparse.ArgumentParser(description="Inserts the provided dataset of friendship relations into a neo4j database")
parser.add_argument("--neo_pw", help="password to the neo4j database")
parser.add_argument("--neo_db", help="name of the neo4j database")
parser.add_argument("--data", help="path to dataset to be inserted into the neo4j database, should contain friendship relations", default="facebook_combined.txt")
return parser.parse_args()
def main():
local_args = parse_args()
args = init(local_args)
if args == None:
exit_str = "\nNon-existing or insufficent config, execute program with -h to view parameters required for config creation\n"
sys.exit(exit_str)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth(args.neo_db, args.neo_pw))
data = []
with open(args.data, 'r') as f:
for line in f:
line = line.strip('\n')
subset = line.split(' ')
data.append(subset)
# Insert data
insert_query = '''
UNWIND {pairs} as pair
MERGE (p1:Person {name:pair[0]})
MERGE (p2:Person {name:pair[1]})
MERGE (p1)-[:KNOWS]-(p2);
'''
with driver.session() as session:
session.run(insert_query, parameters={"pairs": data})
if __name__ == "__main__":
main()