Client

Struct Client 

Source
pub struct Client { /* private fields */ }

Implementations§

Source§

impl Client

Source

pub async fn new(config: ClientConfig) -> Result<Self, Error>

New client

Source

pub fn dataset(&self) -> &BigqueryDatasetClient

https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets BigqueryDatasetClient

Source

pub fn table(&self) -> &BigqueryTableClient

https://cloud.google.com/bigquery/docs/reference/rest/v2/tables BigqueryTableClient

Source

pub fn tabledata(&self) -> &BigqueryTabledataClient

https://cloud.google.com/bigquery/docs/reference/rest/v2/tabledata BigqueryTabledataClient

Source

pub fn job(&self) -> &BigqueryJobClient

https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs BigqueryJobClient

Source

pub fn routine(&self) -> &BigqueryRoutineClient

https://cloud.google.com/bigquery/docs/reference/rest/v2/routines BigqueryRoutineClient

Source

pub fn row_access_policy(&self) -> &BigqueryRowAccessPolicyClient

https://cloud.google.com/bigquery/docs/reference/rest/v2/rowAccessPolicy BigqueryRowAccessPolicyClient

Source

pub fn model(&self) -> &BigqueryModelClient

https://cloud.google.com/bigquery/docs/reference/rest/v2/models BigqueryModelClient

Source

pub fn pending_storage_writer(&self, table: &str) -> Writer

Creates a new pending type storage writer for the specified table. https://cloud.google.com/bigquery/docs/write-api#pending_type

use prost_types::DescriptorProto;
use google_cloud_bigquery::client::Client;
use google_cloud_gax::grpc::Status;
use prost::Message;
use tokio::sync::futures;
use google_cloud_bigquery::storage_write::AppendRowsRequestBuilder;
use futures_util::stream::StreamExt;

pub async fn run<T: Message>(client: &Client, table: &str, rows: Vec<T>, schema: DescriptorProto)
-> Result<(), Status> {
    let mut writer = client.pending_storage_writer(table);
    let stream = writer.create_write_stream().await?;

    let mut data= vec![];
    for row in rows {
        let mut buf = Vec::new();
        row.encode(&mut buf).unwrap();
        data.push(buf);
    }
    let mut result = stream.append_rows(vec![AppendRowsRequestBuilder::new(schema, data)]).await.unwrap();
    while let Some(Ok(res)) = result.next().await {
        tracing::info!("append row errors = {:?}", res.row_errors.len());
    }

    let _ = stream.finalize().await?;
    let _ = writer.commit().await?;
    Ok(())
}
Source

pub fn default_storage_writer(&self) -> Writer

Creates a new default type storage writer. https://cloud.google.com/bigquery/docs/write-api#default_stream

use prost_types::DescriptorProto;
use google_cloud_bigquery::client::Client;
use google_cloud_gax::grpc::Status;
use prost::Message;
use tokio::sync::futures;
use google_cloud_bigquery::storage_write::AppendRowsRequestBuilder;
use futures_util::stream::StreamExt;

pub async fn run<T: Message>(client: &Client, table: &str, rows: Vec<T>, schema: DescriptorProto)
-> Result<(), Status> {
    let writer = client.default_storage_writer();
    let stream = writer.create_write_stream(table).await?;

    let mut data= vec![];
    for row in rows {
        let mut buf = Vec::new();
        row.encode(&mut buf).unwrap();
        data.push(buf);
    }
    let mut result = stream.append_rows(vec![AppendRowsRequestBuilder::new(schema, data)]).await.unwrap();
    while let Some(Ok(res)) = result.next().await {
        tracing::info!("append row errors = {:?}", res.row_errors.len());
    }
    Ok(())
}
Source

pub fn committed_storage_writer(&self) -> Writer

Creates a new committed type storage writer. https://cloud.google.com/bigquery/docs/write-api#committed_type

use prost_types::DescriptorProto;
use google_cloud_bigquery::client::Client;
use google_cloud_gax::grpc::Status;
use prost::Message;
use tokio::sync::futures;
use google_cloud_bigquery::storage_write::AppendRowsRequestBuilder;
use futures_util::stream::StreamExt;

pub async fn run<T: Message>(client: &Client, table: &str, rows: Vec<T>, schema: DescriptorProto)
-> Result<(), Status> {
    let writer = client.committed_storage_writer();
    let stream = writer.create_write_stream(table).await?;

    let mut data= vec![];
    for row in rows {
        let mut buf = Vec::new();
        row.encode(&mut buf).unwrap();
        data.push(buf);
    }
    let mut result = stream.append_rows(vec![AppendRowsRequestBuilder::new(schema, data)]).await.unwrap();
    while let Some(Ok(res)) = result.next().await {
        tracing::info!("append row errors = {:?}", res.row_errors.len());
    }

    let _ = stream.finalize().await?;
    Ok(())
}
Source

pub fn buffered_storage_writer(&self) -> Writer

Creates a new buffered type storage writer. https://cloud.google.com/bigquery/docs/write-api#buffered_type

use prost_types::DescriptorProto;
use google_cloud_bigquery::client::Client;
use prost::Message;
use tokio::sync::futures;
use google_cloud_bigquery::storage_write::AppendRowsRequestBuilder;
use futures_util::stream::StreamExt;
use google_cloud_gax::grpc::Status;

pub async fn run<T: Message>(client: &Client, table: &str, rows: Vec<T>, schema: DescriptorProto)
-> Result<(), Status> {
    let writer = client.buffered_storage_writer();
    let stream = writer.create_write_stream(table).await?;

    let mut data= vec![];
    for row in rows {
        let mut buf = Vec::new();
        row.encode(&mut buf).unwrap();
        data.push(buf);
    }
    let mut result = stream.append_rows(vec![AppendRowsRequestBuilder::new(schema, data)]).await.unwrap();
    while let Some(Ok(res)) = result.next().await {
        tracing::info!("append row errors = {:?}", res.row_errors.len());
    }
    let _ = stream.flush_rows(Some(0)).await?;
    let _ = stream.finalize().await?;
    Ok(())
}
Source

pub async fn query<T>( &self, project_id: &str, request: QueryRequest, ) -> Result<Iterator<T>, QueryError>

Run query job and get result.

use google_cloud_bigquery::http::job::query::QueryRequest;
use google_cloud_bigquery::query::row::Row;
use google_cloud_bigquery::client::Client;

async fn run(client: &Client, project_id: &str) {
    let request = QueryRequest {
        query: "SELECT * FROM dataset.table".to_string(),
        ..Default::default()
    };
    let mut iter = client.query::<Row>(project_id, request).await.unwrap();
    while let Some(row) = iter.next().await.unwrap() {
        let col1 = row.column::<String>(0);
        let col2 = row.column::<Option<String>>(1);
    }
}
Source

pub async fn query_with_option<T>( &self, project_id: &str, request: QueryRequest, option: QueryOption, ) -> Result<Iterator<T>, QueryError>

Run query job and get result.

use google_cloud_bigquery::http::job::query::QueryRequest;
use google_cloud_bigquery::query::row::Row;
use google_cloud_bigquery::client::Client;
use google_cloud_bigquery::query::QueryOption;
use google_cloud_bigquery::query::ExponentialBuilder;

async fn run(client: &Client, project_id: &str) {
    let request = QueryRequest {
        query: "SELECT * FROM dataset.table".to_string(),
        ..Default::default()
    };
    let retry = ExponentialBuilder::default().with_max_times(10);
    let option = QueryOption::default().with_retry(retry).with_enable_storage_read(true);
    let mut iter = client.query_with_option::<Row>(project_id, request, option).await.unwrap();
    while let Some(row) = iter.next().await.unwrap() {
        let col1 = row.column::<String>(0);
        let col2 = row.column::<Option<String>>(1);
    }
}
Source

pub async fn read_table<T>( &self, table: &TableReference, option: Option<ReadTableOption>, ) -> Result<Iterator<T>, Error>
where T: StructDecodable,

Read table data by BigQuery Storage Read API.

use google_cloud_bigquery::storage::row::Row;
use google_cloud_bigquery::client::Client;
use google_cloud_bigquery::http::table::TableReference;

async fn run(client: &Client, project_id: &str) {
    let table = TableReference {
        project_id: project_id.to_string(),
        dataset_id: "dataset".to_string(),
        table_id: "table".to_string(),
    };
    let mut iter = client.read_table::<Row>(&table, None).await.unwrap();
    while let Some(row) = iter.next().await.unwrap() {
        let col1 = row.column::<String>(0);
        let col2 = row.column::<Option<String>>(1);
    }
}

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more