tessl install tessl/golang-cloud-google-com--go--bigquery@1.72.0Google Cloud BigQuery client library providing comprehensive Go APIs for querying, loading data, managing datasets and tables, streaming inserts, and accessing BigQuery's ecosystem of services including Storage, Analytics Hub, Data Transfer, and Migration APIs
This document covers creating and configuring BigQuery clients, including authentication, project settings, and advanced client options.
func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error)Create a BigQuery client for a specific project:
ctx := context.Background()
client, err := bigquery.NewClient(ctx, "my-project-id")
if err != nil {
// Handle error
}
defer client.Close()Use the DetectProjectID constant to automatically detect the project ID from credentials:
const DetectProjectID = "*detect-project-id*"client, err := bigquery.NewClient(ctx, bigquery.DetectProjectID)This extracts the project ID from:
Note: JWT-based credentials do not contain project ID and cannot be auto-detected.
type Client struct {
// Location, if set, will be used as the default location for all subsequent
// dataset creation and job operations. A location specified directly in one of
// those operations will override this value.
Location string
}The Location field sets the default geographic location for dataset and job operations. Common locations include:
"US" - Multi-region in the United States"EU" - Multi-region in the European Union"us-central1" - Iowa, USA"europe-west1" - Belgium"asia-northeast1" - Tokyo, Japanimport "google.golang.org/api/option"
client, err := bigquery.NewClient(ctx, projectID,
option.WithCredentialsFile("/path/to/service-account-key.json"))client, err := bigquery.NewClient(ctx, projectID,
option.WithCredentialsJSON([]byte(`{...}`)))import "golang.org/x/oauth2"
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: "your-access-token",
})
client, err := bigquery.NewClient(ctx, projectID,
option.WithTokenSource(tokenSource))// No option needed - this is the default
client, err := bigquery.NewClient(ctx, projectID)ADC automatically finds credentials in this order:
GOOGLE_APPLICATION_CREDENTIALS environment variablegcloud auth application-default loginconst Scope = "https://www.googleapis.com/auth/bigquery"Specify custom scopes:
client, err := bigquery.NewClient(ctx, projectID,
option.WithScopes(
"https://www.googleapis.com/auth/bigquery",
"https://www.googleapis.com/auth/cloud-platform",
))client, err := bigquery.NewClient(ctx, projectID)
if err != nil {
return err
}
client.Location = "US"import "net/http"
httpClient := &http.Client{
Timeout: 30 * time.Second,
}
client, err := bigquery.NewClient(ctx, projectID,
option.WithHTTPClient(httpClient))client, err := bigquery.NewClient(ctx, projectID,
option.WithEndpoint("https://custom-bigquery-endpoint.example.com"))import "google.golang.org/api/googleapi"
client, err := bigquery.NewClient(ctx, projectID,
option.WithGRPCDialOption(/* gRPC options */))client, err := bigquery.NewClient(ctx, projectID,
option.WithUserAgent("my-application/1.0"))Control how queries are executed for better performance:
func WithDefaultJobCreationMode(mode JobCreationMode) option.ClientOptiontype JobCreationMode int
const (
// JobCreationModeRequired requires a job to be created for all queries
JobCreationModeRequired JobCreationMode = iota
// JobCreationModeOptional allows the API to decide whether to create a job
// This can provide performance improvements for queries with small result sets
JobCreationModeOptional
)client, err := bigquery.NewClient(ctx, projectID,
bigquery.WithDefaultJobCreationMode(bigquery.JobCreationModeOptional))Enable the Storage Read API for faster data retrieval:
func (c *Client) EnableStorageReadClient(ctx context.Context, opts ...option.ClientOption) errorerr := client.EnableStorageReadClient(ctx)
if err != nil {
return err
}Note: Once enabled, pagination methods like PageInfo().Token and RowIterator.StartIndex are not supported. Calling this method twice will return an error.
func (c *Client) Close() errorClose releases resources held by the client. It should be called when the client is no longer needed:
defer client.Close()It need not be called at program exit.
func (c *Client) Project() stringReturns the project ID used by the client, whether explicitly specified or auto-detected:
projectID := client.Project()For testing or reproducible behavior, seed the internal random number generator used for job and insert IDs:
func Seed(s int64)bigquery.Seed(12345) // Must be called before creating clients
client, err := bigquery.NewClient(ctx, projectID)package main
import (
"context"
"log"
"time"
"cloud.google.com/go/bigquery"
"google.golang.org/api/option"
)
func setupClient() (*bigquery.Client, error) {
ctx := context.Background()
// Create client with custom options
client, err := bigquery.NewClient(
ctx,
"my-project-id",
option.WithCredentialsFile("/path/to/key.json"),
bigquery.WithDefaultJobCreationMode(bigquery.JobCreationModeOptional),
)
if err != nil {
return nil, err
}
// Set default location
client.Location = "US"
// Enable Storage Read API for faster reads
if err := client.EnableStorageReadClient(ctx); err != nil {
client.Close()
return nil, err
}
return client, nil
}
func main() {
client, err := setupClient()
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Use client for BigQuery operations
log.Printf("Connected to project: %s", client.Project())
}Errors from BigQuery operations often contain detailed information:
type Error struct {
Location string
Message string
Reason string
}
func (e Error) Error() stringtype MultiError []error
func (m MultiError) Error() stringMultiError contains multiple related errors. This is typically returned when multiple operations fail:
// MultiError is returned by streaming insert operations when multiple rows fail
inserter := table.Inserter()
err := inserter.Put(ctx, rows)
if err != nil {
if multiErr, ok := err.(bigquery.PutMultiError); ok {
fmt.Printf("%d row(s) failed to insert\n", len(multiErr))
for _, rowErr := range multiErr {
fmt.Printf("Row %d failed: %v\n", rowErr.RowIndex, rowErr.Errors)
}
}
}The MultiError.Error() method returns a formatted string:
"(0 errors)""(and 1 other error)""(and N other errors)"Use type assertion to access detailed error information:
import (
"errors"
"google.golang.org/api/googleapi"
)
_, err := bigquery.NewClient(ctx, projectID)
if err != nil {
var apiErr *googleapi.Error
if errors.As(err, &apiErr) {
log.Printf("API Error: Code=%d, Message=%s", apiErr.Code, apiErr.Message)
}
return err
}Common error codes:
400: Bad request (invalid parameters)401: Authentication failed403: Permission denied404: Resource not found409: Resource already exists or conflict500: Internal server error503: Service unavailableBe aware of BigQuery quotas and limits:
Unstructured googleapi.Error responses often indicate quota exceeded. Consider: