You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
asserttype(json_list) ==list, "The JSON string does not represent the expected list"
2848
+
asserttype(json_list) ==list, \
2849
+
"import_json_dump(): the JSON string does not represent a list"
2850
+
2839
2851
2840
2852
id_shifting= {} # To map the Neo4j internal ID's specified in the JSON data dump
2841
2853
# into the ID's of newly-created nodes
2842
2854
2843
-
# Do an initial pass for correctness, to try to avoid partial imports
2844
-
fori, iteminenumerate(json_list):
2845
-
# We use item.get(key_name) to handle without error situation where the key is missing
2846
-
if (item.get("type") !="node") and (item.get("type") !="relationship"):
2847
-
raiseException(f"Item in list index {i} must have a 'type' of either 'node' or 'relationship'. Nothing imported. Item: {item}")
2848
-
2849
-
ifitem["type"] =="node":
2850
-
if"id"notinitem:
2851
-
raiseException(f"Item in list index {i} is marked as 'node' but it lacks an 'id'. Nothing imported. Item: {item}")
2852
-
2853
-
elifitem["type"] =="relationship":
2854
-
if"label"notinitem:
2855
-
raiseException(f"Item in list index {i} is marked as 'relationship' but lacks a 'label'. Nothing imported. Item: {item}")
2856
-
if"start"notinitem:
2857
-
raiseException(f"Item in list index {i} is marked as 'relationship' but lacks a 'start' value. Nothing imported. Item: {item}")
2858
-
if"end"notinitem:
2859
-
raiseException(f"Item in list index {i} is marked as 'relationship' but lacks a 'end' value. Nothing imported. Item: {item}")
2860
-
if"id"notinitem["start"]:
2861
-
raiseException(f"Item in list index {i} is marked as 'relationship' but its 'start' value lacks an 'id'. Nothing imported. Item: {item}")
2862
-
if"id"notinitem["end"]:
2863
-
raiseException(f"Item in list index {i} is marked as 'relationship' but its 'end' value lacks an 'id'. Nothing imported. Item: {item}")
2864
-
2865
-
2866
-
# First, process all the nodes, and in the process create the id_shifting map
2855
+
ifextended_validation:
2856
+
# Do an initial pass for correctness, to help avoid partial imports.
2857
+
# TODO: maybe also check the validity of the start and end nodes of relationships
2858
+
fori, iteminenumerate(json_list):
2859
+
asserttype(item) ==dict, \
2860
+
f"import_json_dump(): Item in list index {i} should be a dict, but instead it's of type {type(item)}. Nothing imported. Item: {item}"
2861
+
# We use item.get(key_name) to handle without error situation where the key is missing
2862
+
if (item.get("type") !="node") and (item.get("type") !="relationship"):
2863
+
raiseException(f"import_json_dump(): Item in list index {i} must be a dict with a 'type' key, "
2864
+
f"whose value is either 'node' or 'relationship'. Nothing imported. Item: {item}")
2865
+
2866
+
ifitem["type"] =="node":
2867
+
if"id"notinitem:
2868
+
raiseException(f"import_json_dump(): Item in list index {i} is marked as 'node' but it lacks an 'id'. Nothing imported. Item: {item}")
2869
+
try:
2870
+
int(item["id"])
2871
+
exceptValueError:
2872
+
raiseException(f"import_json_dump(): Item in list index {i} has an 'id' key whose value ({item['id']}) doesn't correspond to an integer. "
2873
+
f"Nothing imported. Item: {item}")
2874
+
2875
+
elifitem["type"] =="relationship":
2876
+
if"label"notinitem:
2877
+
raiseException(f"import_json_dump(): Item in list index {i} is marked as 'relationship' but lacks a 'label'. Nothing imported. Item: {item}")
2878
+
if"start"notinitem:
2879
+
raiseException(f"import_json_dump(): Item in list index {i} is marked as 'relationship' but lacks a 'start' value. Nothing imported. Item: {item}")
2880
+
if"end"notinitem:
2881
+
raiseException(f"import_json_dump(): Item in list index {i} is marked as 'relationship' but lacks a 'end' value. Nothing imported. Item: {item}")
2882
+
if"id"notinitem["start"]:
2883
+
raiseException(f"import_json_dump(): Item in list index {i} is marked as 'relationship' but its 'start' value lacks an 'id'. Nothing imported. Item: {item}")
2884
+
if"id"notinitem["end"]:
2885
+
raiseException(f"import_json_dump(): Item in list index {i} is marked as 'relationship' but its 'end' value lacks an 'id'. Nothing imported. Item: {item}")
2886
+
2887
+
2888
+
# First, process all the node data, and create the nodes; while doing that, generate the id_shifting map
2867
2889
num_nodes_imported=0
2868
-
foriteminjson_list:
2869
-
ifitem["type"] =="node":
2870
-
#print("ADDING NODE: ", item)
2871
-
#print(f' Creating node with label `{item["labels"][0]}` and properties {item["properties"]}')
2872
-
old_id=int(item["id"])
2873
-
new_id=self.create_node(item["labels"][0], item["properties"]) # TODO: Only the 1st label is used for now
2874
-
id_shifting[old_id] =new_id
2875
-
num_nodes_imported+=1
2890
+
try:
2891
+
foriteminjson_list:
2892
+
ifitem["type"] =="node":
2893
+
#print("ADDING NODE: ", item)
2894
+
#print(f' Creating node with labels `{item["labels"]}` and properties {item["properties"]}')
2895
+
old_id=int(item["id"])
2896
+
new_id=self.create_node(item["labels"], item["properties"]) # Note: any number of labels can be imported
2897
+
id_shifting[old_id] =new_id
2898
+
num_nodes_imported+=1
2899
+
exceptExceptionasex:
2900
+
raiseException(f"import_json_dump(): the import process was INTERRUPTED "
2901
+
f"after importing {num_nodes_imported} node(s) and 0 relationship(s). Reason: "+str(ex))
2902
+
2876
2903
2877
2904
#print("id_shifting map:", id_shifting)
2878
2905
2879
2906
# Then process all the relationships, linking to the correct (newly-created) nodes by using the id_shifting map
2907
+
# (node: item types that aren't either "node" nor "relationship" are currently being ignored during the import)
2880
2908
num_rels_imported=0
2881
-
foriteminjson_list:
2882
-
ifitem["type"] =="relationship":
2883
-
#print("ADDING RELATIONSHIP: ", item)
2884
-
rel_name=item["label"]
2885
-
#rel_props = item["properties"]
2886
-
rel_props=item.get("properties") # Also works if no "properties" is present (relationships may lack it)
2887
-
2888
-
start_id_original=int(item["start"]["id"])
2889
-
end_id_original=int(item["end"]["id"])
2890
-
2891
-
start_id_shifted=id_shifting[start_id_original]
2892
-
end_id_shifted=id_shifting[end_id_original]
2893
-
#print(f' Creating relationship named `{rel_name}` from node {start_id_shifted} to node {end_id_shifted}, with properties {rel_props}')
0 commit comments