Skip to content

Commit 08e44b9

Browse files
docs: add bigframes_queries sample snippets (#14429)
* docs: add python_libraries sample snippets * docs: rename python_libraries to bigframes_queries * Update bigquery/bigframes/bigframes_queries.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * docs: update pytest skip reasons for welcome page snippets * docs: fix flake8 lint errors in bigframes_queries * Update bigquery/bigframes/bigframes_queries.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update bigquery/bigframes/bigframes_queries.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * docs: address review comments * style: remove extra blank lines * docs: split bigframes query samples into separate files * feat: add __main__ entrypoints to sample scripts --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 24d6117 commit 08e44b9

9 files changed

Lines changed: 285 additions & 0 deletions

bigquery/bigframes/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
# limitations under the License.
1414

1515
import os
16+
import uuid
1617

18+
from google.cloud import bigquery
1719
import pytest
1820

1921

@@ -25,3 +27,14 @@ def project_id() -> str:
2527
@pytest.fixture(scope="session")
2628
def location() -> str:
2729
return "US"
30+
31+
32+
@pytest.fixture(scope="session")
33+
def dataset_id(project_id: str) -> str:
34+
client = bigquery.Client(project=project_id)
35+
dataset_id = f"bigframes_samples_{uuid.uuid4().hex[:8]}"
36+
dataset = bigquery.Dataset(f"{project_id}.{dataset_id}")
37+
dataset.location = "US"
38+
client.create_dataset(dataset)
39+
yield dataset_id
40+
client.delete_dataset(dataset, delete_contents=True, not_found_ok=True)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START bigquery_bigframes_query_bqstorage]
16+
import bigframes.pandas as bpd
17+
18+
import pandas as pd
19+
20+
# Set partial ordering mode as the default configuration for BigQuery
21+
# DataFrames.
22+
bpd.options.bigquery.ordering_mode = "partial"
23+
24+
25+
def query_bqstorage() -> pd.DataFrame:
26+
sql = """
27+
SELECT name FROM `bigquery-public-data.usa_names.usa_1910_current`
28+
WHERE state = 'TX'
29+
LIMIT 100
30+
"""
31+
32+
# Read query results into a server-side DataFrame without downloading data.
33+
df = bpd.read_gbq(sql)
34+
35+
# When downloading results to an in-memory pandas DataFrame,
36+
# bigquery-dataframes automatically uses the BigQuery Storage API if
37+
# installed.
38+
pandas_df = df.to_pandas()
39+
return pandas_df
40+
# [END bigquery_bigframes_query_bqstorage]
41+
42+
43+
if __name__ == "__main__":
44+
print(query_bqstorage())
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import query_bqstorage
16+
17+
18+
def test_query_bqstorage() -> None:
19+
pandas_df = query_bqstorage.query_bqstorage()
20+
assert pandas_df is not None
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START bigquery_bigframes_query_parameters]
16+
import bigframes.pandas as bpd
17+
18+
# Set partial ordering mode as the default configuration for BigQuery
19+
# DataFrames.
20+
bpd.options.bigquery.ordering_mode = "partial"
21+
22+
23+
def query_parameters() -> bpd.DataFrame:
24+
sql = """
25+
SELECT name FROM `bigquery-public-data.usa_names.usa_1910_current`
26+
WHERE state = @state
27+
LIMIT 100
28+
"""
29+
30+
query_config = {
31+
"query": {
32+
"parameterMode": "NAMED",
33+
"queryParameters": [
34+
{
35+
"name": "state",
36+
"parameterType": {"type": "STRING"},
37+
"parameterValue": {"value": "TX"},
38+
}
39+
],
40+
}
41+
}
42+
43+
df = bpd.read_gbq(sql, configuration=query_config)
44+
return df
45+
# [END bigquery_bigframes_query_parameters]
46+
47+
48+
if __name__ == "__main__":
49+
print(query_parameters())
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import query_parameters
16+
17+
18+
def test_query_parameters() -> None:
19+
df = query_parameters.query_parameters()
20+
assert df is not None
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START bigquery_bigframes_query]
16+
import bigframes.pandas as bpd
17+
18+
# Set partial ordering mode as the default configuration for BigQuery
19+
# DataFrames.
20+
bpd.options.bigquery.ordering_mode = "partial"
21+
22+
23+
def query_standard_sql(project_id: str = "your-project-id") -> bpd.DataFrame:
24+
sql = """
25+
SELECT name FROM `bigquery-public-data.usa_names.usa_1910_current`
26+
WHERE state = 'TX'
27+
LIMIT 100
28+
"""
29+
30+
# Run a query alongside existing SQL. The project will be determined from
31+
# default credentials.
32+
df = bpd.read_gbq(sql)
33+
34+
# Run a query after explicitly specifying a project.
35+
bpd.close_session()
36+
bpd.options.bigquery.project = project_id
37+
df = bpd.read_gbq(sql)
38+
return df
39+
# [END bigquery_bigframes_query]
40+
41+
42+
if __name__ == "__main__":
43+
import os
44+
45+
project = os.environ.get("GOOGLE_CLOUD_PROJECT", "your-project-id")
46+
print(query_standard_sql(project_id=project))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import query_standard_sql
16+
17+
18+
def test_query_standard_sql(project_id: str) -> None:
19+
df = query_standard_sql.query_standard_sql(project_id=project_id)
20+
assert df is not None
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START bigquery_bigframes_upload_from_dataframe]
16+
import bigframes.pandas as bpd
17+
18+
import pandas as pd
19+
20+
# Set partial ordering mode as the default configuration for BigQuery
21+
# DataFrames.
22+
bpd.options.bigquery.ordering_mode = "partial"
23+
24+
25+
def upload_from_dataframe(
26+
table_id: str = "your-project.your_dataset.your_table_name",
27+
) -> bpd.DataFrame:
28+
# Create a local pandas DataFrame.
29+
df = pd.DataFrame(
30+
{
31+
"my_string": ["a", "b", "c"],
32+
"my_int64": [1, 2, 3],
33+
"my_float64": [4.0, 5.0, 6.0],
34+
}
35+
)
36+
37+
# Convert the local pandas DataFrame to a BigQuery DataFrame.
38+
bq_df = bpd.read_pandas(df)
39+
40+
# Write the DataFrame to a BigQuery table.
41+
bq_df.to_gbq(table_id, if_exists="replace")
42+
return bq_df
43+
# [END bigquery_bigframes_upload_from_dataframe]
44+
45+
46+
if __name__ == "__main__":
47+
import os
48+
49+
table_id = os.environ.get(
50+
"TABLE_ID", "your-project.your_dataset.your_table_name"
51+
)
52+
print(upload_from_dataframe(table_id=table_id))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import upload_from_dataframe
16+
17+
18+
def test_upload_from_dataframe(project_id: str, dataset_id: str) -> None:
19+
table_id = f"{project_id}.{dataset_id}.upload_from_dataframe"
20+
bq_df = upload_from_dataframe.upload_from_dataframe(table_id=table_id)
21+
assert bq_df is not None

0 commit comments

Comments
 (0)