go: cloud.google.com/go/bigquery Index | Examples | Files

package bigquery

import "cloud.google.com/go/bigquery"

Package bigquery provides a client for the BigQuery service.

Note: This package is in beta. Some backwards-incompatible changes may occur.

The following assumes a basic familiarity with BigQuery concepts. See https://cloud.google.com/bigquery/docs.

Creating a Client

To start working with this package, create a client:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, projectID)
if err != nil {
    // TODO: Handle error.
}

Querying

To query existing tables, create a Query and call its Read method:

q := client.Query(`
    SELECT year, SUM(number) as num
    FROM [bigquery-public-data:usa_names.usa_1910_2013]
    WHERE name = "William"
    GROUP BY year
    ORDER BY year
`)
it, err := q.Read(ctx)
if err != nil {
    // TODO: Handle error.
}

Then iterate through the resulting rows. You can store a row using anything that implements the ValueLoader interface, or with a slice or map of bigquery.Value. A slice is simplest:

for {
    var values []bigquery.Value
    err := it.Next(&values)
    if err == iterator.Done {
        break
    }
    if err != nil {
        // TODO: Handle error.
    }
    fmt.Println(values)
}

You can also use a struct whose exported fields match the query:

type Count struct {
    Year int
    Num  int
}
for {
    var c Count
    err := it.Next(&c)
    if err == iterator.Done {
        break
    }
    if err != nil {
        // TODO: Handle error.
    }
    fmt.Println(c)
}

You can also start the query running and get the results later. Create the query as above, but call Run instead of Read. This returns a Job, which represents an asychronous operation.

job, err := q.Run(ctx)
if err != nil {
    // TODO: Handle error.
}

Get the job's ID, a printable string. You can save this string to retrieve the results at a later time, even in another process.

jobID := job.ID()
fmt.Printf("The job ID is %s\n", jobID)

To retrieve the job's results from the ID, first look up the Job:

job, err = client.JobFromID(ctx, jobID)
if err != nil {
    // TODO: Handle error.
}

Use the Job.Read method to obtain an iterator, and loop over the rows. Query.Read is just a convenience method that combines Query.Run and Job.Read.

it, err = job.Read(ctx)
if err != nil {
    // TODO: Handle error.
}
// Proceed with iteration as above.

Datasets and Tables

You can refer to datasets in the client's project with the Dataset method, and in other projects with the DatasetInProject method:

myDataset := client.Dataset("my_dataset")
yourDataset := client.DatasetInProject("your-project-id", "your_dataset")

These methods create references to datasets, not the datasets themselves. You can have a dataset reference even if the dataset doesn't exist yet. Use Dataset.Create to create a dataset from a reference:

if err := myDataset.Create(ctx); err != nil {
    // TODO: Handle error.
}

You can refer to tables with Dataset.Table. Like bigquery.Dataset, bigquery.Table is a reference to an object in BigQuery that may or may not exist.

table := myDataset.Table("my_table")

You can create, delete and update the metadata of tables with methods on Table. Table.Create supports a few options. For instance, you could create a temporary table with:

err = myDataset.Table("temp").Create(ctx, bigquery.TableExpiration(time.Now().Add(1*time.Hour)))
if err != nil {
    // TODO: Handle error.
}

We'll see how to create a table with a schema in the next section.

Schemas

There are two ways to construct schemas with this package. You can build a schema by hand, like so:

schema1 := bigquery.Schema{
    &bigquery.FieldSchema{Name: "Name", Required: true, Type: bigquery.StringFieldType},
    &bigquery.FieldSchema{Name: "Grades", Repeated: true, Type: bigquery.IntegerFieldType},
}

Or you can infer the schema from a struct:

type student struct {
    Name   string
    Grades []int
}
schema2, err := bigquery.InferSchema(student{})
if err != nil {
    // TODO: Handle error.
}
// schema1 and schema2 are identical.

Struct inference supports tags like those of the encoding/json package, so you can change names or ignore fields:

type student2 struct {
    Name   string `bigquery:"full_name"`
    Grades []int
    Secret string `bigquery:"-"`
}
schema3, err := bigquery.InferSchema(student2{})
if err != nil {
    // TODO: Handle error.
}
// schema3 has fields "full_name" and "Grade".

Having constructed a schema, you can pass it to Table.Create as an option:

if err := table.Create(ctx, schema1); err != nil {
    // TODO: Handle error.
}

Copying

You can copy one or more tables to another table. Begin by constructing a Copier describing the copy. Then set any desired copy options, and finally call Run to get a Job:

copier := myDataset.Table("dest").CopierFrom(myDataset.Table("src"))
copier.WriteDisposition = bigquery.WriteTruncate
job, err = copier.Run(ctx)
if err != nil {
    // TODO: Handle error.
}

You can chain the call to Run if you don't want to set options:

job, err = myDataset.Table("dest").CopierFrom(myDataset.Table("src")).Run(ctx)
if err != nil {
    // TODO: Handle error.
}

You can wait for your job to complete:

status, err := job.Wait(ctx)
if err != nil {
    // TODO: Handle error.
}

Job.Wait polls with exponential backoff. You can also poll yourself, if you wish:

for {
    status, err := job.Status(ctx)
    if err != nil {
        // TODO: Handle error.
    }
    if status.Done() {
        if status.Err() != nil {
            log.Fatalf("Job failed with error %v", status.Err())
        }
        break
    }
    time.Sleep(pollInterval)
}

Loading and Uploading

There are two ways to populate a table with this package: load the data from a Google Cloud Storage object, or upload rows directly from your program.

For loading, first create a GCSReference, configuring it if desired. Then make a Loader, optionally configure it as well, and call its Run method.

gcsRef := bigquery.NewGCSReference("gs://my-bucket/my-object")
gcsRef.AllowJaggedRows = true
loader := myDataset.Table("dest").LoaderFrom(gcsRef)
loader.CreateDisposition = bigquery.CreateNever
job, err = loader.Run(ctx)
// Poll the job for completion if desired, as above.

To upload, first define a type that implements the ValueSaver interface, which has a single method named Save. Then create an Uploader, and call its Put method with a slice of values.

u := table.Uploader()
// Item implements the ValueSaver interface.
items := []*Item{
    {Name: "n1", Size: 32.6, Count: 7},
    {Name: "n2", Size: 4, Count: 2},
    {Name: "n3", Size: 101.5, Count: 1},
}
if err := u.Put(ctx, items); err != nil {
    // TODO: Handle error.
}

You can also upload a struct that doesn't implement ValueSaver. Use the StructSaver type to specify the schema and insert ID by hand, or just supply the struct or struct pointer directly and the schema will be inferred:

type Item2 struct {
    Name  string
    Size  float64
    Count int
}
// Item implements the ValueSaver interface.
items2 := []*Item2{
    {Name: "n1", Size: 32.6, Count: 7},
    {Name: "n2", Size: 4, Count: 2},
    {Name: "n3", Size: 101.5, Count: 1},
}
if err := u.Put(ctx, items2); err != nil {
    // TODO: Handle error.
}

Extracting

If you've been following so far, extracting data from a BigQuery table into a Google Cloud Storage object will feel familiar. First create an Extractor, then optionally configure it, and lastly call its Run method.

extractor := table.ExtractorTo(gcsRef)
extractor.DisableHeader = true
job, err = extractor.Run(ctx)
// Poll the job for completion if desired, as above.

Authentication

See examples of authorization and authentication at https://godoc.org/cloud.google.com/go#pkg-examples.

Index

Examples

Package Files

bigquery.go copy.go dataset.go doc.go error.go extract.go file.go gcs.go iterator.go job.go load.go params.go query.go schema.go service.go table.go uploader.go value.go

Constants

const Scope = "https://www.googleapis.com/auth/bigquery"

type Client Uses

type Client struct {
    // contains filtered or unexported fields
}

Client may be used to perform BigQuery operations.

func NewClient Uses

func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error)

NewClient constructs a new Client which can perform BigQuery operations. Operations performed via the client are billed to the specified GCP project.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
_ = client // TODO: Use client.

func (*Client) Close Uses

func (c *Client) Close() error

Close closes any resources held by the client. Close should be called when the client is no longer needed. It need not be called at program exit.

func (*Client) Dataset Uses

func (c *Client) Dataset(id string) *Dataset

Dataset creates a handle to a BigQuery dataset in the client's project.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
ds := client.Dataset("my_dataset")
fmt.Println(ds)

func (*Client) DatasetInProject Uses

func (c *Client) DatasetInProject(projectID, datasetID string) *Dataset

DatasetInProject creates a handle to a BigQuery dataset in the specified project.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
ds := client.DatasetInProject("their-project-id", "their-dataset")
fmt.Println(ds)

func (*Client) Datasets Uses

func (c *Client) Datasets(ctx context.Context) *DatasetIterator

Datasets returns an iterator over the datasets in a project. The Client's project is used by default, but that can be changed by setting ProjectID on the returned iterator before calling Next.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
it := client.Datasets(ctx)
_ = it // TODO: iterate using Next or iterator.Pager.

func (*Client) DatasetsInProject Uses

func (c *Client) DatasetsInProject(ctx context.Context, projectID string) *DatasetIterator

DatasetsInProject returns an iterator over the datasets in the provided project.

Deprecated: call Client.Datasets, then set ProjectID on the returned iterator.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
it := client.DatasetsInProject(ctx, "their-project-id")
_ = it // TODO: iterate using Next or iterator.Pager.

func (*Client) JobFromID Uses

func (c *Client) JobFromID(ctx context.Context, id string) (*Job, error)

JobFromID creates a Job which refers to an existing BigQuery job. The job need not have been created by this package. For example, the job may have been created in the BigQuery console.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
jobID := getJobID() // Get a job ID using Job.ID, the console or elsewhere.
job, err := client.JobFromID(ctx, jobID)
if err != nil {
    // TODO: Handle error.
}
fmt.Println(job)

func (*Client) Jobs Uses

func (c *Client) Jobs(ctx context.Context) *JobIterator

Jobs lists jobs within a project.

func (*Client) Query Uses

func (c *Client) Query(q string) *Query

Query creates a query with string q. The returned Query may optionally be further configured before its Run method is called.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
q := client.Query("select name, num from t1")
q.DefaultProjectID = "project-id"
// TODO: set other options on the Query.
// TODO: Call Query.Run or Query.Read.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
q := client.Query("select num from t1 where name = @user")
q.Parameters = []bigquery.QueryParameter{
    {Name: "user", Value: "Elizabeth"},
}
// TODO: set other options on the Query.
// TODO: Call Query.Run or Query.Read.

type Compression Uses

type Compression string

Compression is the type of compression to apply when writing data to Google Cloud Storage.

const (
    None Compression = "NONE"
    Gzip Compression = "GZIP"
)

type Copier Uses

type Copier struct {
    CopyConfig
    // contains filtered or unexported fields
}

A Copier copies data into a BigQuery table from one or more BigQuery tables.

func (*Copier) Run Uses

func (c *Copier) Run(ctx context.Context) (*Job, error)

Run initiates a copy job.

type CopyConfig Uses

type CopyConfig struct {
    // JobID is the ID to use for the job. If empty, a random job ID will be generated.
    JobID string

    // If AddJobIDSuffix is true, then a random string will be appended to JobID.
    AddJobIDSuffix bool

    // Srcs are the tables from which data will be copied.
    Srcs []*Table

    // Dst is the table into which the data will be copied.
    Dst *Table

    // CreateDisposition specifies the circumstances under which the destination table will be created.
    // The default is CreateIfNeeded.
    CreateDisposition TableCreateDisposition

    // WriteDisposition specifies how existing data in the destination table is treated.
    // The default is WriteAppend.
    WriteDisposition TableWriteDisposition
}

CopyConfig holds the configuration for a copy job.

type CreateTableOption Uses

type CreateTableOption interface {
    // contains filtered or unexported methods
}

A CreateTableOption is an optional argument to CreateTable.

func TableExpiration Uses

func TableExpiration(exp time.Time) CreateTableOption

TableExpiration returns a CreateTableOption that will cause the created table to be deleted after the expiration time.

func UseLegacySQL Uses

func UseLegacySQL() CreateTableOption

UseLegacySQL returns a CreateTableOption to set the table to use legacy SQL. This is currently the default.

func UseStandardSQL Uses

func UseStandardSQL() CreateTableOption

UseStandardSQL returns a CreateTableOption to set the table to use standard SQL. The default setting is false (using legacy SQL).

func ViewQuery Uses

func ViewQuery(query string) CreateTableOption

ViewQuery returns a CreateTableOption that causes the created table to be a virtual table defined by the supplied query. For more information see: https://cloud.google.com/bigquery/querying-data#views

type DataFormat Uses

type DataFormat string

DataFormat describes the format of BigQuery table data.

const (
    CSV             DataFormat = "CSV"
    Avro            DataFormat = "AVRO"
    JSON            DataFormat = "NEWLINE_DELIMITED_JSON"
    DatastoreBackup DataFormat = "DATASTORE_BACKUP"
)

Constants describing the format of BigQuery table data.

type Dataset Uses

type Dataset struct {
    ProjectID string
    DatasetID string
    // contains filtered or unexported fields
}

Dataset is a reference to a BigQuery dataset.

func (*Dataset) Create Uses

func (d *Dataset) Create(ctx context.Context, md *DatasetMetadata) error

Create creates a dataset in the BigQuery service. An error will be returned if the dataset already exists. Pass in a DatasetMetadata value to configure the dataset.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
ds := client.Dataset("my_dataset")
if err := ds.Create(ctx, &bigquery.DatasetMetadata{Location: "EU"}); err != nil {
    // TODO: Handle error.
}

func (*Dataset) Delete Uses

func (d *Dataset) Delete(ctx context.Context) error

Delete deletes the dataset.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
if err := client.Dataset("my_dataset").Delete(ctx); err != nil {
    // TODO: Handle error.
}

func (*Dataset) Metadata Uses

func (d *Dataset) Metadata(ctx context.Context) (*DatasetMetadata, error)

Metadata fetches the metadata for the dataset.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
md, err := client.Dataset("my_dataset").Metadata(ctx)
if err != nil {
    // TODO: Handle error.
}
fmt.Println(md)

func (*Dataset) Table Uses

func (d *Dataset) Table(tableID string) *Table

Table creates a handle to a BigQuery table in the dataset. To determine if a table exists, call Table.Metadata. If the table does not already exist, use Table.Create to create it.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
// Table creates a reference to the table. It does not create the actual
// table in BigQuery; to do so, use Table.Create.
t := client.Dataset("my_dataset").Table("my_table")
fmt.Println(t)

func (*Dataset) Tables Uses

func (d *Dataset) Tables(ctx context.Context) *TableIterator

Tables returns an iterator over the tables in the Dataset.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
it := client.Dataset("my_dataset").Tables(ctx)
_ = it // TODO: iterate using Next or iterator.Pager.

func (*Dataset) Update Uses

func (d *Dataset) Update(ctx context.Context, dm DatasetMetadataToUpdate, etag string) (*DatasetMetadata, error)

Update modifies specific Dataset metadata fields. To perform a read-modify-write that protects against intervening reads, set the etag argument to the DatasetMetadata.ETag field from the read. Pass the empty string for etag for a "blind write" that will always succeed.

To perform a blind write, ignoring the existing state (and possibly overwriting other updates), pass the empty string as the etag.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
md, err := client.Dataset("my_dataset").Update(ctx, bigquery.DatasetMetadataToUpdate{Name: "blind"}, "")
if err != nil {
    // TODO: Handle error.
}
fmt.Println(md)

This example illustrates how to perform a read-modify-write sequence on dataset metadata. Passing the metadata's ETag to the Update call ensures that the call will fail if the metadata was changed since the read.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
ds := client.Dataset("my_dataset")
md, err := ds.Metadata(ctx)
if err != nil {
    // TODO: Handle error.
}
md2, err := ds.Update(ctx,
    bigquery.DatasetMetadataToUpdate{Name: "new " + md.Name},
    md.ETag)
if err != nil {
    // TODO: Handle error.
}
fmt.Println(md2)

type DatasetIterator Uses

type DatasetIterator struct {
    // ListHidden causes hidden datasets to be listed when set to true.
    // Set before the first call to Next.
    ListHidden bool

    // Filter restricts the datasets returned by label. The filter syntax is described in
    // https://cloud.google.com/bigquery/docs/labeling-datasets#filtering_datasets_using_labels
    // Set before the first call to Next.
    Filter string

    // The project ID of the listed datasets.
    // Set before the first call to Next.
    ProjectID string
    // contains filtered or unexported fields
}

DatasetIterator iterates over the datasets in a project.

func (*DatasetIterator) Next Uses

func (it *DatasetIterator) Next() (*Dataset, error)

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
it := client.Datasets(ctx)
for {
    ds, err := it.Next()
    if err == iterator.Done {
        break
    }
    if err != nil {
        // TODO: Handle error.
    }
    fmt.Println(ds)
}

func (*DatasetIterator) PageInfo Uses

func (it *DatasetIterator) PageInfo() *iterator.PageInfo

PageInfo supports pagination. See the google.golang.org/api/iterator package for details.

type DatasetMetadata Uses

type DatasetMetadata struct {
    // These fields can be set when creating a dataset.
    Name                   string            // The user-friendly name for this dataset.
    Description            string            // The user-friendly description of this dataset.
    Location               string            // The geo location of the dataset.
    DefaultTableExpiration time.Duration     // The default expiration time for new tables.
    Labels                 map[string]string // User-provided labels.

    // These fields are read-only.
    CreationTime     time.Time
    LastModifiedTime time.Time // When the dataset or any of its tables were modified.
    FullID           string    // The full dataset ID in the form projectID:datasetID.

    // ETag is the ETag obtained when reading metadata. Pass it to Dataset.Update to
    // ensure that the metadata hasn't changed since it was read.
    ETag string
}

DatasetMetadata contains information about a BigQuery dataset.

type DatasetMetadataToUpdate Uses

type DatasetMetadataToUpdate struct {
    Description optional.String // The user-friendly description of this table.
    Name        optional.String // The user-friendly name for this dataset.
    // DefaultTableExpiration is the the default expiration time for new tables.
    // If set to time.Duration(0), new tables never expire.
    DefaultTableExpiration optional.Duration
    // contains filtered or unexported fields
}

DatasetMetadataToUpdate is used when updating a dataset's metadata. Only non-nil fields will be updated.

func (*DatasetMetadataToUpdate) DeleteLabel Uses

func (dm *DatasetMetadataToUpdate) DeleteLabel(name string)

DeleteLabel causes a label to be deleted when dm is used in a call to Dataset.Update.

func (*DatasetMetadataToUpdate) SetLabel Uses

func (dm *DatasetMetadataToUpdate) SetLabel(name, value string)

SetLabel causes a label to be added or modified when dm is used in a call to Dataset.Update.

type Encoding Uses

type Encoding string

Encoding specifies the character encoding of data to be loaded into BigQuery. See https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.load.encoding for more details about how this is used.

const (
    UTF_8      Encoding = "UTF-8"
    ISO_8859_1 Encoding = "ISO-8859-1"
)

type Error Uses

type Error struct {
    // Mirrors bq.ErrorProto, but drops DebugInfo
    Location, Message, Reason string
}

An Error contains detailed information about a failed bigquery operation.

func (Error) Error Uses

func (e Error) Error() string

type ExplainQueryStage Uses

type ExplainQueryStage struct {
    // Relative amount of the total time the average shard spent on CPU-bound tasks.
    ComputeRatioAvg float64

    // Relative amount of the total time the slowest shard spent on CPU-bound tasks.
    ComputeRatioMax float64

    // Unique ID for stage within plan.
    ID  int64

    // Human-readable name for stage.
    Name string

    // Relative amount of the total time the average shard spent reading input.
    ReadRatioAvg float64

    // Relative amount of the total time the slowest shard spent reading input.
    ReadRatioMax float64

    // Number of records read into the stage.
    RecordsRead int64

    // Number of records written by the stage.
    RecordsWritten int64

    // Current status for the stage.
    Status string

    // List of operations within the stage in dependency order (approximately
    // chronological).
    Steps []*ExplainQueryStep

    // Relative amount of the total time the average shard spent waiting to be scheduled.
    WaitRatioAvg float64

    // Relative amount of the total time the slowest shard spent waiting to be scheduled.
    WaitRatioMax float64

    // Relative amount of the total time the average shard spent on writing output.
    WriteRatioAvg float64

    // Relative amount of the total time the slowest shard spent on writing output.
    WriteRatioMax float64
}

ExplainQueryStage describes one stage of a query.

type ExplainQueryStep Uses

type ExplainQueryStep struct {
    // Machine-readable operation type.
    Kind string

    // Human-readable stage descriptions.
    Substeps []string
}

ExplainQueryStep describes one step of a query stage.

type ExternalData Uses

type ExternalData interface {
    // contains filtered or unexported methods
}

ExternalData is a table which is stored outside of BigQuery. It is implemented by GCSReference.

type ExtractConfig Uses

type ExtractConfig struct {
    // JobID is the ID to use for the job. If empty, a random job ID will be generated.
    JobID string

    // If AddJobIDSuffix is true, then a random string will be appended to JobID.
    AddJobIDSuffix bool

    // Src is the table from which data will be extracted.
    Src *Table

    // Dst is the destination into which the data will be extracted.
    Dst *GCSReference

    // DisableHeader disables the printing of a header row in exported data.
    DisableHeader bool
}

ExtractConfig holds the configuration for an extract job.

type ExtractStatistics Uses

type ExtractStatistics struct {
    // The number of files per destination URI or URI pattern specified in the
    // extract configuration. These values will be in the same order as the
    // URIs specified in the 'destinationUris' field.
    DestinationURIFileCounts []int64
}

ExtractStatistics contains statistics about an extract job.

type Extractor Uses

type Extractor struct {
    ExtractConfig
    // contains filtered or unexported fields
}

An Extractor extracts data from a BigQuery table into Google Cloud Storage.

func (*Extractor) Run Uses

func (e *Extractor) Run(ctx context.Context) (*Job, error)

Run initiates an extract job.

type FieldSchema Uses

type FieldSchema struct {
    // The field name.
    // Must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_),
    // and must start with a letter or underscore.
    // The maximum length is 128 characters.
    Name string

    // A description of the field. The maximum length is 16,384 characters.
    Description string

    // Whether the field may contain multiple values.
    Repeated bool
    // Whether the field is required.  Ignored if Repeated is true.
    Required bool

    // The field data type.  If Type is Record, then this field contains a nested schema,
    // which is described by Schema.
    Type FieldType
    // Describes the nested schema if Type is set to Record.
    Schema Schema
}

type FieldType Uses

type FieldType string
const (
    StringFieldType    FieldType = "STRING"
    BytesFieldType     FieldType = "BYTES"
    IntegerFieldType   FieldType = "INTEGER"
    FloatFieldType     FieldType = "FLOAT"
    BooleanFieldType   FieldType = "BOOLEAN"
    TimestampFieldType FieldType = "TIMESTAMP"
    RecordFieldType    FieldType = "RECORD"
    DateFieldType      FieldType = "DATE"
    TimeFieldType      FieldType = "TIME"
    DateTimeFieldType  FieldType = "DATETIME"
)

type FileConfig Uses

type FileConfig struct {
    // SourceFormat is the format of the GCS data to be read.
    // Allowed values are: CSV, Avro, JSON, DatastoreBackup.  The default is CSV.
    SourceFormat DataFormat

    // FieldDelimiter is the separator for fields in a CSV file, used when
    // reading or exporting data. The default is ",".
    FieldDelimiter string

    // The number of rows at the top of a CSV file that BigQuery will skip when
    // reading data.
    SkipLeadingRows int64

    // AllowJaggedRows causes missing trailing optional columns to be tolerated
    // when reading CSV data. Missing values are treated as nulls.
    AllowJaggedRows bool

    // AllowQuotedNewlines sets whether quoted data sections containing
    // newlines are allowed when reading CSV data.
    AllowQuotedNewlines bool

    // Indicates if we should automatically infer the options and
    // schema for CSV and JSON sources.
    AutoDetect bool

    // Encoding is the character encoding of data to be read.
    Encoding Encoding

    // MaxBadRecords is the maximum number of bad records that will be ignored
    // when reading data.
    MaxBadRecords int64

    // IgnoreUnknownValues causes values not matching the schema to be
    // tolerated. Unknown values are ignored. For CSV this ignores extra values
    // at the end of a line. For JSON this ignores named values that do not
    // match any column name. If this field is not set, records containing
    // unknown values are treated as bad records. The MaxBadRecords field can
    // be used to customize how bad records are handled.
    IgnoreUnknownValues bool

    // Schema describes the data. It is required when reading CSV or JSON data,
    // unless the data is being loaded into a table that already exists.
    Schema Schema

    // Quote is the value used to quote data sections in a CSV file. The
    // default quotation character is the double quote ("), which is used if
    // both Quote and ForceZeroQuote are unset.
    // To specify that no character should be interpreted as a quotation
    // character, set ForceZeroQuote to true.
    // Only used when reading data.
    Quote          string
    ForceZeroQuote bool
}

FileConfig contains configuration options that pertain to files, typically text files that require interpretation to be used as a BigQuery table. A file may live in Google Cloud Storage (see GCSReference), or it may be loaded into a table via the Table.LoaderFromReader.

type GCSReference Uses

type GCSReference struct {
    FileConfig

    // DestinationFormat is the format to use when writing exported files.
    // Allowed values are: CSV, Avro, JSON.  The default is CSV.
    // CSV is not supported for tables with nested or repeated fields.
    DestinationFormat DataFormat

    // Compression specifies the type of compression to apply when writing data
    // to Google Cloud Storage, or using this GCSReference as an ExternalData
    // source with CSV or JSON SourceFormat. Default is None.
    Compression Compression
    // contains filtered or unexported fields
}

GCSReference is a reference to one or more Google Cloud Storage objects, which together constitute an input or output to a BigQuery operation.

func NewGCSReference Uses

func NewGCSReference(uri ...string) *GCSReference

NewGCSReference constructs a reference to one or more Google Cloud Storage objects, which together constitute a data source or destination. In the simple case, a single URI in the form gs://bucket/object may refer to a single GCS object. Data may also be split into mutiple files, if multiple URIs or URIs containing wildcards are provided. Each URI may contain one '*' wildcard character, which (if present) must come after the bucket name. For more information about the treatment of wildcards and multiple URIs, see https://cloud.google.com/bigquery/exporting-data-from-bigquery#exportingmultiple

Code:

gcsRef := bigquery.NewGCSReference("gs://my-bucket/my-object")
fmt.Println(gcsRef)

type Job Uses

type Job struct {
    // contains filtered or unexported fields
}

A Job represents an operation which has been submitted to BigQuery for processing.

func (*Job) Cancel Uses

func (j *Job) Cancel(ctx context.Context) error

Cancel requests that a job be cancelled. This method returns without waiting for cancellation to take effect. To check whether the job has terminated, use Job.Status. Cancelled jobs may still incur costs.

func (*Job) ID Uses

func (j *Job) ID() string

ID returns the job's ID.

func (*Job) Read Uses

func (j *Job) Read(ctx context.Context) (*RowIterator, error)

Read fetches the results of a query job. If j is not a query job, Read returns an error.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
q := client.Query("select name, num from t1")
// Call Query.Run to get a Job, then call Read on the job.
// Note: Query.Read is a shorthand for this.
job, err := q.Run(ctx)
if err != nil {
    // TODO: Handle error.
}
it, err := job.Read(ctx)
if err != nil {
    // TODO: Handle error.
}
_ = it // TODO: iterate using Next or iterator.Pager.

func (*Job) Status Uses

func (j *Job) Status(ctx context.Context) (*JobStatus, error)

Status returns the current status of the job. It fails if the Status could not be determined.

func (*Job) Wait Uses

func (j *Job) Wait(ctx context.Context) (*JobStatus, error)

Wait blocks until the job or the context is done. It returns the final status of the job. If an error occurs while retrieving the status, Wait returns that error. But Wait returns nil if the status was retrieved successfully, even if status.Err() != nil. So callers must check both errors. See the example.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
ds := client.Dataset("my_dataset")
job, err := ds.Table("t1").CopierFrom(ds.Table("t2")).Run(ctx)
if err != nil {
    // TODO: Handle error.
}
status, err := job.Wait(ctx)
if err != nil {
    // TODO: Handle error.
}
if status.Err() != nil {
    // TODO: Handle error.
}

type JobInfo Uses

type JobInfo struct {
    Job    *Job
    Status *JobStatus
}

A JobInfo consists of a Job and a JobStatus.

type JobIterator Uses

type JobIterator struct {
    ProjectID string // Project ID of the jobs to list. Default is the client's project.
    AllUsers  bool   // Whether to list jobs owned by all users in the project, or just the current caller.
    State     State  // List only jobs in the given state. Defaults to all states.
    // contains filtered or unexported fields
}

JobIterator iterates over jobs in a project.

func (*JobIterator) Next Uses

func (it *JobIterator) Next() (JobInfo, error)

func (*JobIterator) PageInfo Uses

func (it *JobIterator) PageInfo() *iterator.PageInfo

type JobStatistics Uses

type JobStatistics struct {
    CreationTime        time.Time
    StartTime           time.Time
    EndTime             time.Time
    TotalBytesProcessed int64

    Details Statistics
}

JobStatistics contains statistics about a job.

type JobStatus Uses

type JobStatus struct {
    State State

    // All errors encountered during the running of the job.
    // Not all Errors are fatal, so errors here do not necessarily mean that the job has completed or was unsuccessful.
    Errors []*Error

    // Statistics about the job.
    Statistics *JobStatistics
    // contains filtered or unexported fields
}

JobStatus contains the current State of a job, and errors encountered while processing that job.

func (*JobStatus) Done Uses

func (s *JobStatus) Done() bool

Done reports whether the job has completed. After Done returns true, the Err method will return an error if the job completed unsuccesfully.

func (*JobStatus) Err Uses

func (s *JobStatus) Err() error

Err returns the error that caused the job to complete unsuccesfully (if any).

type LoadConfig Uses

type LoadConfig struct {
    // JobID is the ID to use for the job. If empty, a random job ID will be generated.
    JobID string

    // If AddJobIDSuffix is true, then a random string will be appended to JobID.
    AddJobIDSuffix bool

    // Src is the source from which data will be loaded.
    Src LoadSource

    // Dst is the table into which the data will be loaded.
    Dst *Table

    // CreateDisposition specifies the circumstances under which the destination table will be created.
    // The default is CreateIfNeeded.
    CreateDisposition TableCreateDisposition

    // WriteDisposition specifies how existing data in the destination table is treated.
    // The default is WriteAppend.
    WriteDisposition TableWriteDisposition
}

LoadConfig holds the configuration for a load job.

type LoadSource Uses

type LoadSource interface {
    // contains filtered or unexported methods
}

A LoadSource represents a source of data that can be loaded into a BigQuery table.

This package defines two LoadSources: GCSReference, for Google Cloud Storage objects, and ReaderSource, for data read from an io.Reader.

type LoadStatistics Uses

type LoadStatistics struct {
    // The number of bytes of source data in a load job.
    InputFileBytes int64

    // The number of source files in a load job.
    InputFiles int64

    // Size of the loaded data in bytes. Note that while a load job is in the
    // running state, this value may change.
    OutputBytes int64

    // The number of rows imported in a load job. Note that while an import job is
    // in the running state, this value may change.
    OutputRows int64
}

LoadStatistics contains statistics about a load job.

type Loader Uses

type Loader struct {
    LoadConfig
    // contains filtered or unexported fields
}

A Loader loads data from Google Cloud Storage into a BigQuery table.

func (*Loader) Run Uses

func (l *Loader) Run(ctx context.Context) (*Job, error)

Run initiates a load job.

type MultiError Uses

type MultiError []error

A MultiError contains multiple related errors.

func (MultiError) Error Uses

func (m MultiError) Error() string

type PutMultiError Uses

type PutMultiError []RowInsertionError

PutMultiError contains an error for each row which was not successfully inserted into a BigQuery table.

func (PutMultiError) Error Uses

func (pme PutMultiError) Error() string

type Query Uses

type Query struct {
    QueryConfig
    // contains filtered or unexported fields
}

A Query queries data from a BigQuery table. Use Client.Query to create a Query.

func (*Query) Read Uses

func (q *Query) Read(ctx context.Context) (*RowIterator, error)

Read submits a query for execution and returns the results via a RowIterator. It is a shorthand for Query.Run followed by Job.Read.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
q := client.Query("select name, num from t1")
it, err := q.Read(ctx)
if err != nil {
    // TODO: Handle error.
}
_ = it // TODO: iterate using Next or iterator.Pager.

func (*Query) Run Uses

func (q *Query) Run(ctx context.Context) (*Job, error)

Run initiates a query job.

type QueryConfig Uses

type QueryConfig struct {
    // JobID is the ID to use for the job. If empty, a random job ID will be generated.
    JobID string

    // If AddJobIDSuffix is true, then a random string will be appended to JobID.
    AddJobIDSuffix bool

    // Dst is the table into which the results of the query will be written.
    // If this field is nil, a temporary table will be created.
    Dst *Table

    // The query to execute. See https://cloud.google.com/bigquery/query-reference for details.
    Q   string

    // DefaultProjectID and DefaultDatasetID specify the dataset to use for unqualified table names in the query.
    // If DefaultProjectID is set, DefaultDatasetID must also be set.
    DefaultProjectID string
    DefaultDatasetID string

    // TableDefinitions describes data sources outside of BigQuery.
    // The map keys may be used as table names in the query string.
    TableDefinitions map[string]ExternalData

    // CreateDisposition specifies the circumstances under which the destination table will be created.
    // The default is CreateIfNeeded.
    CreateDisposition TableCreateDisposition

    // WriteDisposition specifies how existing data in the destination table is treated.
    // The default is WriteEmpty.
    WriteDisposition TableWriteDisposition

    // DisableQueryCache prevents results being fetched from the query cache.
    // If this field is false, results are fetched from the cache if they are available.
    // The query cache is a best-effort cache that is flushed whenever tables in the query are modified.
    // Cached results are only available when TableID is unspecified in the query's destination Table.
    // For more information, see https://cloud.google.com/bigquery/querying-data#querycaching
    DisableQueryCache bool

    // DisableFlattenedResults prevents results being flattened.
    // If this field is false, results from nested and repeated fields are flattened.
    // DisableFlattenedResults implies AllowLargeResults
    // For more information, see https://cloud.google.com/bigquery/docs/data#nested
    DisableFlattenedResults bool

    // AllowLargeResults allows the query to produce arbitrarily large result tables.
    // The destination must be a table.
    // When using this option, queries will take longer to execute, even if the result set is small.
    // For additional limitations, see https://cloud.google.com/bigquery/querying-data#largequeryresults
    AllowLargeResults bool

    // Priority specifies the priority with which to schedule the query.
    // The default priority is InteractivePriority.
    // For more information, see https://cloud.google.com/bigquery/querying-data#batchqueries
    Priority QueryPriority

    // MaxBillingTier sets the maximum billing tier for a Query.
    // Queries that have resource usage beyond this tier will fail (without
    // incurring a charge). If this field is zero, the project default will be used.
    MaxBillingTier int

    // MaxBytesBilled limits the number of bytes billed for
    // this job.  Queries that would exceed this limit will fail (without incurring
    // a charge).
    // If this field is less than 1, the project default will be
    // used.
    MaxBytesBilled int64

    // UseStandardSQL causes the query to use standard SQL.
    // The default is false (using legacy SQL).
    UseStandardSQL bool

    // UseLegacySQL causes the query to use legacy SQL.
    UseLegacySQL bool

    // Parameters is a list of query parameters. The presence of parameters
    // implies the use of standard SQL.
    // If the query uses positional syntax ("?"), then no parameter may have a name.
    // If the query uses named syntax ("@p"), then all parameters must have names.
    // It is illegal to mix positional and named syntax.
    Parameters []QueryParameter
}

QueryConfig holds the configuration for a query job.

type QueryParameter Uses

type QueryParameter struct {
    // Name is used for named parameter mode.
    // It must match the name in the query case-insensitively.
    Name string

    // Value is the value of the parameter.
    // The following Go types are supported, with their corresponding
    // Bigquery types:
    // int, int8, int16, int32, int64, uint8, uint16, uint32: INT64
    //   Note that uint, uint64 and uintptr are not supported, because
    //   they may contain values that cannot fit into a 64-bit signed integer.
    // float32, float64: FLOAT64
    // bool: BOOL
    // string: STRING
    // []byte: BYTES
    // time.Time: TIMESTAMP
    // Arrays and slices of the above.
    // Structs of the above. Only the exported fields are used.
    Value interface{}
}

A QueryParameter is a parameter to a query.

type QueryPriority Uses

type QueryPriority string

QueryPriority specifies a priority with which a query is to be executed.

const (
    BatchPriority       QueryPriority = "BATCH"
    InteractivePriority QueryPriority = "INTERACTIVE"
)

type QueryStatistics Uses

type QueryStatistics struct {
    // Billing tier for the job.
    BillingTier int64

    // Whether the query result was fetched from the query cache.
    CacheHit bool

    // The type of query statement, if valid.
    StatementType string

    // Total bytes billed for the job.
    TotalBytesBilled int64

    // Total bytes processed for the job.
    TotalBytesProcessed int64

    // Describes execution plan for the query.
    QueryPlan []*ExplainQueryStage

    // The number of rows affected by a DML statement. Present only for DML
    // statements INSERT, UPDATE or DELETE.
    NumDMLAffectedRows int64

    // ReferencedTables: [Output-only, Experimental] Referenced tables for
    // the job. Queries that reference more than 50 tables will not have a
    // complete list.
    ReferencedTables []*Table

    // The schema of the results. Present only for successful dry run of
    // non-legacy SQL queries.
    Schema Schema

    // Standard SQL: list of undeclared query parameter names detected during a
    // dry run validation.
    UndeclaredQueryParameterNames []string
}

QueryStatistics contains statistics about a query job.

type ReaderSource Uses

type ReaderSource struct {
    FileConfig
    // contains filtered or unexported fields
}

A ReaderSource is a source for a load operation that gets data from an io.Reader.

func NewReaderSource Uses

func NewReaderSource(r io.Reader) *ReaderSource

NewReaderSource creates a ReaderSource from an io.Reader. You may optionally configure properties on the ReaderSource that describe the data being read, before passing it to Table.LoaderFrom.

type RowInsertionError Uses

type RowInsertionError struct {
    InsertID string // The InsertID associated with the affected row.
    RowIndex int    // The 0-based index of the affected row in the batch of rows being inserted.
    Errors   MultiError
}

RowInsertionError contains all errors that occurred when attempting to insert a row.

func (*RowInsertionError) Error Uses

func (e *RowInsertionError) Error() string

type RowIterator Uses

type RowIterator struct {

    // StartIndex can be set before the first call to Next. If PageInfo().Token
    // is also set, StartIndex is ignored.
    StartIndex uint64
    // contains filtered or unexported fields
}

A RowIterator provides access to the result of a BigQuery lookup.

func (*RowIterator) Next Uses

func (it *RowIterator) Next(dst interface{}) error

Next loads the next row into dst. Its return value is iterator.Done if there are no more results. Once Next returns iterator.Done, all subsequent calls will return iterator.Done.

dst may implement ValueLoader, or may be a *[]Value, *map[string]Value, or struct pointer.

If dst is a *[]Value, it will be set to to new []Value whose i'th element will be populated with the i'th column of the row.

If dst is a *map[string]Value, a new map will be created if dst is nil. Then for each schema column name, the map key of that name will be set to the column's value. STRUCT types (RECORD types or nested schemas) become nested maps.

If dst is pointer to a struct, each column in the schema will be matched with an exported field of the struct that has the same name, ignoring case. Unmatched schema columns and struct fields will be ignored.

Each BigQuery column type corresponds to one or more Go types; a matching struct field must be of the correct type. The correspondences are:

STRING      string
BOOL        bool
INTEGER     int, int8, int16, int32, int64, uint8, uint16, uint32
FLOAT       float32, float64
BYTES       []byte
TIMESTAMP   time.Time
DATE        civil.Date
TIME        civil.Time
DATETIME    civil.DateTime

A repeated field corresponds to a slice or array of the element type. A STRUCT type (RECORD or nested schema) corresponds to a nested struct or struct pointer. All calls to Next on the same iterator must use the same struct type.

It is an error to attempt to read a BigQuery NULL value into a struct field. If your table contains NULLs, use a *[]Value or *map[string]Value.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
q := client.Query("select name, num from t1")
it, err := q.Read(ctx)
if err != nil {
    // TODO: Handle error.
}
for {
    var row []bigquery.Value
    err := it.Next(&row)
    if err == iterator.Done {
        break
    }
    if err != nil {
        // TODO: Handle error.
    }
    fmt.Println(row)
}

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}

type score struct {
    Name string
    Num  int
}

q := client.Query("select name, num from t1")
it, err := q.Read(ctx)
if err != nil {
    // TODO: Handle error.
}
for {
    var s score
    err := it.Next(&s)
    if err == iterator.Done {
        break
    }
    if err != nil {
        // TODO: Handle error.
    }
    fmt.Println(s)
}

func (*RowIterator) PageInfo Uses

func (it *RowIterator) PageInfo() *iterator.PageInfo

PageInfo supports pagination. See the google.golang.org/api/iterator package for details.

type Schema Uses

type Schema []*FieldSchema

Schema describes the fields in a table or query result.

func InferSchema Uses

func InferSchema(st interface{}) (Schema, error)

InferSchema tries to derive a BigQuery schema from the supplied struct value. NOTE: All fields in the returned Schema are configured to be required, unless the corresponding field in the supplied struct is a slice or array.

It is considered an error if the struct (including nested structs) contains any exported fields that are pointers or one of the following types: uint, uint64, uintptr, map, interface, complex64, complex128, func, chan. In these cases, an error will be returned. Future versions may handle these cases without error.

Recursively defined structs are also disallowed.

Code:

type Item struct {
    Name  string
    Size  float64
    Count int
}
schema, err := bigquery.InferSchema(Item{})
if err != nil {
    fmt.Println(err)
    // TODO: Handle error.
}
for _, fs := range schema {
    fmt.Println(fs.Name, fs.Type)
}

Output:

Name STRING
Size FLOAT
Count INTEGER

Code:

type Item struct {
    Name   string
    Size   float64
    Count  int    `bigquery:"number"`
    Secret []byte `bigquery:"-"`
}
schema, err := bigquery.InferSchema(Item{})
if err != nil {
    fmt.Println(err)
    // TODO: Handle error.
}
for _, fs := range schema {
    fmt.Println(fs.Name, fs.Type)
}

Output:

Name STRING
Size FLOAT
number INTEGER

type State Uses

type State int

State is one of a sequence of states that a Job progresses through as it is processed.

const (
    StateUnspecified State = iota // used only as a default in JobIterator
    Pending
    Running
    Done
)

type Statistics Uses

type Statistics interface {
    // contains filtered or unexported methods
}

Statistics is one of ExtractStatistics, LoadStatistics or QueryStatistics.

type StreamingBuffer Uses

type StreamingBuffer struct {
    // A lower-bound estimate of the number of bytes currently in the streaming
    // buffer.
    EstimatedBytes uint64

    // A lower-bound estimate of the number of rows currently in the streaming
    // buffer.
    EstimatedRows uint64

    // The time of the oldest entry in the streaming buffer.
    OldestEntryTime time.Time
}

StreamingBuffer holds information about the streaming buffer.

type StructSaver Uses

type StructSaver struct {
    // Schema determines what fields of the struct are uploaded. It should
    // match the table's schema.
    Schema Schema

    // If non-empty, BigQuery will use InsertID to de-duplicate insertions
    // of this row on a best-effort basis.
    InsertID string

    // Struct should be a struct or a pointer to a struct.
    Struct interface{}
}

StructSaver implements ValueSaver for a struct. The struct is converted to a map of values by using the values of struct fields corresponding to schema fields. Additional and missing fields are ignored, as are nested struct pointers that are nil.

func (*StructSaver) Save Uses

func (ss *StructSaver) Save() (row map[string]Value, insertID string, err error)

Save implements ValueSaver.

type Table Uses

type Table struct {
    // ProjectID, DatasetID and TableID may be omitted if the Table is the destination for a query.
    // In this case the result will be stored in an ephemeral table.
    ProjectID string
    DatasetID string
    // TableID must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_).
    // The maximum length is 1,024 characters.
    TableID string
    // contains filtered or unexported fields
}

A Table is a reference to a BigQuery table.

func (*Table) CopierFrom Uses

func (t *Table) CopierFrom(srcs ...*Table) *Copier

CopierFrom returns a Copier which can be used to copy data into a BigQuery table from one or more BigQuery tables. The returned Copier may optionally be further configured before its Run method is called.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
ds := client.Dataset("my_dataset")
c := ds.Table("combined").CopierFrom(ds.Table("t1"), ds.Table("t2"))
c.WriteDisposition = bigquery.WriteTruncate
// TODO: set other options on the Copier.
job, err := c.Run(ctx)
if err != nil {
    // TODO: Handle error.
}
status, err := job.Wait(ctx)
if err != nil {
    // TODO: Handle error.
}
if status.Err() != nil {
    // TODO: Handle error.
}

func (*Table) Create Uses

func (t *Table) Create(ctx context.Context, options ...CreateTableOption) error

Create creates a table in the BigQuery service. To create a table with a schema, pass in a Schema to Create; Schema is a valid CreateTableOption.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
t := client.Dataset("my_dataset").Table("new-table")
if err := t.Create(ctx); err != nil {
    // TODO: Handle error.
}

If you know your table's schema initially, pass a Schema to Create.

Code:

ctx := context.Background()
// Infer table schema from a Go type.
schema, err := bigquery.InferSchema(Item{})
if err != nil {
    // TODO: Handle error.
}
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
t := client.Dataset("my_dataset").Table("new-table")
if err := t.Create(ctx, schema); err != nil {
    // TODO: Handle error.
}

func (*Table) Delete Uses

func (t *Table) Delete(ctx context.Context) error

Delete deletes the table.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
if err := client.Dataset("my_dataset").Table("my_table").Delete(ctx); err != nil {
    // TODO: Handle error.
}

func (*Table) ExtractorTo Uses

func (t *Table) ExtractorTo(dst *GCSReference) *Extractor

ExtractorTo returns an Extractor which can be used to extract data from a BigQuery table into Google Cloud Storage. The returned Extractor may optionally be further configured before its Run method is called.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
gcsRef := bigquery.NewGCSReference("gs://my-bucket/my-object")
gcsRef.FieldDelimiter = ":"
// TODO: set other options on the GCSReference.
ds := client.Dataset("my_dataset")
extractor := ds.Table("my_table").ExtractorTo(gcsRef)
extractor.DisableHeader = true
// TODO: set other options on the Extractor.
job, err := extractor.Run(ctx)
if err != nil {
    // TODO: Handle error.
}
status, err := job.Wait(ctx)
if err != nil {
    // TODO: Handle error.
}
if status.Err() != nil {
    // TODO: Handle error.
}

func (*Table) FullyQualifiedName Uses

func (t *Table) FullyQualifiedName() string

FullyQualifiedName returns the ID of the table in projectID:datasetID.tableID format.

func (*Table) LoaderFrom Uses

func (t *Table) LoaderFrom(src LoadSource) *Loader

LoaderFrom returns a Loader which can be used to load data into a BigQuery table. The returned Loader may optionally be further configured before its Run method is called. See GCSReference and ReaderSource for additional configuration options that affect loading.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
gcsRef := bigquery.NewGCSReference("gs://my-bucket/my-object")
gcsRef.AllowJaggedRows = true
gcsRef.MaxBadRecords = 5
gcsRef.Schema = schema
// TODO: set other options on the GCSReference.
ds := client.Dataset("my_dataset")
loader := ds.Table("my_table").LoaderFrom(gcsRef)
loader.CreateDisposition = bigquery.CreateNever
// TODO: set other options on the Loader.
job, err := loader.Run(ctx)
if err != nil {
    // TODO: Handle error.
}
status, err := job.Wait(ctx)
if err != nil {
    // TODO: Handle error.
}
if status.Err() != nil {
    // TODO: Handle error.
}

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
f, err := os.Open("data.csv")
if err != nil {
    // TODO: Handle error.
}
rs := bigquery.NewReaderSource(f)
rs.AllowJaggedRows = true
rs.MaxBadRecords = 5
rs.Schema = schema
// TODO: set other options on the GCSReference.
ds := client.Dataset("my_dataset")
loader := ds.Table("my_table").LoaderFrom(rs)
loader.CreateDisposition = bigquery.CreateNever
// TODO: set other options on the Loader.
job, err := loader.Run(ctx)
if err != nil {
    // TODO: Handle error.
}
status, err := job.Wait(ctx)
if err != nil {
    // TODO: Handle error.
}
if status.Err() != nil {
    // TODO: Handle error.
}

func (*Table) Metadata Uses

func (t *Table) Metadata(ctx context.Context) (*TableMetadata, error)

Metadata fetches the metadata for the table.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
md, err := client.Dataset("my_dataset").Table("my_table").Metadata(ctx)
if err != nil {
    // TODO: Handle error.
}
fmt.Println(md)

func (*Table) Read Uses

func (t *Table) Read(ctx context.Context) *RowIterator

Read fetches the contents of the table.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
it := client.Dataset("my_dataset").Table("my_table").Read(ctx)
_ = it // TODO: iterate using Next or iterator.Pager.

func (*Table) Update Uses

func (t *Table) Update(ctx context.Context, tm TableMetadataToUpdate, etag string) (*TableMetadata, error)

Update modifies specific Table metadata fields.

To perform a blind write, ignoring the existing state (and possibly overwriting other updates), pass the empty string as the etag.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
t := client.Dataset("my_dataset").Table("my_table")
tm, err := t.Update(ctx, bigquery.TableMetadataToUpdate{
    Description: "my favorite table",
}, "")
if err != nil {
    // TODO: Handle error.
}
fmt.Println(tm)

This example illustrates how to perform a read-modify-write sequence on table metadata. Passing the metadata's ETag to the Update call ensures that the call will fail if the metadata was changed since the read.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
t := client.Dataset("my_dataset").Table("my_table")
md, err := t.Metadata(ctx)
if err != nil {
    // TODO: Handle error.
}
md2, err := t.Update(ctx,
    bigquery.TableMetadataToUpdate{Name: "new " + md.Name},
    md.ETag)
if err != nil {
    // TODO: Handle error.
}
fmt.Println(md2)

func (*Table) Uploader Uses

func (t *Table) Uploader() *Uploader

Uploader returns an Uploader that can be used to append rows to t. The returned Uploader may optionally be further configured before its Put method is called.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
u := client.Dataset("my_dataset").Table("my_table").Uploader()
_ = u // TODO: Use u.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
u := client.Dataset("my_dataset").Table("my_table").Uploader()
u.SkipInvalidRows = true
u.IgnoreUnknownValues = true
_ = u // TODO: Use u.

type TableCreateDisposition Uses

type TableCreateDisposition string

TableCreateDisposition specifies the circumstances under which destination table will be created. Default is CreateIfNeeded.

const (
    // CreateIfNeeded will create the table if it does not already exist.
    // Tables are created atomically on successful completion of a job.
    CreateIfNeeded TableCreateDisposition = "CREATE_IF_NEEDED"

    // CreateNever ensures the table must already exist and will not be
    // automatically created.
    CreateNever TableCreateDisposition = "CREATE_NEVER"
)

type TableIterator Uses

type TableIterator struct {
    // contains filtered or unexported fields
}

A TableIterator is an iterator over Tables.

func (*TableIterator) Next Uses

func (it *TableIterator) Next() (*Table, error)

Next returns the next result. Its second return value is Done if there are no more results. Once Next returns Done, all subsequent calls will return Done.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
it := client.Dataset("my_dataset").Tables(ctx)
for {
    t, err := it.Next()
    if err == iterator.Done {
        break
    }
    if err != nil {
        // TODO: Handle error.
    }
    fmt.Println(t)
}

func (*TableIterator) PageInfo Uses

func (it *TableIterator) PageInfo() *iterator.PageInfo

PageInfo supports pagination. See the google.golang.org/api/iterator package for details.

type TableMetadata Uses

type TableMetadata struct {
    Description string // The user-friendly description of this table.
    Name        string // The user-friendly name for this table.
    Schema      Schema
    View        string

    ID   string // An opaque ID uniquely identifying the table.
    Type TableType

    // The time when this table expires. If not set, the table will persist
    // indefinitely. Expired tables will be deleted and their storage reclaimed.
    ExpirationTime time.Time

    CreationTime     time.Time
    LastModifiedTime time.Time

    // The size of the table in bytes.
    // This does not include data that is being buffered during a streaming insert.
    NumBytes int64

    // The number of rows of data in this table.
    // This does not include data that is being buffered during a streaming insert.
    NumRows uint64

    // The time-based partitioning settings for this table.
    TimePartitioning *TimePartitioning

    // Contains information regarding this table's streaming buffer, if one is
    // present. This field will be nil if the table is not being streamed to or if
    // there is no data in the streaming buffer.
    StreamingBuffer *StreamingBuffer

    // ETag is the ETag obtained when reading metadata. Pass it to Table.Update to
    // ensure that the metadata hasn't changed since it was read.
    ETag string
}

TableMetadata contains information about a BigQuery table.

type TableMetadataToUpdate Uses

type TableMetadataToUpdate struct {
    // Description is the user-friendly description of this table.
    Description optional.String

    // Name is the user-friendly name for this table.
    Name optional.String

    // Schema is the table's schema.
    // When updating a schema, you can add columns but not remove them.
    Schema Schema

    // ExpirationTime is the time when this table expires.
    ExpirationTime time.Time
}

TableMetadataToUpdate is used when updating a table's metadata. Only non-nil fields will be updated.

type TableType Uses

type TableType string

TableType is the type of table.

const (
    RegularTable  TableType = "TABLE"
    ViewTable     TableType = "VIEW"
    ExternalTable TableType = "EXTERNAL"
)

type TableWriteDisposition Uses

type TableWriteDisposition string

TableWriteDisposition specifies how existing data in a destination table is treated. Default is WriteAppend.

const (
    // WriteAppend will append to any existing data in the destination table.
    // Data is appended atomically on successful completion of a job.
    WriteAppend TableWriteDisposition = "WRITE_APPEND"

    // WriteTruncate overrides the existing data in the destination table.
    // Data is overwritten atomically on successful completion of a job.
    WriteTruncate TableWriteDisposition = "WRITE_TRUNCATE"

    // WriteEmpty fails writes if the destination table already contains data.
    WriteEmpty TableWriteDisposition = "WRITE_EMPTY"
)

type TimePartitioning Uses

type TimePartitioning struct {
    // (Optional) The amount of time to keep the storage for a partition.
    // If the duration is empty (0), the data in the partitions do not expire.
    Expiration time.Duration
}

TimePartitioning is a CreateTableOption that can be used to set time-based date partitioning on a table. For more information see: https://cloud.google.com/bigquery/docs/creating-partitioned-tables

type Uploader Uses

type Uploader struct {

    // SkipInvalidRows causes rows containing invalid data to be silently
    // ignored. The default value is false, which causes the entire request to
    // fail if there is an attempt to insert an invalid row.
    SkipInvalidRows bool

    // IgnoreUnknownValues causes values not matching the schema to be ignored.
    // The default value is false, which causes records containing such values
    // to be treated as invalid records.
    IgnoreUnknownValues bool

    // A TableTemplateSuffix allows Uploaders to create tables automatically.
    //
    // Experimental: this option is experimental and may be modified or removed in future versions,
    // regardless of any other documented package stability guarantees.
    //
    // When you specify a suffix, the table you upload data to
    // will be used as a template for creating a new table, with the same schema,
    // called <table> + <suffix>.
    //
    // More information is available at
    // https://cloud.google.com/bigquery/streaming-data-into-bigquery#template-tables
    TableTemplateSuffix string
    // contains filtered or unexported fields
}

An Uploader does streaming inserts into a BigQuery table. It is safe for concurrent use.

func (*Uploader) Put Uses

func (u *Uploader) Put(ctx context.Context, src interface{}) error

Put uploads one or more rows to the BigQuery service.

If src is ValueSaver, then its Save method is called to produce a row for uploading.

If src is a struct or pointer to a struct, then a schema is inferred from it and used to create a StructSaver. The InsertID of the StructSaver will be empty.

If src is a slice of ValueSavers, structs, or struct pointers, then each element of the slice is treated as above, and multiple rows are uploaded.

Put returns a PutMultiError if one or more rows failed to be uploaded. The PutMultiError contains a RowInsertionError for each failed row.

Put will retry on temporary errors (see https://cloud.google.com/bigquery/troubleshooting-errors). This can result in duplicate rows if you do not use insert IDs. Also, if the error persists, the call will run indefinitely. Pass a context with a timeout to prevent hanging calls.

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
u := client.Dataset("my_dataset").Table("my_table").Uploader()
// Item implements the ValueSaver interface.
items := []*Item{
    {Name: "n1", Size: 32.6, Count: 7},
    {Name: "n2", Size: 4, Count: 2},
    {Name: "n3", Size: 101.5, Count: 1},
}
if err := u.Put(ctx, items); err != nil {
    // TODO: Handle error.
}

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
u := client.Dataset("my_dataset").Table("my_table").Uploader()

type score struct {
    Name string
    Num  int
}
scores := []score{
    {Name: "n1", Num: 12},
    {Name: "n2", Num: 31},
    {Name: "n3", Num: 7},
}
// Schema is inferred from the score type.
if err := u.Put(ctx, scores); err != nil {
    // TODO: Handle error.
}

Code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}
u := client.Dataset("my_dataset").Table("my_table").Uploader()

type score struct {
    Name string
    Num  int
}

// Assume schema holds the table's schema.
savers := []*bigquery.StructSaver{
    {Struct: score{Name: "n1", Num: 12}, Schema: schema, InsertID: "id1"},
    {Struct: score{Name: "n2", Num: 31}, Schema: schema, InsertID: "id2"},
    {Struct: score{Name: "n3", Num: 7}, Schema: schema, InsertID: "id3"},
}
if err := u.Put(ctx, savers); err != nil {
    // TODO: Handle error.
}

type Value Uses

type Value interface{}

Value stores the contents of a single cell from a BigQuery result.

type ValueLoader Uses

type ValueLoader interface {
    Load(v []Value, s Schema) error
}

ValueLoader stores a slice of Values representing a result row from a Read operation. See RowIterator.Next for more information.

type ValueSaver Uses

type ValueSaver interface {
    // Save returns a row to be inserted into a BigQuery table, represented
    // as a map from field name to Value.
    // If insertID is non-empty, BigQuery will use it to de-duplicate
    // insertions of this row on a best-effort basis.
    Save() (row map[string]Value, insertID string, err error)
}

A ValueSaver returns a row of data to be inserted into a table.

type ValuesSaver Uses

type ValuesSaver struct {
    Schema Schema

    // If non-empty, BigQuery will use InsertID to de-duplicate insertions
    // of this row on a best-effort basis.
    InsertID string

    Row []Value
}

ValuesSaver implements ValueSaver for a slice of Values.

func (*ValuesSaver) Save Uses

func (vls *ValuesSaver) Save() (map[string]Value, string, error)

Save implements ValueSaver.

Package bigquery imports 25 packages (graph) and is imported by 13 packages. Updated 2017-09-26. Refresh now. Tools for package owners.