-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): adding browse table sample and test (#422)
- Loading branch information
Showing
2 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
samples/snippets/src/main/java/com/example/bigquery/BrowseTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
81 changes: 81 additions & 0 deletions
81
samples/snippets/src/test/java/com/example/bigquery/BrowseTableIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
} |