44
55client = weaviate .connect_to_local ()
66
7- print ("=== DEBUG: Downloading data ===" )
8- url = "https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json"
9-
10- try :
11- resp = requests .get (url , timeout = 10 )
12- print (f"DEBUG: Status code: { resp .status_code } " )
13- print (f"DEBUG: Content length: { len (resp .content )} bytes" )
14-
15- if resp .status_code != 200 :
16- print (f"DEBUG: HTTP Error - Response: { resp .text } " )
17- raise Exception (f"HTTP { resp .status_code } " )
18-
19- if not resp .text .strip ():
20- print ("DEBUG: ERROR - Response is empty!" )
21- raise Exception ("Empty response" )
22-
23- print (f"DEBUG: Response preview: { resp .text [:200 ]} ..." )
24-
25- data = json .loads (resp .text )
26- print (f"DEBUG: ✓ JSON parsed successfully, { len (data )} items" )
27-
28- if data and len (data ) > 0 :
29- print (f"DEBUG: First item: { data [0 ]} " )
30-
31- except Exception as e :
32- print (f"DEBUG: ✗ Download failed: { e } " )
33- print ("DEBUG: Using fallback test data" )
34- data = [
35- {"Answer" : "DNA" , "Question" : "What carries genetic information?" , "Category" : "BIOLOGY" },
36- {"Answer" : "Photosynthesis" , "Question" : "Process plants use to make food?" , "Category" : "BIOLOGY" },
37- {"Answer" : "Mitochondria" , "Question" : "Powerhouse of the cell?" , "Category" : "BIOLOGY" }
38- ]
39-
40- print ("=== DEBUG: Starting batch import ===" )
41- questions = client .collections .get ("Question" )
42-
43- if not data :
44- print ("DEBUG: ✗ No data to import" )
45- client .close ()
46- exit (1 )
47-
48- print (f"DEBUG: About to import { len (data )} objects" )
7+ resp = requests .get (
8+ "https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json"
9+ )
10+ data = json .loads (resp .text )
4911
5012# highlight-start
51- successful_imports = 0
52- failed_imports = 0
13+ questions = client .collections .get ("Question" )
5314
5415with questions .batch .fixed_size (batch_size = 200 ) as batch :
55- for i , item in enumerate (data ):
56- try :
57- batch .add_object ({
58- "answer" : item ["Answer" ],
59- "question" : item ["Question" ],
60- "category" : item ["Category" ],
61- })
62- successful_imports += 1
63-
64- if (i + 1 ) % 10 == 0 :
65- print (f"DEBUG: Processed { i + 1 } /{ len (data )} items" )
66-
67- except KeyError as ke :
68- failed_imports += 1
69- print (f"DEBUG: ✗ Item { i } missing key { ke } : { item } " )
70- except Exception as e :
71- failed_imports += 1
72- print (f"DEBUG: ✗ Error with item { i } : { e } " )
73-
16+ for d in data :
17+ batch .add_object (
18+ {
19+ "answer" : d ["Answer" ],
20+ "question" : d ["Question" ],
21+ "category" : d ["Category" ],
22+ }
23+ )
7424 # highlight-end
7525 if batch .number_errors > 10 :
76- print (f"DEBUG: ✗ Too many batch errors: { batch . number_errors } " )
26+ print ("Batch import stopped due to excessive errors. " )
7727 break
7828
79- print (f"DEBUG: Import attempt completed - Success: { successful_imports } , Failed: { failed_imports } " )
80-
8129failed_objects = questions .batch .failed_objects
8230if failed_objects :
83- print (f"DEBUG: ✗ Batch failed objects: { len (failed_objects )} " )
84- if len (failed_objects ) > 0 :
85- print (f"DEBUG: First failed: { failed_objects [0 ]} " )
86- else :
87- print ("DEBUG: ✓ No batch failures" )
31+ print (f"Number of failed imports: { len (failed_objects )} " )
32+ print (f"First failed object: { failed_objects [0 ]} " )
8833
89- # Verify import
90- print ("=== DEBUG: Verifying import ===" )
91- try :
92- aggregate_result = questions .aggregate .over_all (total_count = True )
93- total_count = aggregate_result .total_count
94- print (f"DEBUG: ✓ Total objects in collection: { total_count } " )
95-
96- if total_count > 0 :
97- sample_objects = questions .query .fetch_objects (limit = 2 )
98- print (f"DEBUG: Sample objects:" )
99- for obj in sample_objects .objects :
100- print (f" - { obj .properties } " )
101-
102- except Exception as e :
103- print (f"DEBUG: ✗ Verification error: { e } " )
34+ client .close () # Free up resources
35+ # END Import
10436
105- client .close ()
106- print ("DEBUG: Import completed" )
107- # END Import
0 commit comments