Skip to content

Commit

Permalink
formatting and url change
Browse files Browse the repository at this point in the history
  • Loading branch information
mbana641 committed Apr 23, 2024
1 parent 47fcff6 commit ff7b2fc
Showing 1 changed file with 48 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"all_ent = \"all_entities.json\"\n",
"all_rel = \"all_relationships.json\"\n",
"prov_file = \"provenance_entities.json\" # this will only be used if you want to backup provenance records\n",
"servicedef = 'service_definition.json'"
"servicedef = \"service_definition.json\""
]
},
{
Expand Down Expand Up @@ -158,7 +158,7 @@
},
{
"cell_type": "markdown",
"id": "423cbfe2",
"id": "da690f5c",
"metadata": {},
"source": [
"Get the service definition for the knowledge graph service and save it to the service definition file"
Expand All @@ -167,7 +167,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "b24f732f",
"id": "952e670f",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -177,11 +177,15 @@
" \"username\": \"myUsername\",\n",
" \"password\": \"myPassword\",\n",
" \"referer\": \"https://myportal.com/portal\",\n",
" \"f\": \"json\"\n",
" \"f\": \"json\",\n",
"}\n",
"token_response = requests.post(token_url, data=creds, verify=False)\n",
"sd_data = requests.get(url=kg_url, params={'f': 'json', 'token': token_response.json()['token']}, verify=False)\n",
"with open(os.path.join(output_folder, servicedef), 'w') as f:\n",
"sd_data = requests.get(\n",
" url=\"https://myportal.com/server/rest/services/Hosted/myknowledgegraph/KnowledgeGraphServer\",\n",
" params={\"f\": \"json\", \"token\": token_response.json()[\"token\"]},\n",
" verify=False,\n",
")\n",
"with open(os.path.join(output_folder, servicedef), \"w\") as f:\n",
" json.dump(sd_data.text, f)"
]
},
Expand Down Expand Up @@ -282,9 +286,7 @@
"outputs": [],
"source": [
"# query for all entities in graph\n",
"original_entities = knowledgegraph_backup.query_streaming(\n",
" \"MATCH (n) RETURN distinct n\"\n",
")"
"original_entities = knowledgegraph_backup.query_streaming(\"MATCH (n) RETURN distinct n\")"
]
},
{
Expand All @@ -304,9 +306,7 @@
" if type(curr_entity[\"_properties\"][prop]) == UUID:\n",
" curr_entity[\"_properties\"][prop] = str(curr_entity[\"_properties\"][prop])\n",
" # delete objectid, the server will handle creating new ones when we load the backup\n",
" del curr_entity[\"_properties\"][\n",
" \"objectid\"\n",
" ]\n",
" del curr_entity[\"_properties\"][\"objectid\"]\n",
" all_entities_fromquery.append(curr_entity)"
]
},
Expand Down Expand Up @@ -367,9 +367,7 @@
" curr_relationship[\"_properties\"][prop]\n",
" )\n",
" # delete objectid, the server will handle creating new ones when we load the backup\n",
" del curr_relationship[\"_properties\"][\n",
" \"objectid\"\n",
" ]\n",
" del curr_relationship[\"_properties\"][\"objectid\"]\n",
" all_relationships_fromquery.append(curr_relationship)"
]
},
Expand Down Expand Up @@ -397,13 +395,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d4db5b47",
"id": "9f237840",
"metadata": {},
"outputs": [],
"source": [
"# write provenance information to the provenance data model file\n",
"prov_structure = knowledgegraph_backup.datamodel['meta_entity_types']['Provenance']\n",
"with open(os.path.join(output_folder, dm_prov), 'w') as f:\n",
"prov_structure = knowledgegraph_backup.datamodel[\"meta_entity_types\"][\"Provenance\"]\n",
"with open(os.path.join(output_folder, dm_prov), \"w\") as f:\n",
" json.dump(prov_structure, f)"
]
},
Expand Down Expand Up @@ -439,9 +437,7 @@
" curr_provenance[\"_properties\"][prop]\n",
" )\n",
" # delete objectid, the server will handle creating new ones when we load the backup\n",
" del curr_provenance[\"_properties\"][\n",
" \"objectid\"\n",
" ]\n",
" del curr_provenance[\"_properties\"][\"objectid\"]\n",
" all_provenance_fromquery.append(curr_provenance)"
]
},
Expand All @@ -468,7 +464,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "0b62742e",
"id": "fa7d1671",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -538,34 +534,34 @@
"outputs": [],
"source": [
"# connect to portal via GIS\n",
"gis_load = GIS(\n",
" \"https://myportal.com/portal\", \"username\", \"password\"\n",
")\n",
"gis_load = GIS(\"https://myportal.com/portal\", \"username\", \"password\")\n",
"\n",
"with open(os.path.join(output_folder, servicedef), 'r') as file:\n",
"with open(os.path.join(output_folder, servicedef), \"r\") as file:\n",
" service_def = json.load(file)\n",
"service_def = json.loads(service_def)\n",
"updated_service_def = {\n",
" 'name': \"myNewKnowledgeGraph\", #replace with the name for your new knowledge graph\n",
" 'capabilities': service_def['capabilities'],\n",
" 'jsonProperties': {\n",
" 'allowGeometryUpdates': service_def['allowGeometryUpdates'],\n",
" 'searchMaxRecordCount': service_def['searchMaxRecordCount'],\n",
" 'spatialReference': service_def['spatialReference'],\n",
" 'maxRecordCount': service_def['maxRecordCount'],\n",
" 'description': service_def['description'],\n",
" 'copyrightText': service_def['copyrightText'],\n",
" 'documentEntityTypeInfo': {\n",
" \"name\": \"myNewKnowledgeGraph\", # replace with the name for your new knowledge graph\n",
" \"capabilities\": service_def[\"capabilities\"],\n",
" \"jsonProperties\": {\n",
" \"allowGeometryUpdates\": service_def[\"allowGeometryUpdates\"],\n",
" \"searchMaxRecordCount\": service_def[\"searchMaxRecordCount\"],\n",
" \"spatialReference\": service_def[\"spatialReference\"],\n",
" \"maxRecordCount\": service_def[\"maxRecordCount\"],\n",
" \"description\": service_def[\"description\"],\n",
" \"copyrightText\": service_def[\"copyrightText\"],\n",
" \"documentEntityTypeInfo\": {\n",
" \"documentEntityTypeName\": doc_type_name,\n",
" \"hasDocumentsRelationshipTypeName\": doc_rel_type_name\n",
" \"hasDocumentsRelationshipTypeName\": doc_rel_type_name,\n",
" },\n",
" 'supportsDocuments': service_def['supportsDocuments'],\n",
" 'supportsSearch': service_def['supportsSearch'],\n",
" 'supportsProvenance': service_def['supportsProvenance']\n",
" }\n",
" \"supportsDocuments\": service_def[\"supportsDocuments\"],\n",
" \"supportsSearch\": service_def[\"supportsSearch\"],\n",
" \"supportsProvenance\": service_def[\"supportsProvenance\"],\n",
" },\n",
"}\n",
"\n",
"result = gis_copyto.content.create_service(name=\"\",service_type=\"KnowledgeGraph\",create_params=updated_service_def)\n",
"result = gis_copyto.content.create_service(\n",
" name=\"\", service_type=\"KnowledgeGraph\", create_params=updated_service_def\n",
")\n",
"\n",
"knowledgegraph_load = KnowledgeGraph(result.url, gis=gis_load)"
]
Expand Down Expand Up @@ -843,7 +839,9 @@
" prop_list = []\n",
" for prop in load_dm[\"relationship_types\"][relationship_type][\"properties\"]:\n",
" if (\n",
" load_dm[\"relationship_types\"][relationship_type][\"properties\"][prop][\"fieldType\"]\n",
" load_dm[\"relationship_types\"][relationship_type][\"properties\"][prop][\n",
" \"fieldType\"\n",
" ]\n",
" == \"esriFieldTypeString\"\n",
" ):\n",
" prop_list.append(prop)\n",
Expand All @@ -863,7 +861,7 @@
},
{
"cell_type": "markdown",
"id": "9bce8d5b",
"id": "8a6a5ca5",
"metadata": {},
"source": [
"### Add additional provenance entity type properties\n",
Expand All @@ -873,19 +871,18 @@
{
"cell_type": "code",
"execution_count": null,
"id": "e3563b59",
"id": "e663e9fd",
"metadata": {},
"outputs": [],
"source": [
"with open(os.path.join(file_loc, dm_prov), 'r') as file:\n",
" prov_dm = json.load(file)\n",
"with open(os.path.join(file_loc, dm_prov), \"r\") as file:\n",
" prov_dm = json.load(file)\n",
"\n",
"prop_list = []\n",
"for prop in prov_dm['properties']:\n",
" prop_list.append(prov_dm['properties'][prop])\n",
"for prop in prov_dm[\"properties\"]:\n",
" prop_list.append(prov_dm[\"properties\"][prop])\n",
"knowledgegraph_load.graph_property_adds(\n",
" type_name=\"Provenance\",\n",
" graph_properties=prop_list\n",
" type_name=\"Provenance\", graph_properties=prop_list\n",
")\n",
"# errors about already existing properties are expected"
]
Expand Down

0 comments on commit ff7b2fc

Please sign in to comment.