Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python install bigquery

pip install google-cloud-bigquery
Comment

python bigquery example

# Here is a sample code from https://cloud.google.com/bigquery/docs/samples/bigquery-add-column-load-append#bigquery_add_column_load_append-python 

# from google.cloud import bigquery
# client = bigquery.Client()
# project = client.project
# dataset_ref = bigquery.DatasetReference(project, 'my_dataset')
# filepath = 'path/to/your_file.csv'

# Retrieves the destination table and checks the length of the schema
table_id = "my_table"
table_ref = dataset_ref.table(table_id)
table = client.get_table(table_ref)
print("Table {} contains {} columns.".format(table_id, len(table.schema)))

# Configures the load job to append the data to the destination table,
# allowing field addition
job_config = bigquery.LoadJobConfig()
job_config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND
job_config.schema_update_options = [
    bigquery.SchemaUpdateOption.ALLOW_FIELD_ADDITION
]
# In this example, the existing table contains only the 'full_name' column.
# 'REQUIRED' fields cannot be added to an existing schema, so the
# additional column must be 'NULLABLE'.
job_config.schema = [
    bigquery.SchemaField("full_name", "STRING", mode="REQUIRED"),
    bigquery.SchemaField("age", "INTEGER", mode="NULLABLE"),
]
job_config.source_format = bigquery.SourceFormat.CSV
job_config.skip_leading_rows = 1

with open(filepath, "rb") as source_file:
    job = client.load_table_from_file(
        source_file,
        table_ref,
        location="US",  # Must match the destination dataset location.
        job_config=job_config,
    )  # API request

job.result()  # Waits for table load to complete.
print(
    "Loaded {} rows into {}:{}.".format(
        job.output_rows, dataset_id, table_ref.table_id
    )
)

# Checks the updated length of the schema
table = client.get_table(table)
print("Table {} now contains {} columns.".format(table_id, len(table.schema)))
Comment

PREVIOUS NEXT
Code Example
Python :: scaling image interpolation python 
Python :: python print do not use scientific notation 
Python :: python how to change size of a window 
Python :: How to set up flash message in html template in flask app 
Python :: python sum of natural numbers recursion 
Python :: python change a key in a dictionary 
Python :: how to count in a loop python 
Python :: get last file in directory python 
Python :: how to add row in spark dataframe 
Python :: round list of floats python 
Python :: sort tuple list python 
Python :: find python path cmd 
Python :: Basic method of Converting List to Dataframe 
Python :: set password on a zip file in python 
Python :: how to create a loop in python turtle 
Python :: author nextcord interactions 
Python :: tkinter input box 
Python :: pytorch use multiple gpu 
Python :: remove a char in a string python 
Python :: urlsplit python 
Python :: python webdriver open with chrome extension 
Python :: filter dataframe 
Python :: if in lambda function python 
Python :: django template tags capitalize 
Python :: python dictionary to csv 
Python :: Python - Count the Number of Keys in a Python Dictionary 
Python :: export_excel file python 
Python :: right angle triangle in python 
Python :: python make dictionary based on list 
Python :: install python packages from inside within python program 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =