-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathimport_csv.py
More file actions
164 lines (151 loc) · 4.84 KB
/
Copy pathimport_csv.py
File metadata and controls
164 lines (151 loc) · 4.84 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import sys
import pandas as pd
from itertools import islice
import re
from glob import glob
from neo4j import GraphDatabase
from dotenv import load_dotenv
import numpy as np
load_dotenv()
def df_parser_node(df):
for i in df.iterrows():
props = i[1].dropna().to_dict()
props["id"] = str(props["id"])
yield props
def df_parser_edge(df):
for i in df.iterrows():
props = i[1].dropna().to_dict()
source = str(props.pop('source'))
target = str(props.pop('target'))
yield (source, props, target)
def index_nodes(node_type, name):
with GraphDatabase.driver(os.getenv('NEO4J_URL'), auth=(os.getenv('NEO4J_USER'), os.getenv('NEO4J_PASSWORD'))) as driver:
with driver.session(database="neo4j") as session:
tx = session.begin_transaction()
try:
tx.run("CREATE CONSTRAINT unique_id_%s IF NOT EXISTS FOR (n:%s) REQUIRE n.id IS UNIQUE"%(name, node_type))
tx.run("CREATE INDEX index_id_%s IF NOT EXISTS FOR (n:%s) ON (n.id)"%(name, node_type))
tx.run("CREATE INDEX index_label_%s IF NOT EXISTS FOR (n:%s) ON (n.label)"%(name, node_type))
tx.commit()
except Exception as e:
print(e)
tx.rollback()
finally:
tx.close()
def ingest_node(node_type, nodes, limit=10000):
success = True
with GraphDatabase.driver(os.getenv('NEO4J_URL'), auth=(os.getenv('NEO4J_USER'), os.getenv('NEO4J_PASSWORD'))) as driver:
with driver.session(database="neo4j") as session:
skip = 0
print("Ingesting: %s"%(node_type))
while skip < len(nodes):
batch = nodes[skip: skip+limit]
tx = session.begin_transaction()
try:
query = '''
UNWIND $batch as map
CREATE (n:%s)
SET n = map
'''%(node_type)
tx.run(query, {"batch": batch})
skip += limit
tx.commit()
except Exception as e:
print("Error rolling back...")
print("Exception", e)
tx.rollback()
success = False
break
finally:
tx.close()
else:
success = True
return success
def ingest_edges(relation, meta, source, target, edges, limit=10000):
success = True
with GraphDatabase.driver(os.getenv('NEO4J_URL'), auth=(os.getenv('NEO4J_USER'), os.getenv('NEO4J_PASSWORD'))) as driver:
with driver.session(database="neo4j") as session:
skip = 0
while skip < len(edges):
batch = edges[skip: skip+limit]
tx = session.begin_transaction()
try:
query = '''
UNWIND $batch as row
MATCH (n:%s), (m:%s)
WHERE n.id=row.source and m.id=row.target
CREATE (n)-[r:%s {
%s
}]->(m)
'''%(source, target, relation, meta)
print(query)
tx.run(query, {"batch": batch})
skip += limit
tx.commit()
except Exception as e:
print("Error rolling back...")
print("Exception", e)
tx.rollback()
success = False
break
finally:
tx.close()
else:
success = True
return success
directories = sys.argv[1:]
node_pattern = "(?P<directory>.+)/(?P<label>.+)\.(?P<entity>.+)\.csv"
edge_pattern = "(?P<directory>.+)/(?P<source_type>.+)\.(?P<relation>.+)\.(?P<target_type>.+)\.(?P<entity>.+)\.csv"
for directory in directories:
directory = directory.strip()
for filename in glob(directory + "/*.nodes.csv"):
match = re.match(node_pattern, filename).groupdict()
entity = match["entity"]
label = match["label"]
n = label
if len(label.split(" ")) > 1:
n = "`%s`"%label
# add constraint
index_nodes(n, label.replace(" ", "_"))
print("Ingesting %s nodes..."%label)
node_dict = {}
df = pd.read_csv(filename)
for k,row in df.iterrows():
v = {}
for i,j in row.items():
if type(j) == str:
v[i] = j
elif not np.isnan(j):
v[i] = int(j)
if k not in node_dict:
node_dict[k] = {
"id": k,
**v
}
else:
node_dict[k] = {
**node_dict[k],
**v
}
print(n)
r = ingest_node(n, list(node_dict.values()))
for filename in glob(directory + "/*.edges.csv"):
match = re.match(edge_pattern, filename).groupdict()
entity = match["entity"]
source_type = "`%s`"%match["source_type"]
relation = "`%s`"%match["relation"]
print("Ingesting %s edges..."%relation)
target_type = "`%s`"%match["target_type"]
# add constraint
df = pd.read_csv(filename)
meta = []
for col in df.columns:
if (col not in ["source", 'target']):
meta.append("%s:row.%s"%(col, col))
edges = list(df.to_dict(orient="index").values())
print("Ingesting %d %s relation"%(len(edges), relation))
meta = ",\n".join(meta)
success = ingest_edges(relation, meta, source_type, target_type, edges)
if not success:
break