Skip to content

Commit 45f5661

Browse files
committed
Merge pull request #514 from mziccard/bigquery-template-suffix
Add support for templateSuffix to BigQuery InsertAllRequest
2 parents dac9cbf + acbd5c0 commit 45f5661

5 files changed

Lines changed: 111 additions & 17 deletions

File tree

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ public InsertAllResponse insertAll(InsertAllRequest request) throws BigQueryExce
412412
final TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest();
413413
requestPb.setIgnoreUnknownValues(request.ignoreUnknownValues());
414414
requestPb.setSkipInvalidRows(request.skipInvalidRows());
415+
requestPb.setTemplateSuffix(request.templateSuffix());
415416
List<Rows> rowsPb = Lists.transform(request.rows(), new Function<RowToInsert, Rows>() {
416417
@Override
417418
public Rows apply(RowToInsert rowToInsert) {

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/InsertAllRequest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class InsertAllRequest implements Serializable {
4545
private final List<RowToInsert> rows;
4646
private final Boolean skipInvalidRows;
4747
private final Boolean ignoreUnknownValues;
48+
private final String templateSuffix;
4849

4950
/**
5051
* A Google Big Query row to be inserted into a table. Each {@code RowToInsert} has an associated
@@ -140,6 +141,7 @@ public static final class Builder {
140141
private List<RowToInsert> rows;
141142
private Boolean skipInvalidRows;
142143
private Boolean ignoreUnknownValues;
144+
private String templateSuffix;
143145

144146
private Builder() {}
145147

@@ -231,6 +233,20 @@ public Builder ignoreUnknownValues(boolean ignoreUnknownValues) {
231233
return this;
232234
}
233235

236+
/**
237+
* If specified, the destination table is treated as a base template. Rows are inserted into an
238+
* instance table named "{destination}{templateSuffix}". BigQuery will manage the creation of
239+
* the instance table, using the schema of the base template table.
240+
*
241+
* @see <a
242+
* href="https://cloud.google.com/bigquery/streaming-data-into-bigquery#template-tables">
243+
* Template Tables</a>
244+
*/
245+
public Builder templateSuffix(String templateSuffix) {
246+
this.templateSuffix = templateSuffix;
247+
return this;
248+
}
249+
234250
public InsertAllRequest build() {
235251
return new InsertAllRequest(this);
236252
}
@@ -241,6 +257,7 @@ private InsertAllRequest(Builder builder) {
241257
this.rows = ImmutableList.copyOf(checkNotNull(builder.rows));
242258
this.ignoreUnknownValues = builder.ignoreUnknownValues;
243259
this.skipInvalidRows = builder.skipInvalidRows;
260+
this.templateSuffix = builder.templateSuffix;
244261
}
245262

246263
/**
@@ -273,6 +290,19 @@ public Boolean skipInvalidRows() {
273290
return skipInvalidRows;
274291
}
275292

293+
/**
294+
* If specified, the destination table is treated as a base template. Rows are inserted into an
295+
* instance table named "{destination}{templateSuffix}". BigQuery will manage the creation of the
296+
* instance table, using the schema of the base template table.
297+
*
298+
* @see <a
299+
* href="https://cloud.google.com/bigquery/streaming-data-into-bigquery#template-tables">
300+
* Template Tables</a>
301+
*/
302+
public String templateSuffix() {
303+
return templateSuffix;
304+
}
305+
276306
/**
277307
* Returns a builder for an {@code InsertAllRequest} object given the destination table.
278308
*/
@@ -384,12 +414,13 @@ public String toString() {
384414
.add("rows", rows)
385415
.add("ignoreUnknownValues", ignoreUnknownValues)
386416
.add("skipInvalidRows", skipInvalidRows)
417+
.add("templateSuffix", templateSuffix)
387418
.toString();
388419
}
389420

390421
@Override
391422
public int hashCode() {
392-
return Objects.hash(table, rows, ignoreUnknownValues, skipInvalidRows);
423+
return Objects.hash(table, rows, ignoreUnknownValues, skipInvalidRows, templateSuffix);
393424
}
394425

395426
@Override
@@ -401,6 +432,7 @@ public boolean equals(Object obj) {
401432
return Objects.equals(table, other.table)
402433
&& Objects.equals(rows, other.rows)
403434
&& Objects.equals(ignoreUnknownValues, other.ignoreUnknownValues)
404-
&& Objects.equals(skipInvalidRows, other.skipInvalidRows);
435+
&& Objects.equals(skipInvalidRows, other.skipInvalidRows)
436+
&& Objects.equals(templateSuffix, other.templateSuffix);
405437
}
406438
}

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ public void testInsertAll() {
614614
.rows(rows)
615615
.skipInvalidRows(false)
616616
.ignoreUnknownValues(true)
617+
.templateSuffix("suffix")
617618
.build();
618619
TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest().setRows(
619620
Lists.transform(rows, new Function<RowToInsert, TableDataInsertAllRequest.Rows>() {
@@ -623,7 +624,7 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) {
623624
.setJson(rowToInsert.content());
624625
}
625626
})
626-
).setSkipInvalidRows(false).setIgnoreUnknownValues(true);
627+
).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix");
627628
TableDataInsertAllResponse responsePb = new TableDataInsertAllResponse().setInsertErrors(
628629
ImmutableList.of(new TableDataInsertAllResponse.InsertErrors().setIndex(0L).setErrors(
629630
ImmutableList.of(new ErrorProto().setMessage("ErrorMessage")))));

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ITBigQueryTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,41 @@ public void testInsertAll() {
475475
assertTrue(bigquery.delete(TableId.of(DATASET, tableName)));
476476
}
477477

478+
@Test
479+
public void testInsertAllWithSuffix() {
480+
String tableName = "test_insert_all_with_suffix_table";
481+
BaseTableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), TABLE_SCHEMA);
482+
assertNotNull(bigquery.create(tableInfo));
483+
InsertAllRequest request = InsertAllRequest.builder(tableInfo.tableId())
484+
.addRow(ImmutableMap.<String, Object>of(
485+
"TimestampField", "2014-08-19 07:41:35.220 -05:00",
486+
"StringField", "stringValue",
487+
"IntegerField", ImmutableList.of(0, 1),
488+
"BooleanField", false,
489+
"RecordField", ImmutableMap.of(
490+
"TimestampField", "1969-07-20 20:18:04 UTC",
491+
"IntegerField", ImmutableList.of(1, 0),
492+
"BooleanField", true)))
493+
.addRow(ImmutableMap.<String, Object>of(
494+
"TimestampField", "2014-08-19 07:41:35.220 -05:00",
495+
"StringField", "stringValue",
496+
"IntegerField", ImmutableList.of(0, 1),
497+
"BooleanField", false,
498+
"RecordField", ImmutableMap.of(
499+
"TimestampField", "1969-07-20 20:18:04 UTC",
500+
"IntegerField", ImmutableList.of(1, 0),
501+
"BooleanField", true)))
502+
.templateSuffix("_suffix")
503+
.build();
504+
InsertAllResponse response = bigquery.insertAll(request);
505+
assertFalse(response.hasErrors());
506+
assertEquals(0, response.insertErrors().size());
507+
String newTableName = tableName + "_suffix";
508+
assertNotNull(bigquery.getTable(DATASET, newTableName, TableOption.fields()));
509+
assertTrue(bigquery.delete(TableId.of(DATASET, tableName)));
510+
assertTrue(bigquery.delete(TableId.of(DATASET, newTableName)));
511+
}
512+
478513
@Test
479514
public void testInsertAllWithErrors() {
480515
String tableName = "test_insert_all_with_errors_table";

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/InsertAllRequestTest.java

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNull;
2122
import static org.junit.Assert.assertTrue;
2223

2324
import com.google.common.collect.ImmutableList;
@@ -45,6 +46,7 @@ public class InsertAllRequestTest {
4546
private static final BaseTableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_SCHEMA);
4647
private static final boolean SKIP_INVALID_ROWS = true;
4748
private static final boolean IGNORE_UNKNOWN_VALUES = false;
49+
private static final String TEMPLATE_SUFFIX = "templateSuffix";
4850
private static final InsertAllRequest INSERT_ALL_REQUEST1 = InsertAllRequest.builder(TABLE_ID)
4951
.addRow(CONTENT1)
5052
.addRow(CONTENT2)
@@ -90,20 +92,25 @@ public class InsertAllRequestTest {
9092
.ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
9193
.skipInvalidRows(SKIP_INVALID_ROWS)
9294
.build();
93-
private static final InsertAllRequest INSERT_ALL_REQUEST9 =
94-
InsertAllRequest.builder(TABLE_INFO)
95-
.addRow("id1", CONTENT1)
96-
.addRow("id2", CONTENT2)
97-
.ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
98-
.skipInvalidRows(SKIP_INVALID_ROWS)
99-
.build();
100-
private static final InsertAllRequest INSERT_ALL_REQUEST10 =
101-
InsertAllRequest.builder(TABLE_INFO)
102-
.addRow("id1", CONTENT1)
103-
.addRow("id2", CONTENT2)
104-
.ignoreUnknownValues(true)
105-
.skipInvalidRows(false)
106-
.build();
95+
private static final InsertAllRequest INSERT_ALL_REQUEST9 = InsertAllRequest.builder(TABLE_INFO)
96+
.addRow("id1", CONTENT1)
97+
.addRow("id2", CONTENT2)
98+
.ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
99+
.skipInvalidRows(SKIP_INVALID_ROWS)
100+
.build();
101+
private static final InsertAllRequest INSERT_ALL_REQUEST10 = InsertAllRequest.builder(TABLE_INFO)
102+
.addRow("id1", CONTENT1)
103+
.addRow("id2", CONTENT2)
104+
.ignoreUnknownValues(true)
105+
.skipInvalidRows(false)
106+
.build();
107+
private static final InsertAllRequest INSERT_ALL_REQUEST11 = InsertAllRequest.builder(TABLE_INFO)
108+
.addRow("id1", CONTENT1)
109+
.addRow("id2", CONTENT2)
110+
.ignoreUnknownValues(true)
111+
.skipInvalidRows(false)
112+
.templateSuffix(TEMPLATE_SUFFIX)
113+
.build();
107114

108115
@Test
109116
public void testBuilder() {
@@ -117,6 +124,7 @@ public void testBuilder() {
117124
assertEquals(TABLE_ID, INSERT_ALL_REQUEST8.table());
118125
assertEquals(TABLE_ID, INSERT_ALL_REQUEST9.table());
119126
assertEquals(TABLE_ID, INSERT_ALL_REQUEST10.table());
127+
assertEquals(TABLE_ID, INSERT_ALL_REQUEST11.table());
120128
assertEquals(ROWS, INSERT_ALL_REQUEST1.rows());
121129
assertEquals(ROWS, INSERT_ALL_REQUEST2.rows());
122130
assertEquals(ROWS, INSERT_ALL_REQUEST4.rows());
@@ -127,6 +135,7 @@ public void testBuilder() {
127135
assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST8.rows());
128136
assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST9.rows());
129137
assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST10.rows());
138+
assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST11.rows());
130139
assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST1.skipInvalidRows());
131140
assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST2.skipInvalidRows());
132141
assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST3.skipInvalidRows());
@@ -137,6 +146,7 @@ public void testBuilder() {
137146
assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST8.skipInvalidRows());
138147
assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST9.skipInvalidRows());
139148
assertFalse(INSERT_ALL_REQUEST10.skipInvalidRows());
149+
assertFalse(INSERT_ALL_REQUEST11.skipInvalidRows());
140150
assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST1.ignoreUnknownValues());
141151
assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST2.ignoreUnknownValues());
142152
assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST3.ignoreUnknownValues());
@@ -147,6 +157,18 @@ public void testBuilder() {
147157
assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST8.ignoreUnknownValues());
148158
assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST9.ignoreUnknownValues());
149159
assertTrue(INSERT_ALL_REQUEST10.ignoreUnknownValues());
160+
assertTrue(INSERT_ALL_REQUEST11.ignoreUnknownValues());
161+
assertNull(INSERT_ALL_REQUEST1.templateSuffix());
162+
assertNull(INSERT_ALL_REQUEST2.templateSuffix());
163+
assertNull(INSERT_ALL_REQUEST3.templateSuffix());
164+
assertNull(INSERT_ALL_REQUEST4.templateSuffix());
165+
assertNull(INSERT_ALL_REQUEST5.templateSuffix());
166+
assertNull(INSERT_ALL_REQUEST6.templateSuffix());
167+
assertNull(INSERT_ALL_REQUEST7.templateSuffix());
168+
assertNull(INSERT_ALL_REQUEST8.templateSuffix());
169+
assertNull(INSERT_ALL_REQUEST9.templateSuffix());
170+
assertNull(INSERT_ALL_REQUEST10.templateSuffix());
171+
assertEquals(TEMPLATE_SUFFIX, INSERT_ALL_REQUEST11.templateSuffix());
150172
}
151173

152174
@Test
@@ -183,6 +205,8 @@ public void testEquals() {
183205
compareInsertAllRequest(INSERT_ALL_REQUEST5, INSERT_ALL_REQUEST7);
184206
compareInsertAllRequest(INSERT_ALL_REQUEST7, INSERT_ALL_REQUEST8);
185207
compareInsertAllRequest(INSERT_ALL_REQUEST8, INSERT_ALL_REQUEST9);
208+
compareInsertAllRequest(INSERT_ALL_REQUEST10, INSERT_ALL_REQUEST10);
209+
compareInsertAllRequest(INSERT_ALL_REQUEST11, INSERT_ALL_REQUEST11);
186210
}
187211

188212
private void compareInsertAllRequest(InsertAllRequest expected, InsertAllRequest value) {
@@ -193,5 +217,6 @@ private void compareInsertAllRequest(InsertAllRequest expected, InsertAllRequest
193217
assertEquals(expected.rows(), value.rows());
194218
assertEquals(expected.ignoreUnknownValues(), value.ignoreUnknownValues());
195219
assertEquals(expected.skipInvalidRows(), value.skipInvalidRows());
220+
assertEquals(expected.templateSuffix(), value.templateSuffix());
196221
}
197222
}

0 commit comments

Comments
 (0)