Skip to content

Repository files navigation

Google Cloud BigQuery Client for Java

Java idiomatic client for Cloud BigQuery.

MavenStability

Quickstart

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

<!-- Using libraries-bom to manage versions.See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>8.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
</dependency>

If you are using Maven without BOM, add this to your dependencies:

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

If you are using Gradle, add this to your dependencies

compile 'com.google.cloud:google-cloud-bigquery:1.116.3'

If you are using SBT, add this to your dependencies

libraryDependencies +="com.google.cloud"%"google-cloud-bigquery"%"1.116.3"

Authentication

See the Authentication section in the base directory's README.

Getting Started

Prerequisites

You will need a Google Cloud Platform Console project with the Cloud BigQuery API enabled. You will need to enable billing to use Google Cloud BigQuery. Follow these instructions to get your project set up. You will also need to set up the local development environment by installing the Google Cloud SDK and running the following commands in command line: gcloud auth login and gcloud config set project [YOUR PROJECT ID].

Installation and setup

You'll need to obtain the google-cloud-bigquery library. See the Quickstart section to add google-cloud-bigquery as a dependency in your code.

About Cloud BigQuery

Cloud BigQuery is a fully managed, NoOps, low cost data analytics service. Data can be streamed into BigQuery at millions of rows per second to enable real-time analysis. With BigQuery you can easily deploy Petabyte-scale Databases.

See the Cloud BigQuery client library docs to learn how to use this Cloud BigQuery Client Library.

Creating a dataset

With BigQuery you can create datasets. A dataset is a grouping mechanism that holds zero or more tables. Add the following import at the top of your file:

importcom.google.cloud.bigquery.DatasetInfo;

Then, to create the dataset, use the following code:

// Create a datasetStringdatasetId = "my_dataset_id";
bigquery.create(DatasetInfo.newBuilder(datasetId).build());

Creating a table

With BigQuery you can create different types of tables: normal tables with an associated schema, external tables backed by data stored on Google Cloud Storage and view tables that are created from a BigQuery SQL query. In this code snippet we show how to create a normal table with only one string field. Add the following imports at the top of your file:

importcom.google.cloud.bigquery.Field;
importcom.google.cloud.bigquery.Schema;
importcom.google.cloud.bigquery.StandardTableDefinition;
importcom.google.cloud.bigquery.Table;
importcom.google.cloud.bigquery.TableId;
importcom.google.cloud.bigquery.TableInfo;

Then add the following code to create the table:

TableIdtableId = TableId.of(datasetId, "my_table_id");
// Table field definitionFieldstringField = Field.of("StringField", LegacySQLTypeName.STRING);
// Table schema definitionSchemaschema = Schema.of(stringField);
// Create a tableStandardTableDefinitiontableDefinition = StandardTableDefinition.of(schema);
TablecreatedTable = bigquery.create(TableInfo.of(tableId, tableDefinition));

Loading data into a table

BigQuery provides several ways to load data into a table: streaming rows or loading data from a Google Cloud Storage file. In this code snippet we show how to stream rows into a table. Add the following imports at the top of your file:

importcom.google.cloud.bigquery.InsertAllRequest;
importcom.google.cloud.bigquery.InsertAllResponse;
importjava.util.HashMap;
importjava.util.Map;

Then add the following code to insert data:

Map<String, Object> firstRow = newHashMap<>();
Map<String, Object> secondRow = newHashMap<>();
firstRow.put("StringField", "value1");
secondRow.put("StringField", "value2");
// Create an insert requestInsertAllRequestinsertRequest = InsertAllRequest.newBuilder(tableId)
.addRow(firstRow)
.addRow(secondRow)
.build();
// Insert rowsInsertAllResponseinsertResponse = bigquery.insertAll(insertRequest);
// Check if errors occurredif (insertResponse.hasErrors()) {
System.out.println("Errors occurred while inserting rows");
}

Querying data

BigQuery enables querying data by running queries and waiting for the result. Queries can be run directly or through a Query Job. In this code snippet we show how to run a query directly and wait for the result. Add the following imports at the top of your file:

importcom.google.cloud.bigquery.FieldValueList;
importcom.google.cloud.bigquery.QueryJobConfiguration;

Then add the following code to run the query and wait for the result:

// Create a query requestQueryJobConfigurationqueryConfig =
QueryJobConfiguration.newBuilder("SELECT my_column FROM my_dataset_id.my_table_id").build();
// Read rowsSystem.out.println("Table rows:");
for (FieldValueListrow : bigquery.query(queryConfig).iterateAll()) {
System.out.println(row);
}

Complete source code

In InsertDataAndQueryTable.java we put together all the code shown above into one program. The program assumes that you are running on Compute Engine or from your own desktop. To run the example on App Engine, simply move the code from the main method to your application's servlet class and change the print statements to display on your webpage.

Samples

Samples are in the samples/ directory. The samples' README.md has instructions for running the samples.

SampleSource CodeTry it
Add Column Load Appendsource codeOpen in Cloud Shell
Add Empty Columnsource codeOpen in Cloud Shell
Auth Drive Scopesource codeOpen in Cloud Shell
Auth Snippetssource codeOpen in Cloud Shell
Browse Tablesource codeOpen in Cloud Shell
Cancel Jobsource codeOpen in Cloud Shell
Copy Multiple Tablessource codeOpen in Cloud Shell
Copy Tablesource codeOpen in Cloud Shell
Create Clustered Tablesource codeOpen in Cloud Shell
Create Datasetsource codeOpen in Cloud Shell
Create Jobsource codeOpen in Cloud Shell
Create Modelsource codeOpen in Cloud Shell
Create Partitioned Tablesource codeOpen in Cloud Shell
Create Range Partitioned Tablesource codeOpen in Cloud Shell
Create Routinesource codeOpen in Cloud Shell
Create Routine DDLsource codeOpen in Cloud Shell
Create Tablesource codeOpen in Cloud Shell
Create Table Without Schemasource codeOpen in Cloud Shell
Create Viewsource codeOpen in Cloud Shell
Dataset Existssource codeOpen in Cloud Shell
Delete Datasetsource codeOpen in Cloud Shell
Delete Modelsource codeOpen in Cloud Shell
Delete Routinesource codeOpen in Cloud Shell
Delete Tablesource codeOpen in Cloud Shell
Extract Table To Csvsource codeOpen in Cloud Shell
Extract Table To Jsonsource codeOpen in Cloud Shell
Get Dataset Infosource codeOpen in Cloud Shell
Get Jobsource codeOpen in Cloud Shell
Get Modelsource codeOpen in Cloud Shell
Get Routinesource codeOpen in Cloud Shell
Get Tablesource codeOpen in Cloud Shell
Get Viewsource codeOpen in Cloud Shell
Inserting Data Typessource codeOpen in Cloud Shell
List Datasetssource codeOpen in Cloud Shell
List Modelssource codeOpen in Cloud Shell
List Tablessource codeOpen in Cloud Shell
Load Csv From Gcssource codeOpen in Cloud Shell
Load Csv From Gcs Truncatesource codeOpen in Cloud Shell
Load Local Filesource codeOpen in Cloud Shell
Load Parquetsource codeOpen in Cloud Shell
Load Parquet Replace Tablesource codeOpen in Cloud Shell
Load Partitioned Tablesource codeOpen in Cloud Shell
Load Table Clusteredsource codeOpen in Cloud Shell
Nested Repeated Schemasource codeOpen in Cloud Shell
Querysource codeOpen in Cloud Shell
Query Batchsource codeOpen in Cloud Shell
Query Clustered Tablesource codeOpen in Cloud Shell
Query With Named Parameterssource codeOpen in Cloud Shell
Query With Positional Parameterssource codeOpen in Cloud Shell
Query With Structs Parameterssource codeOpen in Cloud Shell
Quickstart Samplesource codeOpen in Cloud Shell
Relax Column Modesource codeOpen in Cloud Shell
Relax Table Querysource codeOpen in Cloud Shell
Run Legacy Querysource codeOpen in Cloud Shell
Save Query To Tablesource codeOpen in Cloud Shell
Simple Appsource codeOpen in Cloud Shell
Simple Querysource codeOpen in Cloud Shell
Table Insert Rowssource codeOpen in Cloud Shell
Update Dataset Accesssource codeOpen in Cloud Shell
Update Dataset Descriptionsource codeOpen in Cloud Shell
Update Dataset Expirationsource codeOpen in Cloud Shell
Update Table DMLsource codeOpen in Cloud Shell
Update Table Descriptionsource codeOpen in Cloud Shell
Update Table Expirationsource codeOpen in Cloud Shell

Troubleshooting

To get help, follow the instructions in the shared Troubleshooting document.

Java Versions

Java 7 or above is required for using this client.

Versioning

This library follows Semantic Versioning.

Contributing

Contributions to this library are always welcome and highly encouraged.

See CONTRIBUTING for more information how to get started.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.

License

Apache 2.0 - See LICENSE for more information.

CI Status

Java VersionStatus
Java 7Kokoro CI
Java 8Kokoro CI
Java 8 OSXKokoro CI
Java 8 WindowsKokoro CI
Java 11Kokoro CI

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages