Dede Soetopo
Dede Soetopo

Reputation: 87

Error load data local CSV to BigQuery using Python

I'm new into data engineering field, and want to create table and inserting the data to BigQuery using Python, but in the process I got error message enter image description here even though I already set the google_application_credential through the shell, the error message still appear

here is my code

from google.cloud import bigquery  
from google.cloud import language
from google.oauth2 import service_account

import os

os.environ["GOOGLE_APPLICATION_CREDENTIAL"]=r"C:/Users/Pamungkas/Downloads/testing-353407-a3c774efeb5a.json"

client = bigquery.Client() 

table_id="testing-353407.testing_field.sales"
file_path=r"C:\Users\Pamungkas\Downloads\sales.csv"

job_config = bigquery.LoadJobConfig(
    source_format=bigquery.SourceFormat.CSV, skip_leading_rows=1, autodetect=True,
        write_disposition=bigquery.WriteDisposition.WRITE_TRUNCATE  #added to have truncate and insert load
)

with open(file_path, "rb") as source_file:
    job = client.load_table_from_file(source_file, table_id, job_config=job_config)
    
job.result()  # Waits for the job to complete.

table = client.get_table(table_id)  # Make an API request.
print(
    "Loaded {} rows and {} columns to {}".format(
        table.num_rows, len(table.schema), table_id
    )
)

Upvotes: 0

Views: 172

Answers (1)

Vishal K
Vishal K

Reputation: 1464

As @p13rr0m suggested, you should have to use the environment variable as GOOGLE_APPLICATION_CREDENTIALS instead of GOOGLE_APPLICATION_CREDENTIAL to resolve your issue.

Upvotes: 1

Related Questions