Skip to content

Commit ad1e255

Browse files
authored
---
yaml --- r: 8923 b: refs/heads/lesv-patch-1 c: f370cbe h: refs/heads/master i: 8921: 51f31fb 8919: a67b155
1 parent 205882d commit ad1e255

4 files changed

Lines changed: 72 additions & 24 deletions

File tree

  • branches/lesv-patch-1
    • google-cloud-bigquery/src
    • google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ refs/tags/v0.22.0: 18b298fe4bfe8ec2f20b0e0bf7ffdcce5cc3c5fe
6666
refs/heads/vam-google-patch-1: d0c8fee3a4074d0bf7360ce8c4f7f7223d0ee7b9
6767
refs/heads/vam-google-patch-CODEOWNERS: 2ac1616e25229e51d08a984708ef1918f91a35ee
6868
refs/heads/danoscarmike-patch-1: 7342a9916bce4ed00002c7202e2a16c5d46afaea
69-
refs/heads/lesv-patch-1: 7d843ad4523cc73b921737d6eb09fa2eb8d9f006
69+
refs/heads/lesv-patch-1: f370cbe4953bc0fcd62758ee6254f306f8d55f61
7070
refs/heads/ml-update-branch: 079dd6610017f5c51b9d1938c12d6d55b61513cf
7171
refs/heads/vkedia-patch-2: 7d8241388a9769a5c069334761b06c7012c878e7
7272
refs/heads/vkedia-patch-3: 4d128043acaa7db9160faf439d2ca6104e8a88cb

branches/lesv-patch-1/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.cloud.bigquery.BigQuery.TableDataListOption;
2424
import com.google.cloud.bigquery.BigQuery.TableOption;
2525
import com.google.common.collect.ImmutableList;
26-
2726
import java.io.IOException;
2827
import java.io.ObjectInputStream;
2928
import java.util.List;
@@ -289,7 +288,10 @@ public InsertAllResponse insert(Iterable<InsertAllRequest.RowToInsert> rows,
289288
* Returns the paginated list rows in this table.
290289
*
291290
* <p>Example of listing rows in the table.
292-
* <pre> {@code
291+
*
292+
* <pre>{@code
293+
* // This example reads the result 100 rows per RPC call. If there's no need to limit the number,
294+
* // simply omit the option.
293295
* Page<FieldValueList> page = table.list(TableDataListOption.pageSize(100));
294296
* for (FieldValueList row : page.iterateAll()) {
295297
* // do something with the row
@@ -299,11 +301,32 @@ public InsertAllResponse insert(Iterable<InsertAllRequest.RowToInsert> rows,
299301
* @param options table data list options
300302
* @throws BigQueryException upon failure
301303
*/
302-
public Page<FieldValueList> list(TableDataListOption... options)
303-
throws BigQueryException {
304+
public Page<FieldValueList> list(TableDataListOption... options) throws BigQueryException {
304305
return bigquery.listTableData(getTableId(), options);
305306
}
306307

308+
/**
309+
* Returns the paginated list rows in this table.
310+
*
311+
* <p>Example of listing rows in the table.
312+
*
313+
* <pre>{@code
314+
* Schema schema = ...;
315+
* String field = "my_field";
316+
* Page<FieldValueList> page = table.list(schema);
317+
* for (FieldValueList row : page.iterateAll()) {
318+
* row.get(field);
319+
* }
320+
* }</pre>
321+
*
322+
* @param options table data list options
323+
* @throws BigQueryException upon failure
324+
*/
325+
public Page<FieldValueList> list(Schema schema, TableDataListOption... options)
326+
throws BigQueryException {
327+
return bigquery.listTableData(getTableId(), schema, options);
328+
}
329+
307330
/**
308331
* Starts a BigQuery Job to copy the current table to the provided destination table. Returns the
309332
* started {@link Job} object.

branches/lesv-patch-1/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableTest.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.bigquery;
1818

19+
import static com.google.common.truth.Truth.assertThat;
1920
import static org.easymock.EasyMock.createMock;
2021
import static org.easymock.EasyMock.createStrictMock;
2122
import static org.easymock.EasyMock.eq;
@@ -33,8 +34,6 @@
3334
import com.google.cloud.bigquery.InsertAllRequest.RowToInsert;
3435
import com.google.common.collect.ImmutableList;
3536
import com.google.common.collect.ImmutableMap;
36-
import com.google.common.collect.Iterators;
37-
import java.util.Iterator;
3837
import java.util.List;
3938
import org.junit.After;
4039
import org.junit.Test;
@@ -58,9 +57,9 @@ public class TableTest {
5857
JobInfo.of(LoadJobConfiguration.of(TABLE_ID1, ImmutableList.of("URI"), FormatOptions.json()));
5958
private static final JobInfo EXTRACT_JOB_INFO =
6059
JobInfo.of(ExtractJobConfiguration.of(TABLE_ID1, ImmutableList.of("URI"), "CSV"));
61-
private static final Field FIELD = Field.of("FieldName", LegacySQLTypeName.INTEGER);
62-
private static final TableDefinition TABLE_DEFINITION =
63-
StandardTableDefinition.of(Schema.of(FIELD));
60+
private static final Field FIELD = Field.of("FieldName", LegacySQLTypeName.STRING);
61+
private static final Schema SCHEMA = Schema.of(FIELD);
62+
private static final TableDefinition TABLE_DEFINITION = StandardTableDefinition.of(SCHEMA);
6463
private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID1, TABLE_DEFINITION);
6564
private static final List<RowToInsert> ROWS_TO_INSERT = ImmutableList.of(
6665
RowToInsert.of("id1", ImmutableMap.<String, Object>of("key", "val1")),
@@ -82,6 +81,10 @@ public class TableTest {
8281
ImmutableList.of(
8382
FieldValueList.of(ImmutableList.of(FIELD_VALUE1)),
8483
FieldValueList.of(ImmutableList.of(FIELD_VALUE2)));
84+
private static final List<FieldValueList> ROWS_WITH_SCHEMA =
85+
ImmutableList.of(
86+
FieldValueList.of(ImmutableList.of(FIELD_VALUE1)).withSchema(SCHEMA.getFields()),
87+
FieldValueList.of(ImmutableList.of(FIELD_VALUE2)).withSchema(SCHEMA.getFields()));
8588
private BigQuery serviceMockReturnsOptions = createStrictMock(BigQuery.class);
8689
private BigQueryOptions mockOptions = createMock(BigQueryOptions.class);
8790
private BigQuery bigquery;
@@ -269,31 +272,38 @@ public void testInsertComplete() throws Exception {
269272

270273
@Test
271274
public void testList() throws Exception {
275+
Page<FieldValueList> page = new PageImpl<>(null, "c", ROWS);
276+
272277
initializeExpectedTable(1);
273278
expect(bigquery.getOptions()).andReturn(mockOptions);
274-
TableResult tableDataPage = new TableResult(null, ROWS.size(), new PageImpl<>(null, "c", ROWS));
275-
expect(bigquery.listTableData(TABLE_ID1)).andReturn(tableDataPage);
279+
expect(bigquery.listTableData(TABLE_ID1)).andReturn(new TableResult(null, ROWS.size(), page));
280+
expect(bigquery.listTableData(TABLE_ID1, SCHEMA))
281+
.andReturn(new TableResult(SCHEMA, ROWS.size(), page));
276282
replay(bigquery);
277283
initializeTable();
278284
Page<FieldValueList> dataPage = table.list();
279-
Iterator<FieldValueList> tableDataIterator = dataPage.getValues().iterator();
280-
Iterator<FieldValueList> dataIterator = dataPage.getValues().iterator();
281-
assertTrue(Iterators.elementsEqual(tableDataIterator, dataIterator));
285+
assertThat(dataPage.getValues()).containsExactlyElementsIn(ROWS).inOrder();
286+
287+
dataPage = table.list(SCHEMA);
288+
assertThat(dataPage.getValues()).containsExactlyElementsIn(ROWS_WITH_SCHEMA).inOrder();
282289
}
283290

284291
@Test
285292
public void testListWithOptions() throws Exception {
293+
Page<FieldValueList> page = new PageImpl<>(null, "c", ROWS);
286294
initializeExpectedTable(1);
287295
expect(bigquery.getOptions()).andReturn(mockOptions);
288-
TableResult tableDataPage = new TableResult(null, ROWS.size(), new PageImpl<>(null, "c", ROWS));
289296
expect(bigquery.listTableData(TABLE_ID1, BigQuery.TableDataListOption.pageSize(10L)))
290-
.andReturn(tableDataPage);
297+
.andReturn(new TableResult(null, ROWS.size(), page));
298+
expect(bigquery.listTableData(TABLE_ID1, SCHEMA, BigQuery.TableDataListOption.pageSize(10L)))
299+
.andReturn(new TableResult(SCHEMA, ROWS.size(), page));
291300
replay(bigquery);
292301
initializeTable();
293302
Page<FieldValueList> dataPage = table.list(BigQuery.TableDataListOption.pageSize(10L));
294-
Iterator<FieldValueList> tableDataIterator = dataPage.getValues().iterator();
295-
Iterator<FieldValueList> dataIterator = dataPage.getValues().iterator();
296-
assertTrue(Iterators.elementsEqual(tableDataIterator, dataIterator));
303+
assertThat(dataPage.getValues()).containsExactlyElementsIn(ROWS).inOrder();
304+
305+
dataPage = table.list(SCHEMA, BigQuery.TableDataListOption.pageSize(10L));
306+
assertThat(dataPage.getValues()).containsExactlyElementsIn(ROWS_WITH_SCHEMA).inOrder();
297307
}
298308

299309
@Test

branches/lesv-patch-1/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/TableSnippets.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
package com.google.cloud.examples.bigquery.snippets;
2424

25-
import com.google.cloud.bigquery.FieldValueList;
26-
import org.threeten.bp.Duration;
2725
import com.google.api.gax.paging.Page;
2826
import com.google.cloud.RetryOption;
2927
import com.google.cloud.bigquery.BigQuery.JobField;
@@ -32,18 +30,19 @@
3230
import com.google.cloud.bigquery.BigQuery.TableField;
3331
import com.google.cloud.bigquery.BigQuery.TableOption;
3432
import com.google.cloud.bigquery.BigQueryException;
33+
import com.google.cloud.bigquery.FieldValueList;
3534
import com.google.cloud.bigquery.FormatOptions;
3635
import com.google.cloud.bigquery.InsertAllRequest.RowToInsert;
3736
import com.google.cloud.bigquery.InsertAllResponse;
3837
import com.google.cloud.bigquery.Job;
38+
import com.google.cloud.bigquery.Schema;
3939
import com.google.cloud.bigquery.Table;
4040
import com.google.cloud.bigquery.TableId;
41-
4241
import java.util.ArrayList;
4342
import java.util.HashMap;
4443
import java.util.List;
4544
import java.util.Map;
46-
45+
import org.threeten.bp.Duration;
4746

4847
/**
4948
* This class contains a number of snippets for the {@link Table} class.
@@ -168,6 +167,8 @@ public InsertAllResponse insertWithParams(String rowId1, String rowId2) {
168167
// [TARGET list(TableDataListOption...)]
169168
public Page<FieldValueList> list() {
170169
// [START list]
170+
// This example reads the result 100 rows per RPC call. If there's no need to limit the number,
171+
// simply omit the option.
171172
Page<FieldValueList> page = table.list(TableDataListOption.pageSize(100));
172173
for (FieldValueList row : page.iterateAll()) {
173174
// do something with the row
@@ -176,6 +177,20 @@ public Page<FieldValueList> list() {
176177
return page;
177178
}
178179

180+
/** Example of listing rows in the table. */
181+
// [TARGET list(Schema, TableDataListOption...)]
182+
// [VARIABLE ...]
183+
// [VARIABLE "my_field"]
184+
public Page<FieldValueList> list(Schema schema, String field) {
185+
// [START list]
186+
Page<FieldValueList> page = table.list(schema);
187+
for (FieldValueList row : page.iterateAll()) {
188+
row.get(field);
189+
}
190+
// [END list]
191+
return page;
192+
}
193+
179194
/**
180195
* Example of copying the table to a destination table.
181196
*/

0 commit comments

Comments
 (0)