google_cloud_bigquery/lib.rs
1// Copyright 2025 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// https://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//! Google Cloud Client Libraries for Rust - BigQuery
16//!
17//! **WARNING:** this is a preview release of the crate. We believe the APIs to be
18//! stable. We also are seeking feedback about the APIs and may need to make
19//! breaking changes if we discover that some parts are hard to use.
20//!
21//! We welcome feedback about the APIs, documentation, missing features, bugs, etc.
22//!
23//! This crate contains traits, types, and functions to interact with
24//! [Google Cloud BigQuery][bigquery]. Most applications will use the structs
25//! defined in the [client] module.
26//!
27//! For executing queries and managing jobs:
28//! * [BigQuery][client::BigQuery]
29//!
30//! For streaming data to BigQuery:
31//! * [Write][client::Write]
32//!
33//! [bigquery]: https://cloud.google.com/bigquery
34//!
35//! # Example: Executing a Query
36//!
37//! ```
38//! # use google_cloud_bigquery::client::BigQuery;
39//! # async fn sample() -> anyhow::Result<()> {
40//! // Create a client configured with a default project ID.
41//! let client = BigQuery::builder()
42//! .with_project_id("my-project-id")
43//! .build()
44//! .await?;
45//!
46//! // Configure, run, and read query results.
47//! let mut rows = client
48//! .query("SELECT 'hello world' AS greeting")
49//! .until_done()
50//! .await?
51//! .read();
52//!
53//! while let Some(row) = rows.next().await.transpose()? {
54//! let greeting: String = row.get("greeting");
55//! println!("Greeting: {greeting}");
56//! }
57//! # Ok(())
58//! # }
59//! ```
60//!
61//! # Example: Mapping Rows to Rust Structs
62//!
63//! Define typed Rust structs with `#[derive(FromRow)]` to convert rows
64//! directly into domain types using `TryFrom<Row>`:
65//!
66//! ```
67//! # use google_cloud_bigquery::client::BigQuery;
68//! # use google_cloud_bigquery::query::FromRow;
69//! #[derive(FromRow, Debug)]
70//! struct UserStats {
71//! name: String,
72//! count: i64,
73//! }
74//!
75//! # async fn sample(client: BigQuery) -> anyhow::Result<()> {
76//! let mut rows = client
77//! .query("SELECT name, count FROM `bigquery-public-data.usa_names.usa_1910_2013` WHERE state = 'WA' LIMIT 5")
78//! .until_done()
79//! .await?
80//! .read();
81//!
82//! while let Some(row) = rows.next().await.transpose()? {
83//! let user: UserStats = row.try_into()?;
84//! println!("{} has count {}", user.name, user.count);
85//! }
86//! # Ok(())
87//! # }
88//! ```
89//!
90//! # Example: Writing to BigQuery
91//!
92//! ```
93//! use google_cloud_bigquery::client::Write;
94//! use google_cloud_bigquery::model::{ArrowSchema, ArrowRecordBatch};
95//! # async fn sample() -> anyhow::Result<()> {
96//! let client = Write::builder().build().await?;
97//! let writer = client
98//! .arrow(schema())
99//! .default("projects/my-project/datasets/my-dataset/tables/my-table")?;
100//!
101//! let f1 = writer.append(rows()).send();
102//! let f2 = writer.append(rows()).send();
103//!
104//! let _ = f1.await?;
105//! let _ = f2.await?;
106//! # Ok(()) }
107//!
108//! fn schema() -> ArrowSchema {
109//! todo!("Define your table's schema...")
110//! }
111//! fn rows() -> ArrowRecordBatch {
112//! todo!("Serialize your rows...")
113//! }
114//! ```
115
116pub use google_cloud_gax::Result;
117pub use google_cloud_gax::error::Error;
118
119pub(crate) mod generated;
120
121/// Clients to interact with Google Cloud BigQuery.
122pub mod client {
123 pub use crate::query::client::BigQuery;
124 pub use crate::write::client::Write;
125 // TODO(#6152) - add Write admin client
126}
127
128/// The messages and enums that are part of this client library
129pub mod model {
130 pub(crate) use crate::write::generated::gapic_storage::model::*;
131 pub use crate::write::generated::gapic_storage::model::{
132 ArrowRecordBatch, ArrowSchema, BatchCommitWriteStreamsResponse,
133 FinalizeWriteStreamResponse, FlushRowsResponse, RowError, StorageError, TableFieldSchema,
134 TableSchema, row_error, storage_error, table_field_schema,
135 };
136}
137
138/// Extends [crate::model].
139///
140/// Note that there is no real distinction between the types in `model` and
141/// `model_ext`. The two modules are separate for library maintenance reasons.
142pub mod model_ext {
143 pub use crate::generated::{CompleteQueryMetadata, QueryMetadata, QueryRequest};
144 pub use crate::write::append_response::AppendResponse;
145}
146
147/// Request and client builders.
148pub mod builder {
149 /// Request and client builders for the [BigQuery][crate::client::BigQuery] client.
150 pub mod bigquery {
151 pub use crate::generated::QueryRequest;
152 pub use crate::query::builder::Query;
153 pub use crate::query::client_builder::ClientBuilder;
154 }
155 /// Request and client builders for the [Write][crate::client::Write] client.
156 pub mod write {
157 pub use crate::write::append_builder::{Append, AppendWithOffset};
158 pub use crate::write::client_builder::ClientBuilder;
159 }
160}
161
162/// Custom errors for the BigQuery clients.
163pub mod error;
164
165/// Types related to querying with a [BigQuery][crate::client::BigQuery] client.
166pub mod query;
167
168/// Types related to writing with a [Write][crate::client::Write] client.
169pub mod write;
170
171pub mod datatypes;
172
173pub(crate) use google_cloud_gax::client_builder::Result as ClientBuilderResult;
174pub(crate) use google_cloud_gax::options::RequestOptions;
175pub(crate) use google_cloud_gax::options::internal::RequestBuilder;
176pub(crate) use google_cloud_gax::response::Response;
177
178#[allow(dead_code)]
179pub(crate) mod google {
180 pub mod api {
181 include!("write/generated/protos/storage/google.api.rs");
182 }
183 pub mod cloud {
184 pub mod bigquery {
185 pub mod storage {
186 pub mod v1 {
187 #![allow(deprecated)]
188 include!("write/generated/protos/storage/google.cloud.bigquery.storage.v1.rs");
189 include!("write/generated/convert/storage/convert.rs");
190 }
191 }
192 }
193 }
194 pub mod rpc {
195 include!("write/generated/protos/storage/google.rpc.rs");
196 }
197}