Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python install bigquery

pip install google-cloud-bigquery
Comment

bigquery data using python

from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

query = """
    SELECT name, SUM(number) as total_people
    FROM `bigquery-public-data.usa_names.usa_1910_2013`
    WHERE state = 'TX'
    GROUP BY name, state
    ORDER BY total_people DESC
    LIMIT 20"""

query_job = client.query(query)  # Make an API request.

df = query_job.to_dataframe(max_results = 1000) # max_results - num of rows to get
print(df.head())
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 :: pandas previous row 
Python :: py string find regex pos 
Python :: pyhton mcq 
Python :: 2)Write a function that checks whether a number is in a given range (inclusive of high and low) python 
Python :: python range function examples 
Python :: receipt data extraction python 
Python :: how to unstack multiindex pandas 
Python :: list of single item repeated python 
Python :: python 3.6 release date 
Python :: pandas qcut 
Python :: time complexity of data structures in python 
Python :: skeppy python 
Python :: how to add keyboard to python turtle 
Python :: python data first column indices 
Python :: get the largest of 2 strings python 
Python :: google codelabs 
Python :: converting multipage tiff to pdf python 
Python :: print(shahzaib) 
Python :: pandas Timedelta to postgres 
Python :: file = Root() path = file.fileDialog() print("PATH = ", path) 
Python :: pypi modules for 3d gui 
Python :: how to update pip in python 
Shell :: restart apache ubuntu 
Shell :: conda statsmodels python 
Shell :: nginx restart ubuntu 
Shell :: install imutils 
Shell :: git undo soft reset 
Shell :: pip install urllib 
Shell :: como instalar telegram ubuntu 
Shell :: The requested apache plugin does not appear to be installed 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =