Expand description
Google Cloud Client Libraries for Rust - BigQuery
NOTE: While the version is still 0.x, we believe the APIs to be stable.
We plan to release a 1.0 version of this crate in the following release.
We welcome feedback about the APIs, documentation, missing features, bugs, etc.
This crate contains traits, types, and functions to interact with Google Cloud BigQuery. Most applications will use the structs defined in the client module.
For executing queries and managing jobs:
For reading query results efficiently:
For streaming data to BigQuery:
§Example: Executing a Query
// Create a client configured with a default project ID.
let client = BigQuery::builder()
.with_project_id("my-project-id")
.build()
.await?;
// Configure, run, and read query results.
let mut rows = client
.query("SELECT 'hello world' AS greeting")
.until_done()
.await?
.read();
while let Some(row) = rows.next().await.transpose()? {
let greeting: String = row.get("greeting")?;
println!("Greeting: {greeting}");
}§Example: Mapping Rows to Rust Structs
Define typed Rust structs with #[derive(FromRow)] to convert rows
directly into domain types using TryFrom<Row>:
#[derive(FromRow, Debug)]
struct UserStats {
name: String,
number: i64,
}
let mut rows = client
.query("SELECT name, number FROM `bigquery-public-data.usa_names.usa_1910_2013` WHERE state = 'WA' LIMIT 5")
.until_done()
.await?
.read();
while let Some(row) = rows.next().await.transpose()? {
let user: UserStats = row.try_into()?;
println!("{} has count {}", user.name, user.number);
}§Example: Reading from BigQuery
use google_cloud_bigquery::client::Read;
use google_cloud_bigquery::model::{DataFormat, ReadSession};
let client = Read::builder().build().await?;
let session = client
.create_read_session()
.set_parent("projects/my-project")
.set_read_session(
ReadSession::new()
.set_data_format(DataFormat::Arrow)
.set_table("projects/my-project/datasets/my-dataset/tables/my-table"),
)
.set_max_stream_count(1)
.send()
.await?;
for stream in &session.streams {
let mut rows = client
.read_rows()
.set_read_stream(&stream.name)
.send()
.await?;
while let Some(response) = rows.next().await.transpose()? {
println!("Read {} rows", response.row_count);
}
}§Example: Writing to BigQuery
use google_cloud_bigquery::client::Write;
use google_cloud_bigquery::model::{ArrowSchema, ArrowRecordBatch};
let client = Write::builder().build().await?;
let writer = client
.open_default_stream("projects/my-project/datasets/my-dataset/tables/my-table")
.build_arrow(schema())
.await?;
let f1 = writer.append(rows()).send();
let f2 = writer.append(rows()).send();
let _ = f1.await?;
let _ = f2.await?;
fn schema() -> ArrowSchema {
todo!("Define your table's schema...")
}
fn rows() -> ArrowRecordBatch {
todo!("Serialize your rows...")
}Modules§
- builder
- Request and client builders.
- client
- Clients to interact with Google Cloud BigQuery.
- datatypes
- Custom data types for BigQuery.
- error
- Custom errors for the BigQuery clients.
- model
- The messages and enums that are part of this client library.
- model_
ext - Extends crate::model.
- query
- Types related to querying with a BigQuery client.
- stub
- Traits to mock the clients in this library.
- write
- Types related to writing with a Write client.
Structs§
- Error
- The core error returned by all client libraries.
Type Aliases§
- Result
- An alias of std::result::Result where the error is always Error.