Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/docs/source/src/python/user-guide/io/bigquery.py
7890 views
1
"""
2
# --8<-- [start:read]
3
import polars as pl
4
from google.cloud import bigquery
5
6
client = bigquery.Client()
7
8
# Perform a query.
9
QUERY = (
10
'SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` '
11
'WHERE state = "TX" '
12
'LIMIT 100')
13
query_job = client.query(QUERY) # API request
14
rows = query_job.result() # Waits for query to finish
15
16
df = pl.from_arrow(rows.to_arrow())
17
# --8<-- [end:read]
18
19
# --8<-- [start:write]
20
from google.cloud import bigquery
21
22
client = bigquery.Client()
23
24
# Write DataFrame to stream as parquet file; does not hit disk
25
with io.BytesIO() as stream:
26
df.write_parquet(stream)
27
stream.seek(0)
28
parquet_options = bigquery.ParquetOptions()
29
parquet_options.enable_list_inference = True
30
job = client.load_table_from_file(
31
stream,
32
destination='tablename',
33
project='projectname',
34
job_config=bigquery.LoadJobConfig(
35
source_format=bigquery.SourceFormat.PARQUET,
36
parquet_options=parquet_options,
37
),
38
)
39
job.result() # Waits for the job to complete
40
# --8<-- [end:write]
41
"""
42
43