|
139 | 139 | "outputs": [],
|
140 | 140 | "source": [
|
141 | 141 | "gis_backup = GIS(\"home\") # connect to portal\n",
|
| 142 | + "# connect to knowledge graph service\n", |
142 | 143 | "knowledgegraph_backup = KnowledgeGraph(\n",
|
143 | 144 | " \"https://myportal.com/server/rest/services/Hosted/myknowledgegraph/KnowledgeGraphServer\",\n",
|
144 | 145 | " gis=gis_backup,\n",
|
145 |
| - ") # connect to knowledge graph service\n", |
| 146 | + ")\n", |
146 | 147 | "try:\n",
|
147 | 148 | " knowledgegraph_backup.datamodel\n",
|
148 | 149 | "except:\n",
|
|
155 | 156 | "metadata": {},
|
156 | 157 | "source": [
|
157 | 158 | "## Write data model entity types to backup json file\n",
|
158 |
| - "Iterate through the data model of the knowledge graph to write all entity type objects to a json backup file" |
| 159 | + "Iterate through the data model of the knowledge graph to write all entity type objects to a backup json file" |
159 | 160 | ]
|
160 | 161 | },
|
161 | 162 | {
|
|
165 | 166 | "metadata": {},
|
166 | 167 | "outputs": [],
|
167 | 168 | "source": [
|
168 |
| - "entity_types = []\n", |
169 | 169 | "# create list of formatted entity types\n",
|
| 170 | + "entity_types = []\n", |
170 | 171 | "for types in knowledgegraph_backup.datamodel[\"entity_types\"]:\n",
|
171 | 172 | " curr_entity_type = {\n",
|
172 | 173 | " \"name\": knowledgegraph_backup.datamodel[\"entity_types\"][types][\"name\"],\n",
|
173 | 174 | " \"properties\": knowledgegraph_backup.datamodel[\"entity_types\"][types][\n",
|
174 | 175 | " \"properties\"\n",
|
175 | 176 | " ],\n",
|
176 | 177 | " }\n",
|
177 |
| - " entity_types.append(curr_entity_type)\n", |
| 178 | + " entity_types.append(curr_entity_type)" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "code", |
| 183 | + "execution_count": null, |
| 184 | + "id": "d73069ba", |
| 185 | + "metadata": {}, |
| 186 | + "outputs": [], |
| 187 | + "source": [ |
178 | 188 | "# write entity types to json file\n",
|
179 | 189 | "with open(os.path.join(output_folder, dm_ent), \"w\") as f:\n",
|
180 | 190 | " json.dump(entity_types, f)"
|
|
186 | 196 | "metadata": {},
|
187 | 197 | "source": [
|
188 | 198 | "## Write data model relationship types to backup json file\n",
|
189 |
| - "Iterate through the data model of the knowledge graph to write all relationship type objects to a json backup file" |
| 199 | + "Iterate through the data model of the knowledge graph to write all relationship type objects to a backup json file" |
190 | 200 | ]
|
191 | 201 | },
|
192 | 202 | {
|
|
196 | 206 | "metadata": {},
|
197 | 207 | "outputs": [],
|
198 | 208 | "source": [
|
199 |
| - "relationship_types = []\n", |
200 | 209 | "# create list of formatted relationship types\n",
|
| 210 | + "relationship_types = []\n", |
201 | 211 | "for types in knowledgegraph_backup.datamodel[\"relationship_types\"]:\n",
|
202 | 212 | " curr_relationship_type = {\n",
|
203 | 213 | " \"name\": knowledgegraph_backup.datamodel[\"relationship_types\"][types][\"name\"],\n",
|
204 | 214 | " \"properties\": knowledgegraph_backup.datamodel[\"relationship_types\"][types][\n",
|
205 | 215 | " \"properties\"\n",
|
206 | 216 | " ],\n",
|
207 | 217 | " }\n",
|
208 |
| - " relationship_types.append(curr_relationship_type)\n", |
| 218 | + " relationship_types.append(curr_relationship_type)" |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "code", |
| 223 | + "execution_count": null, |
| 224 | + "id": "cee22fea", |
| 225 | + "metadata": {}, |
| 226 | + "outputs": [], |
| 227 | + "source": [ |
209 | 228 | "# write relationship types to json file\n",
|
210 | 229 | "with open(os.path.join(output_folder, dm_rel), \"w\") as f:\n",
|
211 | 230 | " json.dump(relationship_types, f)"
|
|
227 | 246 | "metadata": {},
|
228 | 247 | "outputs": [],
|
229 | 248 | "source": [
|
| 249 | + "# query for all entities in graph\n", |
230 | 250 | "original_entities = knowledgegraph_backup.query_streaming(\n",
|
231 | 251 | " \"MATCH (n) RETURN distinct n\"\n",
|
232 |
| - ") # query for all entities in graph\n", |
| 252 | + ")" |
| 253 | + ] |
| 254 | + }, |
| 255 | + { |
| 256 | + "cell_type": "code", |
| 257 | + "execution_count": null, |
| 258 | + "id": "bc90a817", |
| 259 | + "metadata": {}, |
| 260 | + "outputs": [], |
| 261 | + "source": [ |
| 262 | + "# create list of formatted entities to add to the graph\n", |
233 | 263 | "all_entities_fromquery = []\n",
|
234 | 264 | "for entity in list(original_entities):\n",
|
235 | 265 | " curr_entity = entity[0]\n",
|
|
238 | 268 | " for prop in curr_entity[\"_properties\"]:\n",
|
239 | 269 | " if type(curr_entity[\"_properties\"][prop]) == UUID:\n",
|
240 | 270 | " curr_entity[\"_properties\"][prop] = str(curr_entity[\"_properties\"][prop])\n",
|
| 271 | + " # delete objectid, the server will handle creating new ones when we load the backup\n", |
241 | 272 | " del curr_entity[\"_properties\"][\n",
|
242 | 273 | " \"objectid\"\n",
|
243 |
| - " ] # delete objectid, the server will handle creating new ones when we load the backup\n", |
244 |
| - " all_entities_fromquery.append(curr_entity)\n", |
| 274 | + " ]\n", |
| 275 | + " all_entities_fromquery.append(curr_entity)" |
| 276 | + ] |
| 277 | + }, |
| 278 | + { |
| 279 | + "cell_type": "code", |
| 280 | + "execution_count": null, |
| 281 | + "id": "2760a47b", |
| 282 | + "metadata": {}, |
| 283 | + "outputs": [], |
| 284 | + "source": [ |
245 | 285 | "# write entities list to json file\n",
|
246 | 286 | "with open(os.path.join(output_folder, all_ent), \"w\") as f:\n",
|
247 | 287 | " json.dump(all_entities_fromquery, f)"
|
|
263 | 303 | "metadata": {},
|
264 | 304 | "outputs": [],
|
265 | 305 | "source": [
|
| 306 | + "# query for all relationships in graph\n", |
266 | 307 | "original_relationships = knowledgegraph_backup.query_streaming(\n",
|
267 | 308 | " \"MATCH ()-[rel]->() RETURN distinct rel\"\n",
|
268 |
| - ") # query for all relationships in graph\n", |
| 309 | + ")" |
| 310 | + ] |
| 311 | + }, |
| 312 | + { |
| 313 | + "cell_type": "code", |
| 314 | + "execution_count": null, |
| 315 | + "id": "b4835acc", |
| 316 | + "metadata": {}, |
| 317 | + "outputs": [], |
| 318 | + "source": [ |
| 319 | + "# create list of formatted entities to add to the graph\n", |
269 | 320 | "all_relationships_fromquery = []\n",
|
270 | 321 | "for relationship in list(original_relationships):\n",
|
271 | 322 | " curr_relationship = relationship[0]\n",
|
|
280 | 331 | " curr_relationship[\"_properties\"][prop] = str(\n",
|
281 | 332 | " curr_relationship[\"_properties\"][prop]\n",
|
282 | 333 | " )\n",
|
| 334 | + " # delete objectid, the server will handle creating new ones when we load the backup\n", |
283 | 335 | " del curr_relationship[\"_properties\"][\n",
|
284 | 336 | " \"objectid\"\n",
|
285 |
| - " ] # delete objectid, the server will handle creating new ones when we load the backup\n", |
286 |
| - " all_relationships_fromquery.append(curr_relationship)\n", |
| 337 | + " ]\n", |
| 338 | + " all_relationships_fromquery.append(curr_relationship)" |
| 339 | + ] |
| 340 | + }, |
| 341 | + { |
| 342 | + "cell_type": "code", |
| 343 | + "execution_count": null, |
| 344 | + "id": "ffb27ca4", |
| 345 | + "metadata": {}, |
| 346 | + "outputs": [], |
| 347 | + "source": [ |
287 | 348 | "# write relationships list to json file\n",
|
288 | 349 | "with open(os.path.join(output_folder, all_rel), \"w\") as f:\n",
|
289 | 350 | " json.dump(all_relationships_fromquery, f)"
|
|
305 | 366 | "metadata": {},
|
306 | 367 | "outputs": [],
|
307 | 368 | "source": [
|
| 369 | + "# query for all provenance records in the graph\n", |
308 | 370 | "provenance_entities = knowledgegraph_backup.query_streaming(\n",
|
309 | 371 | " \"MATCH (n:Provenance) RETURN distinct n\", include_provenance=True\n",
|
310 |
| - ")\n", |
| 372 | + ")" |
| 373 | + ] |
| 374 | + }, |
| 375 | + { |
| 376 | + "cell_type": "code", |
| 377 | + "execution_count": null, |
| 378 | + "id": "402ca4ce", |
| 379 | + "metadata": {}, |
| 380 | + "outputs": [], |
| 381 | + "source": [ |
| 382 | + "# create list of formatted provenance records to the graph\n", |
311 | 383 | "all_provenance_fromquery = []\n",
|
312 | 384 | "for entity in list(provenance_entities):\n",
|
313 | 385 | " curr_provenance = entity[0]\n",
|
|
318 | 390 | " curr_provenance[\"_properties\"][prop] = str(\n",
|
319 | 391 | " curr_provenance[\"_properties\"][prop]\n",
|
320 | 392 | " )\n",
|
| 393 | + " # delete objectid, the server will handle creating new ones when we load the backup\n", |
321 | 394 | " del curr_provenance[\"_properties\"][\n",
|
322 | 395 | " \"objectid\"\n",
|
323 |
| - " ] # delete objectid, the server will handle creating new ones when we load the backup\n", |
324 |
| - " all_provenance_fromquery.append(curr_provenance)\n", |
| 396 | + " ]\n", |
| 397 | + " all_provenance_fromquery.append(curr_provenance)" |
| 398 | + ] |
| 399 | + }, |
| 400 | + { |
| 401 | + "cell_type": "code", |
| 402 | + "execution_count": null, |
| 403 | + "id": "e00efce3", |
| 404 | + "metadata": {}, |
| 405 | + "outputs": [], |
| 406 | + "source": [ |
325 | 407 | "# write provenance list to json file\n",
|
326 | 408 | "with open(os.path.join(output_folder, prov_file), \"w\") as f:\n",
|
327 | 409 | " json.dump(all_provenance_fromquery, f)"
|
|
351 | 433 | "metadata": {},
|
352 | 434 | "outputs": [],
|
353 | 435 | "source": [
|
| 436 | + "# connect to portal via GIS\n", |
354 | 437 | "gis_load = GIS(\n",
|
355 | 438 | " \"https://myportal.com/portal\", \"username\", \"password\"\n",
|
356 |
| - ") # connect to portal via GIS\n", |
| 439 | + ")\n", |
357 | 440 | "# create a knowledge graph without provenance enabled\n",
|
358 | 441 | "result = gis_load.content.create_service(\n",
|
359 | 442 | " name=\"myknowledgegraph\",\n",
|
|
381 | 464 | "metadata": {},
|
382 | 465 | "outputs": [],
|
383 | 466 | "source": [
|
| 467 | + "# load data model json files into graph data model\n", |
384 | 468 | "with open(os.path.join(output_folder, dm_ent), \"r\") as file:\n",
|
385 | 469 | " dm_ents = json.load(file)\n",
|
386 | 470 | "with open(os.path.join(output_folder, dm_rel), \"r\") as file:\n",
|
|
411 | 495 | "for entity_type in dm_ents:\n",
|
412 | 496 | " for prop in entity_type[\"properties\"]:\n",
|
413 | 497 | " if entity_type[\"properties\"][prop][\"role\"] == \"esriGraphNamedObjectDocument\":\n",
|
414 |
| - " doc_type_name = entity_type[\"name\"]\n", |
| 498 | + " doc_type_name = entity_type[\"name\"]" |
| 499 | + ] |
| 500 | + }, |
| 501 | + { |
| 502 | + "cell_type": "code", |
| 503 | + "execution_count": null, |
| 504 | + "id": "0c3eaba3", |
| 505 | + "metadata": {}, |
| 506 | + "outputs": [], |
| 507 | + "source": [ |
415 | 508 | "# get document relationship type name\n",
|
416 | 509 | "doc_rel_type_name = \"HasDocument\"\n",
|
417 | 510 | "for relationship_type in dm_rels:\n",
|
|
449 | 542 | " prop_list.append(origin_document_properties[prop])\n",
|
450 | 543 | "knowledgegraph_load.graph_property_adds(\n",
|
451 | 544 | " type_name=\"Document\", graph_properties=prop_list\n",
|
452 |
| - ")\n", |
| 545 | + ")" |
| 546 | + ] |
| 547 | + }, |
| 548 | + { |
| 549 | + "cell_type": "code", |
| 550 | + "execution_count": null, |
| 551 | + "id": "4aead427", |
| 552 | + "metadata": {}, |
| 553 | + "outputs": [], |
| 554 | + "source": [ |
453 | 555 | "# load any additional document relationship type properties\n",
|
454 | 556 | "for relationship_type in dm_rels:\n",
|
455 | 557 | " if relationship_type[\"name\"] == doc_rel_type_name:\n",
|
|
479 | 581 | "outputs": [],
|
480 | 582 | "source": [
|
481 | 583 | "date_properties = []\n",
|
482 |
| - "# add date property names for entitie types\n", |
| 584 | + "# add date property names for entity types\n", |
483 | 585 | "for types in dm_ents:\n",
|
484 | 586 | " for prop in types[\"properties\"]:\n",
|
485 | 587 | " if types[\"properties\"][prop][\"fieldType\"] == \"esriFieldTypeDate\":\n",
|
486 |
| - " date_properties.append(prop)\n", |
| 588 | + " date_properties.append(prop)" |
| 589 | + ] |
| 590 | + }, |
| 591 | + { |
| 592 | + "cell_type": "code", |
| 593 | + "execution_count": null, |
| 594 | + "id": "4d13d12b", |
| 595 | + "metadata": {}, |
| 596 | + "outputs": [], |
| 597 | + "source": [ |
487 | 598 | "# add date property names for relationship types\n",
|
488 | 599 | "for types in dm_rels:\n",
|
489 | 600 | " for prop in types[\"properties\"]:\n",
|
|
641 | 752 | " prop_list.append(prop)\n",
|
642 | 753 | " knowledgegraph_load.update_search_index(\n",
|
643 | 754 | " adds={entity_type: {\"property_names\": prop_list}}\n",
|
644 |
| - " )\n", |
| 755 | + " )" |
| 756 | + ] |
| 757 | + }, |
| 758 | + { |
| 759 | + "cell_type": "code", |
| 760 | + "execution_count": null, |
| 761 | + "id": "29234c25", |
| 762 | + "metadata": {}, |
| 763 | + "outputs": [], |
| 764 | + "source": [ |
645 | 765 | "# add search indexes for all relationship text properties\n",
|
646 | 766 | "for entity_type in load_dm[\"relationship_types\"]:\n",
|
647 | 767 | " prop_list = []\n",
|
|
674 | 794 | "source": [
|
675 | 795 | "# load provenance records json file\n",
|
676 | 796 | "with open(os.path.join(output_folder, prov_file), \"r\") as file:\n",
|
677 |
| - " prov_entities = json.load(file)\n", |
| 797 | + " prov_entities = json.load(file)" |
| 798 | + ] |
| 799 | + }, |
| 800 | + { |
| 801 | + "cell_type": "code", |
| 802 | + "execution_count": null, |
| 803 | + "id": "973c889f", |
| 804 | + "metadata": {}, |
| 805 | + "outputs": [], |
| 806 | + "source": [ |
678 | 807 | "# add all provenance records\n",
|
679 | 808 | "for curr_prov in prov_entities:\n",
|
680 | 809 | " # format UUID properties\n",
|
|
0 commit comments