Skip to content
Permalink
Browse files
docs(samples): adding browse table sample and test (#422)
  • Loading branch information
ejdarrow committed Jun 8, 2020
1 parent 3902ba1 commit dff4e5f86764b1c779c2ef131182483e2ffa1c1b
@@ -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]
@@ -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);

}
}

0 comments on commit dff4e5f

Please sign in to comment.