Commands to connect to Big query -python
#create client object
client=bigquery.client()
#create reference
dataset_ref=client.dataset(“hacker_news”,project=”bigquery-public-data”)
#api request to fetch the dataset
dataset=client.get_dataset(dataset_ref)
#list all tables using list_tables() function
tables=list(client.list_tables(dataset))
For tables in tables:
Print(table.table_id)
#create reference to full table
table_ref=dataset_ref.table(“full”)
#api request to fetch table
Table=client.get_table(table_ref)
Table.schema
output :- name, field type, mode(nullable), description
#preview first five rows of the “full” table
client.list_rows(table,max_result=5).to_dataframe()
# Preview the first five entries in the "by"
column of the "full" table
client.list_rows(table, selected_fields=table.schema[:1],
max_results=5).to_dataframe()
Triple quotes are used to preserve the format
#create queryjobconfig
object to estimate size
dry_run_config = bigquery.QueryJobConfig(dry_run=True)
#API request – dry run query to estimate costs
dry_run_query_job = client.query(query, job_config=dry_run_config)
print(“This query will process {} bytes.” .format(dry_run_query_job.total_bytes_processed)
#limiting query to 1 mb
one_MB = 1000*1000
safe_config=client.query(query,job_config=safe_config)
safe_query_job.to_dataframe()

Comments
Post a Comment