This page provides a short exercise in building a simple command-line application (in C#, Go, Java, Node.js, PHP, or Ruby) with the Google BigQuery API. This simple application will run a query on one of the available Sample Datasets and display the result.
Before you begin
- An understanding of basic BigQuery concepts and terminology.
See what is BigQuery for a list of basic terms and concepts to become familiar with, such as jobs and projects.
- Ability to write and run a simple application in C#, Go, Java, Node.js, PHP, or Ruby.
The BigQuery service provides a REST-based API that can be programmatically accessed using C#, Go, Java, Node.js, PHP, or Ruby. In addition to a basic understand of how to develop C#, Go, Java, Node.js, PHP, or Ruby applications, you should also be able to download and install additional libraries before attempting this tutorial.
- A Google Cloud Platform Console project with the BigQuery API enabled.
Applications that use BigQuery must be associated with a Google Cloud Platform Console project with the BigQuery API enabled. This project provides authentication credentials you use in your application to identify it to Google and authorize its use of the BigQuery API.
Follow these instructions to create a project, enable the BigQuery API for it, and setup your development environment with authentication credentials. Note down the project's ID, which you'll provide to the application later on.
- Installed Google Cloud Client Libraries.
Our samples use the Google Cloud Client libraries to make calls to the BigQuery API. Download and install the appropriate library from the client libraries page.
If you are developing with Java, Maven is a useful way to manage and organize dependencies. The Google Cloud Client Libraries for Java are available in the central Maven repository.
Create an authorized BigQuery service object
In order to make authenticated requests to Google Cloud Platform APIs using the Google Cloud Client libraries, you must:
- Fetch the credential to use for requests.
- Create a service object that uses that credential.
Refer to BigQuery Client Libraries for how to install and create an authorized client.
Running queries
BigQuery provides two API methods for running queries. The synchronous query method involves a single API call, and waits to provide a response until the query is complete (unless you provide it with an optional timeout value). The asynchronous query method "inserts" a query job, and immediately returns an ID for that job. Then, you can use this job ID to poll for the status of the query, and retrieve the query result if complete. This example uses the synchronous query method. For more information about different ways to query using BigQuery, see querying data.
Running the query
To run a synchronous query, the application makes an API call that passes the query itself (as a string), along with the project number that the query will be run under for billing and quota purposes. The query in the example below finds the 10 of Shakespeare's works with the greatest number of distinct words. BigQuery uses a SQL-like syntax, which is described in our query reference guide.
C#
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Go
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Java
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Node.js
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
PHP
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Python
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Ruby
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Displaying the query result
After the query has completed, the API returns the result set as a JSON object, which the SDK exposes as a native object. In addition to the actual query results, the JSON response contains metadata about the query job, including a unique job ID and the schema of the result set. The application parses the query response and displays the resulting values.
C#
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Go
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Java
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Node.js
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
PHP
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Python
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Ruby
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Next steps
This tutorial covers only the most basic steps necessary to make calls to the BigQuery API from a command-line application. The BigQuery API also provides methods for running asynchronous queries, creating tables and datasets, listing projects, and more.
- For a deeper look into authorizing access to the BigQuery API from various types of applications, see Authenticating requests to the Google BigQuery API.
- Post your general questions about developing applications using the BigQuery API on Stack Overflow. Google engineers monitor and answer questions tagged with google-bigquery.
Complete source code
Here is the complete source code for the examples in this quick start guide.
C#
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Go
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Java
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Node.js
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
PHP
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Python
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.
Ruby
For more on installing and creating a BigQuery client, refer to BigQuery Client Libraries.