@@ -29,12 +29,12 @@ def create_bigquery_dataset(self):
2929 dataset_ref = bigquery .DatasetReference (project_id , dataset_id )
3030 try :
3131 self .bigquery_client .get_dataset (dataset_ref )
32- logging .info (f "Dataset { dataset_id } already exists." )
32+ logging .info ("Dataset %s already exists." , dataset_id )
3333 except Exception :
3434 dataset = bigquery .Dataset (dataset_ref )
3535 dataset .location = dataset_location
3636 self .bigquery_client .create_dataset (dataset )
37- logging .info (f "Created dataset { dataset_id } ." )
37+ logging .info ("Created dataset %s" , dataset_id )
3838
3939 def create_bigquery_table (self ):
4040 """Creates a BigQuery table if it does not exist."""
@@ -43,7 +43,7 @@ def create_bigquery_table(self):
4343
4444 try :
4545 self .bigquery_client .get_table (table_ref )
46- logging .info (f "Table { table_id } already exists." )
46+ logging .info ("Table %s already exists." , table_id )
4747 except Exception :
4848 if self .schema_path is None :
4949 raise Exception ("Schema path is not provided" )
@@ -53,7 +53,10 @@ def create_bigquery_table(self):
5353 table = bigquery .Table (table_ref , schema = schema )
5454 table = self .bigquery_client .create_table (table )
5555 logging .info (
56- f"Created table { table .project } .{ table .dataset_id } .{ table .table_id } "
56+ "Created table %s.%s.%s" ,
57+ table .project ,
58+ table .dataset_id ,
59+ table .table_id ,
5760 )
5861
5962 def load_data_to_bigquery (self ):
@@ -68,7 +71,7 @@ def load_data_to_bigquery(self):
6871 for blob in blobs :
6972 uri = f"gs://{ bucket_name } /{ blob .name } "
7073 source_uris .append (uri )
71- logging .info (f "Found { len ( source_uris ) } files to load to BigQuery." )
74+ logging .info ("Found %s files to load to BigQuery." , len ( source_uris ) )
7275
7376 if len (source_uris ) > 0 :
7477 # Load the data to BigQuery
@@ -82,29 +85,35 @@ def load_data_to_bigquery(self):
8285 try :
8386 load_job .result () # Wait for the job to complete
8487 logging .info (
85- f"Loaded { len (source_uris )} files into "
86- f"{ table_ref .project } .{ table_ref .dataset_id } .{ table_ref .table_id } "
88+ "Loaded %s files into %s.%s.%s." ,
89+ len (source_uris ),
90+ table_ref .project ,
91+ table_ref .dataset_id ,
92+ table_ref .table_id ,
8793 )
8894 # If successful, delete the blobs
8995 for blob in blobs :
9096 blob .delete ()
91- logging .info (f"Deleted blob: { blob .name } " )
97+ logging .debug ("Deleted blob: %s" , blob .name )
98+ logging .info ("Deleted blobs" )
9299 except Exception as e :
93- logging .error (f "An error occurred while loading data to BigQuery: { e } " )
100+ logging .error ("An error occurred while loading data to BigQuery: %s" , e )
94101 for error in load_job .errors :
95- logging .error (f "Error: { error [' message' ] } " )
102+ logging .error ("Error: %s" , error [" message" ] )
96103 if "location" in error :
97- logging .error (f "Location: { error [' location' ] } " )
104+ logging .error ("Location: %s" , error [" location" ] )
98105 if "reason" in error :
99- logging .error (f "Reason: { error [' reason' ] } " )
106+ logging .error ("Reason: %s" , error [" reason" ] )
100107
101108 def send_data_to_bigquery (self ):
102109 """Full process to send data to BigQuery."""
103110 try :
104111 self .create_bigquery_dataset ()
105112 self .create_bigquery_table ()
106113 self .load_data_to_bigquery ()
107- return "Data successfully loaded to BigQuery" , 200
114+ msg = "Data successfully loaded to BigQuery"
115+ logging .info (msg )
116+ return msg , 200
108117 except Exception as e :
109- logging .error (f "An error occurred: { e } " )
118+ logging .error ("An error occurred: %s" , e )
110119 return f"Error while loading data: { e } " , 500
0 commit comments