BigQuery Client Libraries

This page shows the recommended way to get started with the Google BigQuery API in your language of choice. Read more about client libraries for Google Cloud APIs, including the older Google APIs client libraries, in Client Libraries Explained.

Installing the client library

C#

Install-Package Google.Bigquery.V2 -Pre

Go

go get cloud.google.com/go/bigquery

Java

If you are using Maven, add this to your pom.xml file:

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-bigquery</artifactId>
    <version>0.7.0</version>
</dependency>

If you are using Gradle, add this to your dependencies:

compile group: 'com.google.cloud', name: 'google-cloud-bigquery', version: '0.7.0'

Node.js

npm install --save @google-cloud/bigquery

PHP

composer require google/cloud

Python

pip install --upgrade google-cloud-bigquery

Ruby

gem install google-cloud-bigquery

Using the client library

Here's an example of how to use the client library. To run it on your local workstation you must first install the Google Cloud SDK and authenticate by running the following command:

gcloud beta auth application-default login

For information about authenticating in other environments, see the Google Cloud Platform Auth Guide.

C#

using System;
using Google.Bigquery.V2;

namespace GoogleCloudSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            // Your Google Cloud Platform project ID
            string projectId = "YOUR_PROJECT_ID";

            // Instantiates a client
            BigqueryClient client = BigqueryClient.Create(projectId);

            // The id for the new dataset
            string datasetId = "my_new_dataset";

            // Creates the dataset
            BigqueryDataset dataset = client.CreateDataset(datasetId);

            Console.WriteLine($"Dataset {dataset.FullyQualifiedId} created.");
        }
    }
}
Read more in the C# API Reference Documentation for the Google BigQuery API Client Library.

Go

// Sample bigquery_quickstart creates a Google BigQuery dataset.
package main

import (
	"fmt"
	"golang.org/x/net/context"
	"log"

	// Imports the Google Cloud Datastore client package
	"cloud.google.com/go/bigquery"
)

func main() {
	ctx := context.Background()

	// Your Google Cloud Platform project ID
	projectID := "YOUR_PROJECT_ID"

	// Creates a client
	client, err := bigquery.NewClient(ctx, projectID)
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}

	// The name for the new dataset
	datasetName := "my_new_dataset"

	// Prepares the new dataset
	dataset := client.Dataset(datasetName)

	// Creates the dataset
	if err := dataset.Create(ctx); err != nil {
		log.Fatalf("Failed to create dataset: %v", err)
	}

	fmt.Printf("Dataset %v created", dataset)
}
Read more in the Go API Reference Documentation for the Google BigQuery API Client Library.

Java

// Imports the Google Cloud client library
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Dataset;
import com.google.cloud.bigquery.DatasetInfo;

public class QuickstartSample {
  public static void main(String... args) throws Exception {
    // Instantiates a client
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

    // The name for the new dataset
    String datasetName = "my_new_dataset";

    // Prepares a new dataset
    Dataset dataset = null;
    DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();

    // Creates the dataset
    dataset = bigquery.create(datasetInfo);

    System.out.printf("Dataset %s created.%n", dataset.getDatasetId().getDataset());
  }
}
Read more in the Java API Reference Documentation for the Google BigQuery API Client Library.

Node.js

// Imports the Google Cloud client library
const BigQuery = require('@google-cloud/bigquery');

// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';

// Instantiates a client
const bigquery = BigQuery({
  projectId: projectId
});

// The name for the new dataset
const datasetName = 'my_new_dataset';

// Creates the new dataset
bigquery.createDataset(datasetName)
  .then((results) => {
    const dataset = results[0];

    console.log(`Dataset ${dataset.id} created.`);
  });
Read more in the Node.js API Reference Documentation for the Google BigQuery API Client Library.

PHP

# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\BigQuery\BigQueryClient;

# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';

# Instantiates a client
$bigquery = new BigQueryClient([
    'projectId' => $projectId
]);

# The name for the new dataset
$datasetName = 'my_new_dataset';

# Creates the new dataset
$dataset = $bigquery->createDataset($datasetName);

echo 'Dataset ' . $dataset->id() . ' created.';
Read more in the PHP API Reference Documentation for the Google BigQuery API Client Library.

Python

# Imports the Google Cloud client library
from google.cloud import bigquery

# Instantiates a client
bigquery_client = bigquery.Client()

# The name for the new dataset
dataset_name = 'my_new_dataset'

# Prepares the new dataset
dataset = bigquery_client.dataset(dataset_name)

# Creates the new dataset
dataset.create()

print('Dataset {} created.'.format(dataset.name))
Read more in the Python API Reference Documentation for the Google BigQuery API Client Library.

Ruby

# Imports the Google Cloud client library
require "google/cloud/bigquery"

# Your Google Cloud Platform project ID
project_id = "YOUR_PROJECT_ID"

# Instantiates a client
bigquery = Google::Cloud::Bigquery.new project: project_id

# The name for the new dataset
dataset_name = "my_new_dataset"

# Creates the new dataset
dataset = bigquery.create_dataset dataset_name

puts "Dataset #{dataset.dataset_id} created."
Read more in the Ruby API Reference Documentation for the Google BigQuery API Client Library.

Additional Resources

Send feedback about...