Skip to content

Commit 47e805a

Browse files
authored
bigquery: document new methods (googleapis#2709)
1 parent ad69088 commit 47e805a

File tree

3 files changed

+100
-10
lines changed

3 files changed

+100
-10
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,8 @@ public int hashCode() {
865865
* <pre>{@code
866866
* String datasetName = "my_dataset_name";
867867
* String tableName = "my_table_name";
868+
* // This example reads the result 100 rows per RPC call. If there's no need to limit the number,
869+
* // simply omit the option.
868870
* TableResult tableData =
869871
* bigquery.listTableData(datasetName, tableName, TableDataListOption.pageSize(100));
870872
* for (FieldValueList row : tableData.iterateAll()) {
@@ -885,6 +887,8 @@ public int hashCode() {
885887
* String datasetName = "my_dataset_name";
886888
* String tableName = "my_table_name";
887889
* TableId tableIdObject = TableId.of(datasetName, tableName);
890+
* // This example reads the result 100 rows per RPC call. If there's no need to limit the number,
891+
* // simply omit the option.
888892
* TableResult tableData =
889893
* bigquery.listTableData(tableIdObject, TableDataListOption.pageSize(100));
890894
* for (FieldValueList row : rowIterator.hasNext()) {
@@ -896,9 +900,51 @@ public int hashCode() {
896900
*/
897901
TableResult listTableData(TableId tableId, TableDataListOption... options);
898902

903+
/**
904+
* Lists the table's rows. If the {@code schema} is not {@code null}, it is available to the
905+
* {@link FieldValueList} iterated over.
906+
*
907+
* <p>Example of listing table rows.
908+
*
909+
* <pre>{@code
910+
* String datasetName = "my_dataset_name";
911+
* String tableName = "my_table_name";
912+
* Schema schema = ...;
913+
* String field = "my_field";
914+
* Page<FieldValueList> tableData =
915+
* bigquery.listTableData(datasetName, tableName, schema);
916+
* for (FieldValueList row : tableData.iterateAll()) {
917+
* row.get(field)
918+
* }
919+
* }</pre>
920+
*
921+
* @throws BigQueryException upon failure
922+
*/
899923
TableResult listTableData(
900924
String datasetId, String tableId, Schema schema, TableDataListOption... options);
901925

926+
/**
927+
* Lists the table's rows. If the {@code schema} is not {@code null}, it is available to the
928+
* {@link FieldValueList} iterated over.
929+
*
930+
* <p>Example of listing table rows.
931+
*
932+
* <pre>{@code
933+
* Schema schema =
934+
* Schema.of(
935+
* Field.of("word", LegacySQLTypeName.STRING),
936+
* Field.of("word_count", LegacySQLTypeName.STRING),
937+
* Field.of("corpus", LegacySQLTypeName.STRING),
938+
* Field.of("corpus_date", LegacySQLTypeName.STRING));
939+
* Page<FieldValueList> page =
940+
* bigquery.listTableData(
941+
* TableId.of("bigquery-public-data", "samples", "shakespeare"), schema);
942+
* FieldValueList row = page.getValues().iterator().next();
943+
* System.out.println(row.get("word").getStringValue());
944+
* }</pre>
945+
*
946+
* @throws BigQueryException upon failure
947+
*/
902948
TableResult listTableData(TableId tableId, Schema schema, TableDataListOption... options);
903949

904950
/**

google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/BigQuerySnippets.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ public Table createTable(String datasetName, String tableName, String fieldName)
438438
// [VARIABLE "my_table_name"]
439439
public Page<FieldValueList> listTableData(String datasetName, String tableName) {
440440
// [START listTableData]
441+
// This example reads the result 100 rows per RPC call. If there's no need to limit the number,
442+
// simply omit the option.
441443
Page<FieldValueList> tableData =
442444
bigquery.listTableData(datasetName, tableName, TableDataListOption.pageSize(100));
443445
for (FieldValueList row : tableData.iterateAll()) {
@@ -456,6 +458,8 @@ public Page<FieldValueList> listTableData(String datasetName, String tableName)
456458
public Page<FieldValueList> listTableDataFromId(String datasetName, String tableName) {
457459
// [START listTableDataFromId]
458460
TableId tableIdObject = TableId.of(datasetName, tableName);
461+
// This example reads the result 100 rows per RPC call. If there's no need to limit the number,
462+
// simply omit the option.
459463
Page<FieldValueList> tableData =
460464
bigquery.listTableData(tableIdObject, TableDataListOption.pageSize(100));
461465
for (FieldValueList row : tableData.iterateAll()) {
@@ -465,6 +469,43 @@ public Page<FieldValueList> listTableDataFromId(String datasetName, String table
465469
return tableData;
466470
}
467471

472+
/** Example of listing table rows with schema. */
473+
// [TARGET listTableData(String, String, Schema, TableDataListOption...)]
474+
// [VARIABLE "my_dataset_name"]
475+
// [VARIABLE "my_table_name"]
476+
// [VARIABLE ...]
477+
// [VARIABLE "field"]
478+
public Page<FieldValueList> listTableDataSchema(
479+
String datasetName, String tableName, Schema schema, String field) {
480+
// [START listTableDataSchema]
481+
Page<FieldValueList> tableData =
482+
bigquery.listTableData(datasetName, tableName, schema);
483+
for (FieldValueList row : tableData.iterateAll()) {
484+
row.get(field);
485+
}
486+
// [END listTableDataSchema]
487+
return tableData;
488+
}
489+
490+
/** Example of listing table rows with schema. */
491+
// [TARGET listTableData(TableId, Schema, TableDataListOption...)]
492+
public FieldValueList listTableDataSchemaId() {
493+
// [START listTableDataSchemaId]
494+
Schema schema =
495+
Schema.of(
496+
Field.of("word", LegacySQLTypeName.STRING),
497+
Field.of("word_count", LegacySQLTypeName.STRING),
498+
Field.of("corpus", LegacySQLTypeName.STRING),
499+
Field.of("corpus_date", LegacySQLTypeName.STRING));
500+
Page<FieldValueList> page =
501+
bigquery.listTableData(
502+
TableId.of("bigquery-public-data", "samples", "shakespeare"), schema);
503+
FieldValueList row = page.getValues().iterator().next();
504+
System.out.println(row.get("word").getStringValue());
505+
// [END listTableDataSchemaId]
506+
return row;
507+
}
508+
468509
/**
469510
* Example of creating a query job.
470511
*/

google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITBigQuerySnippets.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.assertNotNull;
23-
import static org.junit.Assert.assertNull;
2423
import static org.junit.Assert.assertTrue;
2524

2625
import com.google.api.gax.paging.Page;
@@ -35,9 +34,7 @@
3534
import com.google.cloud.bigquery.Job;
3635
import com.google.cloud.bigquery.JobId;
3736
import com.google.cloud.bigquery.LegacySQLTypeName;
38-
import com.google.cloud.bigquery.QueryResponse;
3937
import com.google.cloud.bigquery.Schema;
40-
import com.google.cloud.bigquery.JobStatus;
4138
import com.google.cloud.bigquery.StandardTableDefinition;
4239
import com.google.cloud.bigquery.Table;
4340
import com.google.cloud.bigquery.TableId;
@@ -47,13 +44,6 @@
4744
import com.google.common.collect.Iterators;
4845
import com.google.common.collect.Sets;
4946
import com.google.common.io.Resources;
50-
51-
import org.junit.AfterClass;
52-
import org.junit.BeforeClass;
53-
import org.junit.Rule;
54-
import org.junit.Test;
55-
import org.junit.rules.Timeout;
56-
5747
import java.io.IOException;
5848
import java.net.URISyntaxException;
5949
import java.nio.file.Path;
@@ -62,6 +52,11 @@
6252
import java.util.Set;
6353
import java.util.concurrent.ExecutionException;
6454
import java.util.concurrent.TimeoutException;
55+
import org.junit.AfterClass;
56+
import org.junit.BeforeClass;
57+
import org.junit.Rule;
58+
import org.junit.Test;
59+
import org.junit.rules.Timeout;
6560

6661
public class ITBigQuerySnippets {
6762

@@ -230,6 +225,14 @@ public void testInsertAllAndListTableData() throws IOException, InterruptedExcep
230225
assertEquals(true, row.get(0).getBooleanValue());
231226
assertArrayEquals(new byte[]{0xA, 0xD, 0xD, 0xE, 0xD}, row.get(1).getBytesValue());
232227
assertEquals("Hello, World!", row.get(2).getRecordValue().get(0).getStringValue());
228+
229+
listPage = bigquerySnippets.listTableDataSchema(DATASET, tableName, schema, fieldName1);
230+
row = listPage.getValues().iterator().next();
231+
assertEquals(true, row.get(fieldName1));
232+
assertArrayEquals(new byte[] {0xA, 0xD, 0xD, 0xE, 0xD}, row.get(fieldName2).getBytesValue());
233+
234+
bigquerySnippets.listTableDataSchemaId();
235+
233236
assertTrue(bigquerySnippets.deleteTable(DATASET, tableName));
234237
}
235238

0 commit comments

Comments
 (0)