-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphLoader.py
More file actions
222 lines (192 loc) · 8.91 KB
/
GraphLoader.py
File metadata and controls
222 lines (192 loc) · 8.91 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from neo4j import GraphDatabase
from neo4j.exceptions import ServiceUnavailable
import myconfig
import logging
class GraphLoader:
def __init__(self):
scheme = "neo4j" # Connecting to Aura, use the "neo4j+s" URI scheme
host_name = myconfig.neo4j_url
port = myconfig.neo4j_port
uri = "{scheme}://{host_name}:{port}".format(scheme=scheme, host_name=host_name, port=port)
user = myconfig.neo4j_user
password = myconfig.neo4j_password
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
# Don't forget to close the driver connection when you are finished with it
self.driver.close()
def create_registry_object(self, row):
# name=row[0],
# title=row[5],
# ro_id=row[0],
# key=row[2],
# status=row[6],
# ro_class=row[3],
# slug=row[4]
with self.driver.session() as session:
# Write transactions allow the driver to handle retries and transient errors
result = session.write_transaction(
self._create_registry_object, row)
for record in result:
print("Created RegistryObject Vertex: {p}".format(
p=record['p']))
result = session.write_transaction(
self._create_identifier, identifier_type="ro_key", identifier_value=row[2], ro_class=row[3])
for record in result:
print("Created Identifier for roKey Vertex: {n}".format(
n=record['n']))
result = session.write_transaction(
self._create_same_as_relationship_to_identifier_vertex, "ro_key", row[2], row[0])
for record in result:
print("Created sameAs relationship between key and Identifier: {r}".format(
r=record['r']))
@staticmethod
def _create_registry_object(tx, row):
query = (
"MERGE (p:RegistryObject {name:$name, title:$title, ro_id:$ro_id, "
"key:$key, status:$status, ro_class:$ro_class, slug:$slug}) "
"RETURN p"
)
#print(row)
#exit(0)
result = tx.run(query, name=row[0], title=row[5], ro_id=row[0],
key=row[2], status=row[6], ro_class=row[3], slug=row[4])
try:
return [{"p": record["p"]["title"]}
for record in result]
# Capture any errors along with the query and data for traceability
except ServiceUnavailable as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise
def create_identifier(self, row):
#print(row)
with self.driver.session() as session:
# Write transactions allow the driver to handle retries and transient errors
result = session.write_transaction(
self._create_identifier, row[3], row[2], row[4])
for record in result:
print("Created Identifier Vertex: {n}".format(
n=record['n']))
result = session.write_transaction(
self._create_same_as_relationship_to_identifier_vertex, row[3], row[2], row[1])
for record in result:
print("Created sameAs to Identifier: {r}".format(
r=record['r']))
@staticmethod
def _create_identifier(tx, identifier_type, identifier_value, ro_class):
if ro_class is None:
ro_class = "unknown"
if ro_class != "unknown":
query = (
"MERGE (n:Identifier {identifier_type:$identifier_type, identifier_value:$identifier_value}) "
"ON CREATE SET n.ro_class = $ro_class "
"RETURN n"
)
else:
query = (
"MERGE (n:Identifier {identifier_type:$identifier_type, identifier_value:$identifier_value}) "
"ON CREATE SET n.ro_class = $ro_class "
"ON MATCH SET n.ro_class = $ro_class "
"RETURN n"
)
#print(row)
#exit(0)
result = tx.run(query, identifier_type=identifier_type, identifier_value=identifier_value, ro_class=ro_class)
try:
return [record for record in result]
# Capture any errors along with the query and data for traceability
except ServiceUnavailable as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise
@staticmethod
def _create_same_as_relationship_to_identifier_vertex(tx, identifier_type, identifier_value, ro_id):
query = (
"MATCH (p:RegistryObject {ro_id: $ro_id}) "
"MATCH (i:Identifier {identifier_value: $identifier_value, identifier_type: $identifier_type}) "
"MERGE (p)-[r:sameAs]->(i)"
"RETURN r"
)
#print(row)
#exit(0)
result = tx.run(query, identifier_type=identifier_type, identifier_value=identifier_value, ro_id=ro_id)
try:
return [record for record in result]
# Capture any errors along with the query and data for traceability
except ServiceUnavailable as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise
def create_registry_object_relationship(self, row):
if row[1] is None or row[1].strip() == '':
return
if row[4] is None or row[4].strip() == '':
return
with self.driver.session() as session:
# Write transactions allow the driver to handle retries and transient errors
result = session.write_transaction(
self._create_identifier, "ro_key", row[1], "unknown")
for record in result:
print("Created Identifier for roKey Vertex: {n}".format(
n=record['n']))
#print(row[1], row[0], row[2], row[4])
result = session.write_transaction(
self._create_relationship_to_identifier_vertex, "ro_key", row[1], row[0], row[2], row[4])
for record in result:
print("Created relationship between key and Identifier: {rel}".format(
rel=record['rel']))
def create_identifier_relationship(self, row):
with self.driver.session() as session:
# Write transactions allow the driver to handle retries and transient errors
result = session.write_transaction(
self._create_identifier, row[4], row[2], row[3])
for record in result:
print("Created Identifier Vertex: {n}".format(
n=record['n']))
#print(row[4], row[2], row[1], row[3], row[5])
# identifier_type, identifier_value, ro_id, to_class, relation_type
result = session.write_transaction(
self._create_relationship_to_identifier_vertex, row[4], row[2], row[1], row[3], row[5])
for record in result:
print("Created relationship between Identifier: {rel}".format(
rel=record['rel']))
@staticmethod
def _create_relationship_to_identifier_vertex(tx, identifier_type, identifier_value, ro_id, to_class, relation_type):
if to_class is None:
to_class = "collection"
query = (
"MATCH (p:RegistryObject {ro_id: $ro_id}) "
"MATCH (i:Identifier {identifier_value: $identifier_value, identifier_type: $identifier_type}) "
"CALL apoc.merge.relationship(p, $relation_type, {to_class:$to_class}, {to_class:$to_class}, i, {}) "
"YIELD rel "
"RETURN rel;"
)
#print(row)
#exit(0)
result = tx.run(query, identifier_type=identifier_type, identifier_value=identifier_value, ro_id=ro_id,
relation_type=relation_type, to_class=to_class)
try:
return [record for record in result]
# Capture any errors along with the query and data for traceability
except ServiceUnavailable as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise
@staticmethod
def _find_and_return_registry_object_by_id(tx, ro_id):
query = (
"MATCH (p:RegistryObject) "
"WHERE p.ro_id = $ro_id "
"RETURN p"
)
result = tx.run(query, ro_id=ro_id)
return [record for record in result]
@staticmethod
def _find_and_return_identifier_by_value_and_type(tx, value, type):
query = (
"MATCH (i:Identifier) "
"WHERE i.identifier_value = $value AND i.identifier_type = $type"
"RETURN i"
)
result = tx.run(query, value=value, type=type)
return [record for record in result]