google-big-queryHow do I use Google Big Query with Java?
Google BigQuery is a serverless, highly scalable, and cost-effective cloud data warehouse that enables users to query large datasets using SQL. It can be used with Java by using the BigQuery Java Client Library. This library provides an API to access BigQuery from Java applications.
Example code
// Imports the Google Cloud client library
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
// Instantiates a client
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
This code creates a BigQuery client object which can then be used to execute queries and other operations. For example, the following code executes a query to find the top 10 most popular words in a table:
// The SQL query to run
String query = "SELECT word, word_count FROM `bigquery-public-data.samples.shakespeare` ORDER BY word_count DESC LIMIT 10";
// Creates a query job
Job job = bigquery.create(JobInfo.of(QueryJobConfiguration.of(query)));
// Waits for the query to complete
job = job.waitFor();
// Prints the results
TableResult result = job.getQueryResults();
// Prints the results
for (FieldValueList row : result.iterateAll()) {
String word = row.get("word").getStringValue();
long wordCount = row.get("word_count").getLongValue();
System.out.printf("word: %s, count: %d%n", word, wordCount);
}
Output example
word: the, count: 27378
word: and, count: 26063
word: I, count: 20681
word: to, count: 19261
word: of, count: 18289
word: a, count: 14667
word: you, count: 13617
word: my, count: 12481
word: in, count: 10977
word: that, count: 10090
The code consists of the following parts:
-
Importing the BigQuery client library - The BigQuery Java client library is imported using
import com.google.cloud.bigquery.BigQuery;
andimport com.google.cloud.bigquery.BigQueryOptions;
. -
Creating a BigQuery client object - A BigQuery client object is created using
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
. -
Executing a query - The query is executed using
Job job = bigquery.create(JobInfo.of(QueryJobConfiguration.of(query)));
andjob = job.waitFor();
. -
Printing the results - The results are printed using a
for
loop and theTableResult
object.
For more information, see the BigQuery Java Client Library documentation and the BigQuery Java API reference.
More of Google Big Query
- How do I start using Google Big Query?
- ¿Cuáles son las ventajas y desventajas de usar Google BigQuery?
- How can I use the CASE WHEN statement in Google Big Query?
- How do I use Google Big Query with Excel?
- How can I compare Google BigQuery and Amazon Redshift for software development?
- How do I rename a column in Google BigQuery?
- How do I use Google Big Query to encrypt data?
- How can I use Google Big Query with Udemy?
- How can I create a Google BigQuery table?
- How can I use Google BigQuery to wait for a query to complete?
See more codes...