Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,63 +20,73 @@
// [START bigquery_simple_app_deps]

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobId;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.TableResult;
import java.util.UUID;

// [END bigquery_simple_app_deps]

public class SimpleApp {

public static void main(String... args) throws Exception {
// [START bigquery_simple_app_client]
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
// [END bigquery_simple_app_client]
// [START bigquery_simple_app_query]
QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(
"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")
// Use standard SQL syntax for queries.
// See: https://cloud.google.com/bigquery/sql-reference/
.setUseLegacySql(false)
.build();
// TODO(developer): Replace these variables before running the app.
String projectId = "MY_PROJECT_ID";
simpleApp(projectId);
}

// Create a job ID so that we can safely retry.
JobId jobId = JobId.of(UUID.randomUUID().toString());
Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
public static void simpleApp(String projectId) {
try {
// [START bigquery_simple_app_client]
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
// [END bigquery_simple_app_client]
// [START bigquery_simple_app_query]
QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(
"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")
// Use standard SQL syntax for queries.
// See: https://cloud.google.com/bigquery/sql-reference/
.setUseLegacySql(false)
.build();

// Wait for the query to complete.
queryJob = queryJob.waitFor();
JobId jobId = JobId.newBuilder().setProject(projectId).build();
Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());

// Check for errors
if (queryJob == null) {
throw new RuntimeException("Job no longer exists");
} else if (queryJob.getStatus().getError() != null) {
// You can also look at queryJob.getStatus().getExecutionErrors() for all
// errors, not just the latest one.
throw new RuntimeException(queryJob.getStatus().getError().toString());
}
// [END bigquery_simple_app_query]
// Wait for the query to complete.
queryJob = queryJob.waitFor();

// Check for errors
if (queryJob == null) {
throw new RuntimeException("Job no longer exists");
} else if (queryJob.getStatus().getError() != null) {
// You can also look at queryJob.getStatus().getExecutionErrors() for all
// errors, not just the latest one.
throw new RuntimeException(queryJob.getStatus().getError().toString());
}
// [END bigquery_simple_app_query]

// [START bigquery_simple_app_print]
// Get the results.
TableResult result = queryJob.getQueryResults();
// [START bigquery_simple_app_print]
// Get the results.
TableResult result = queryJob.getQueryResults();

// Print all pages of the results.
for (FieldValueList row : result.iterateAll()) {
// String type
String url = row.get("url").getStringValue();
String viewCount = row.get("view_count").getStringValue();
System.out.printf("%s : %s views\n", url, viewCount);
// Print all pages of the results.
for (FieldValueList row : result.iterateAll()) {
// String type
String url = row.get("url").getStringValue();
String viewCount = row.get("view_count").getStringValue();
System.out.printf("%s : %s views\n", url, viewCount);
}
} catch (BigQueryException | InterruptedException e) {
System.out.println("Simple App failed due to error: \n" + e.toString());
}
// [END bigquery_simple_app_print]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package com.example.bigquery;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -37,6 +39,20 @@ public class SimpleAppIT {
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");

private static String requireEnvVar(String varName) {
String value = System.getenv(varName);
assertNotNull(
"Environment variable " + varName + " is required to perform these tests.",
System.getenv(varName));
return value;
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_CLOUD_PROJECT");
}

@Before
public void setUp() {
Expand All @@ -56,7 +72,7 @@ public void tearDown() {

@Test
public void testQuickstart() throws Exception {
SimpleApp.main();
SimpleApp.simpleApp(PROJECT_ID);
String got = bout.toString();
assertThat(got).contains("https://stackoverflow.com/questions/");
}
Expand Down