Skip to content

Commit fc8502c

Browse files
author
Megan Banaski
committed
formatting and comment updates
1 parent 3323e44 commit fc8502c

File tree

1 file changed

+152
-23
lines changed

1 file changed

+152
-23
lines changed

samples/05_content_publishers/create_and_load_knowledge_graph_backups.ipynb

Lines changed: 152 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@
139139
"outputs": [],
140140
"source": [
141141
"gis_backup = GIS(\"home\") # connect to portal\n",
142+
"# connect to knowledge graph service\n",
142143
"knowledgegraph_backup = KnowledgeGraph(\n",
143144
" \"https://myportal.com/server/rest/services/Hosted/myknowledgegraph/KnowledgeGraphServer\",\n",
144145
" gis=gis_backup,\n",
145-
") # connect to knowledge graph service\n",
146+
")\n",
146147
"try:\n",
147148
" knowledgegraph_backup.datamodel\n",
148149
"except:\n",
@@ -155,7 +156,7 @@
155156
"metadata": {},
156157
"source": [
157158
"## 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"
159160
]
160161
},
161162
{
@@ -165,16 +166,25 @@
165166
"metadata": {},
166167
"outputs": [],
167168
"source": [
168-
"entity_types = []\n",
169169
"# create list of formatted entity types\n",
170+
"entity_types = []\n",
170171
"for types in knowledgegraph_backup.datamodel[\"entity_types\"]:\n",
171172
" curr_entity_type = {\n",
172173
" \"name\": knowledgegraph_backup.datamodel[\"entity_types\"][types][\"name\"],\n",
173174
" \"properties\": knowledgegraph_backup.datamodel[\"entity_types\"][types][\n",
174175
" \"properties\"\n",
175176
" ],\n",
176177
" }\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": [
178188
"# write entity types to json file\n",
179189
"with open(os.path.join(output_folder, dm_ent), \"w\") as f:\n",
180190
" json.dump(entity_types, f)"
@@ -186,7 +196,7 @@
186196
"metadata": {},
187197
"source": [
188198
"## 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"
190200
]
191201
},
192202
{
@@ -196,16 +206,25 @@
196206
"metadata": {},
197207
"outputs": [],
198208
"source": [
199-
"relationship_types = []\n",
200209
"# create list of formatted relationship types\n",
210+
"relationship_types = []\n",
201211
"for types in knowledgegraph_backup.datamodel[\"relationship_types\"]:\n",
202212
" curr_relationship_type = {\n",
203213
" \"name\": knowledgegraph_backup.datamodel[\"relationship_types\"][types][\"name\"],\n",
204214
" \"properties\": knowledgegraph_backup.datamodel[\"relationship_types\"][types][\n",
205215
" \"properties\"\n",
206216
" ],\n",
207217
" }\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": [
209228
"# write relationship types to json file\n",
210229
"with open(os.path.join(output_folder, dm_rel), \"w\") as f:\n",
211230
" json.dump(relationship_types, f)"
@@ -227,9 +246,20 @@
227246
"metadata": {},
228247
"outputs": [],
229248
"source": [
249+
"# query for all entities in graph\n",
230250
"original_entities = knowledgegraph_backup.query_streaming(\n",
231251
" \"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",
233263
"all_entities_fromquery = []\n",
234264
"for entity in list(original_entities):\n",
235265
" curr_entity = entity[0]\n",
@@ -238,10 +268,20 @@
238268
" for prop in curr_entity[\"_properties\"]:\n",
239269
" if type(curr_entity[\"_properties\"][prop]) == UUID:\n",
240270
" 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",
241272
" del curr_entity[\"_properties\"][\n",
242273
" \"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": [
245285
"# write entities list to json file\n",
246286
"with open(os.path.join(output_folder, all_ent), \"w\") as f:\n",
247287
" json.dump(all_entities_fromquery, f)"
@@ -263,9 +303,20 @@
263303
"metadata": {},
264304
"outputs": [],
265305
"source": [
306+
"# query for all relationships in graph\n",
266307
"original_relationships = knowledgegraph_backup.query_streaming(\n",
267308
" \"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",
269320
"all_relationships_fromquery = []\n",
270321
"for relationship in list(original_relationships):\n",
271322
" curr_relationship = relationship[0]\n",
@@ -280,10 +331,20 @@
280331
" curr_relationship[\"_properties\"][prop] = str(\n",
281332
" curr_relationship[\"_properties\"][prop]\n",
282333
" )\n",
334+
" # delete objectid, the server will handle creating new ones when we load the backup\n",
283335
" del curr_relationship[\"_properties\"][\n",
284336
" \"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": [
287348
"# write relationships list to json file\n",
288349
"with open(os.path.join(output_folder, all_rel), \"w\") as f:\n",
289350
" json.dump(all_relationships_fromquery, f)"
@@ -305,9 +366,20 @@
305366
"metadata": {},
306367
"outputs": [],
307368
"source": [
369+
"# query for all provenance records in the graph\n",
308370
"provenance_entities = knowledgegraph_backup.query_streaming(\n",
309371
" \"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",
311383
"all_provenance_fromquery = []\n",
312384
"for entity in list(provenance_entities):\n",
313385
" curr_provenance = entity[0]\n",
@@ -318,10 +390,20 @@
318390
" curr_provenance[\"_properties\"][prop] = str(\n",
319391
" curr_provenance[\"_properties\"][prop]\n",
320392
" )\n",
393+
" # delete objectid, the server will handle creating new ones when we load the backup\n",
321394
" del curr_provenance[\"_properties\"][\n",
322395
" \"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": [
325407
"# write provenance list to json file\n",
326408
"with open(os.path.join(output_folder, prov_file), \"w\") as f:\n",
327409
" json.dump(all_provenance_fromquery, f)"
@@ -351,9 +433,10 @@
351433
"metadata": {},
352434
"outputs": [],
353435
"source": [
436+
"# connect to portal via GIS\n",
354437
"gis_load = GIS(\n",
355438
" \"https://myportal.com/portal\", \"username\", \"password\"\n",
356-
") # connect to portal via GIS\n",
439+
")\n",
357440
"# create a knowledge graph without provenance enabled\n",
358441
"result = gis_load.content.create_service(\n",
359442
" name=\"myknowledgegraph\",\n",
@@ -381,6 +464,7 @@
381464
"metadata": {},
382465
"outputs": [],
383466
"source": [
467+
"# load data model json files into graph data model\n",
384468
"with open(os.path.join(output_folder, dm_ent), \"r\") as file:\n",
385469
" dm_ents = json.load(file)\n",
386470
"with open(os.path.join(output_folder, dm_rel), \"r\") as file:\n",
@@ -411,7 +495,16 @@
411495
"for entity_type in dm_ents:\n",
412496
" for prop in entity_type[\"properties\"]:\n",
413497
" 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": [
415508
"# get document relationship type name\n",
416509
"doc_rel_type_name = \"HasDocument\"\n",
417510
"for relationship_type in dm_rels:\n",
@@ -449,7 +542,16 @@
449542
" prop_list.append(origin_document_properties[prop])\n",
450543
"knowledgegraph_load.graph_property_adds(\n",
451544
" 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": [
453555
"# load any additional document relationship type properties\n",
454556
"for relationship_type in dm_rels:\n",
455557
" if relationship_type[\"name\"] == doc_rel_type_name:\n",
@@ -479,11 +581,20 @@
479581
"outputs": [],
480582
"source": [
481583
"date_properties = []\n",
482-
"# add date property names for entitie types\n",
584+
"# add date property names for entity types\n",
483585
"for types in dm_ents:\n",
484586
" for prop in types[\"properties\"]:\n",
485587
" 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": [
487598
"# add date property names for relationship types\n",
488599
"for types in dm_rels:\n",
489600
" for prop in types[\"properties\"]:\n",
@@ -641,7 +752,16 @@
641752
" prop_list.append(prop)\n",
642753
" knowledgegraph_load.update_search_index(\n",
643754
" 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": [
645765
"# add search indexes for all relationship text properties\n",
646766
"for entity_type in load_dm[\"relationship_types\"]:\n",
647767
" prop_list = []\n",
@@ -674,7 +794,16 @@
674794
"source": [
675795
"# load provenance records json file\n",
676796
"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": [
678807
"# add all provenance records\n",
679808
"for curr_prov in prov_entities:\n",
680809
" # format UUID properties\n",

0 commit comments

Comments
 (0)