Quickstart: Using client libraries
This page shows you how to get started with the BigQuery API in your favorite programming language.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the BigQuery API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
To provide access to your project, grant the Project > Owner role to your service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the BigQuery API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
To provide access to your project, grant the Project > Owner role to your service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.
Install the client library
C#
For more on setting up your C# development environment, refer to the C# Development Environment Setup Guide.
Install-Package Google.Cloud.BigQuery.V2 -Pre
Go
go mod init YOUR_MODULE_NAME go get cloud.google.com/go/bigquery
Java
For more on setting up your Java development environment, refer to the Java Development Environment Setup Guide.
If you are using Maven, add
the following to your pom.xml
file. For more information about
BOMs, see The Google Cloud Platform Libraries BOM.
If you are using Gradle, add the following to your dependencies:
If you are using sbt, add the following to your dependencies:
If you're using Visual Studio Code, IntelliJ, or Eclipse, you can add client libraries to your project using the following IDE plugins:
The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.
Node.js
For more on setting up your Node.js development environment, refer to the Node.js Development Environment Setup Guide.
npm install --save @google-cloud/bigquery
PHP
composer require google/cloud-bigquery
Python
For more on setting up your Python development environment, refer to the Python Development Environment Setup Guide.
pip install --upgrade google-cloud-bigquery
Ruby
For more on setting up your Ruby development environment, refer to the Ruby Development Environment Setup Guide.
gem install google-cloud-bigquery
Import the libraries
C#
For more information, see the BigQuery C# API reference documentation.
Go
For more information, see the BigQuery Go API reference documentation.
Java
For more information, see the BigQuery Java API reference documentation.
Node.js
For more information, see the BigQuery Node.js API reference documentation.
PHP
For more information, see the BigQuery PHP API reference documentation.
Python
For more information, see the BigQuery Python API reference documentation.
Ruby
For more information, see the BigQuery Ruby API reference documentation.
Initialize a BigQuery client
Initialize a client to authenticate and connect to the BigQuery API.
C#
Use the BigQueryClient.Create() function to create the BigQuery client.
Go
Use the bigquery.NewClient() function to create the BigQuery client.
Java
Use the BigQueryOptions.getDefaultInstance() function to use the default authentication options. Use the BigQueryOptions.getService() function to create the BigQuery client.
Node.js
Instantiate the BigQuery class to create the BigQuery client.
PHP
Instantiate the BigQueryClient class to create the BigQuery client.
Python
Instantiate the bigquery.Client class to create the BigQuery client.
Ruby
Use the Google::Cloud::Bigquery.new function to create the BigQuery client.
Query a dataset
The following query retrieves the most-viewed questions tagged with google-bigquery
from the Stack Overflow public dataset.
SELECT CONCAT( 'https://stackoverflow.com/questions/', CAST(id as STRING)) as url, view_count FROM `bigquery-public-data.stackoverflow.posts_questions` WHERE tags like '%google-bigquery%' ORDER BY view_count DESC LIMIT 10
This query uses GoogleSQL syntax. The client libraries default to GoogleSQL syntax. To change the SQL dialect, see BigQuery SQL dialects.
Run the query
Run the following query using the authenticated BigQuery client.
C#
Define a query string and use the client.ExecuteQuery() function to submit the query and get the results.
Go
Use the bigquery.Query() function to define a query and Query.Read() function to submit the query and get the results.
Java
Define the query with a QueryJobConfiguration instance. Start the query job with the BigQuery.create() method.
Node.js
Use the BigQuery.query() method to start the query.
PHP
Create a query configuration and use the BigQueryClient.startQuery() method to start the query.
Python
Use the Client.query() method to start the query.
Ruby
Use the Google::Cloud::Bigquery::Project.query function to start a query and wait for the results.
Learn more about queries:
- Querying data overview
- How to run interactive and batch queries
- How to write query results to a permanent table
Display the query result
Display the query results.
C#
Go
Use the RowIterator.Next() function to load each row into a struct pointer.