1

I'm trying to insert csv file into bigquery using python, but I think I have missed something since the result is replace,

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

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=r"C:/Users/Pamungkas/Documents/Dump_Data/testing-353407-a3c774efeb5a.json"

client = bigquery.Client() 

table_id="testing-353407.testing_coba.Sales_Menu_COGS_Detail_Report"
file_path=r"C:\Users\Pamungkas\Downloads\Sales_Menu_COGS_Detail_Report_Jan.csv"

job_config = bigquery.LoadJobConfig(
    source_format=bigquery.SourceFormat.CSV, skip_leading_rows=1, autodetect=True,
        write_disposition=bigquery.WriteDisposition.WRITE_TRUNCATE 
)

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
    )
)

I guess the problem is in job_config, but I still didn't get it, can anyone help me on this?

4
  • What is your expected result? Do you expect to append data? Commented Jun 27, 2022 at 5:53
  • yes, to append the data @RiccoD Commented Jun 27, 2022 at 5:56
  • 2
    Try changing the value of your write_disposition to bigquery.WriteDisposition.WRITE_APPEND Commented Jun 27, 2022 at 5:57
  • Were you able to try it out? Commented Jun 27, 2022 at 7:20

1 Answer 1

3

As mentioned by @RiccoD, since you're appending the data from csv into BigQuery, you'll have to change the write disposition in job config to WRITE_APPEND.

So change the job config part as:

job_config = bigquery.LoadJobConfig(
    source_format=bigquery.SourceFormat.CSV, skip_leading_rows=1, autodetect=True,
        write_disposition=bigquery.WriteDisposition.WRITE_APPEND 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.