From dff4e5f86764b1c779c2ef131182483e2ffa1c1b Mon Sep 17 00:00:00 2001 From: Emily Darrow <47046797+ejdarrow@users.noreply.github.com> Date: Mon, 8 Jun 2020 14:32:07 -0400 Subject: [PATCH] docs(samples): adding browse table sample and test (#422) --- .../com/example/bigquery/BrowseTable.java | 64 +++++++++++++++ .../com/example/bigquery/BrowseTableIT.java | 81 +++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/bigquery/BrowseTable.java create mode 100644 samples/snippets/src/test/java/com/example/bigquery/BrowseTableIT.java diff --git a/samples/snippets/src/main/java/com/example/bigquery/BrowseTable.java b/samples/snippets/src/main/java/com/example/bigquery/BrowseTable.java new file mode 100644 index 000000000..7228dfb6e --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/BrowseTable.java @@ -0,0 +1,64 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +// [START bigquery_browse_table] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQuery.TableDataListOption; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.TableId; +import com.google.cloud.bigquery.TableResult; + +// Sample to directly browse a table with optional paging +public class BrowseTable { + + public static void runBrowseTable() { + // TODO(developer): Replace these variables before running the sample. + String table = "MY_TABLE_NAME"; + String dataset = "MY_DATASET_NAME"; + browseTable(dataset, table); + } + + public static void browseTable(String dataset, String table) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Identify the table itself + TableId tableId = TableId.of(dataset, table); + + // Page over 100 records. If you don't need pagination, remove the pageSize parameter. + TableResult result = + bigquery.listTableData(tableId, TableDataListOption.pageSize(100)); + + // Print the records + result.iterateAll().forEach(row -> { + row.forEach(fieldValue -> + System.out.print(fieldValue.toString() + ", ") + ); + System.out.println(); + }); + + System.out.println("Query ran successfully"); + } catch (BigQueryException e) { + System.out.println("Query failed to run \n" + e.toString()); + } + } +} +// [END bigquery_browse_table] diff --git a/samples/snippets/src/test/java/com/example/bigquery/BrowseTableIT.java b/samples/snippets/src/test/java/com/example/bigquery/BrowseTableIT.java new file mode 100644 index 000000000..b88a15e66 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/BrowseTableIT.java @@ -0,0 +1,81 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class BrowseTableIT { + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testBrowseTable() { + String tableName = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_"); + + Schema schema = + Schema.of( + Field.of("stringField", StandardSQLTypeName.STRING), + Field.of("booleanField", StandardSQLTypeName.BOOL)); + + CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema); + + BrowseTable.browseTable(BIGQUERY_DATASET_NAME, tableName); + + assertThat(bout.toString()).contains("Query ran successfully"); + + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + + } +}