Skip to content

Commit dcd7daa

Browse files
feat: add connectionId support for external data sources (#776)
Add connecitonId to ExternalTableDefinition to allow external data source query through the BigQuery client.
1 parent 7ce695f commit dcd7daa

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- see http://www.mojohaus.org/clirr-maven-plugin/examples/ignored-differences.html -->
3+
<differences>
4+
<!-- TODO: REMOVE AFTER RELEASE OF CONNECTION_ID FEAT -->
5+
<difference>
6+
<differenceType>7013</differenceType>
7+
<className>com/google/cloud/bigquery/ExternalTableDefinition</className>
8+
<method>java.lang.String getConnectionId()</method>
9+
</difference>
10+
<difference>
11+
<differenceType>7013</differenceType>
12+
<className>com/google/cloud/bigquery/ExternalTableDefinition$Builder</className>
13+
<method>com.google.cloud.bigquery.ExternalTableDefinition$Builder setConnectionId(java.lang.String)</method>
14+
</difference>
15+
</differences>

‎google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ExternalTableDefinition.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ public Builder setFormatOptions(FormatOptions formatOptions) {
124124
*/
125125
public abstract Builder setCompression(String compression);
126126

127+
/**
128+
* [Optional, Trusted Tester] connectionId for external data source. The value may be {@code
129+
* null}.
130+
*/
131+
public abstract Builder setConnectionId(String connectionId);
132+
127133
/**
128134
* [Experimental] Sets detection of schema and format options automatically. Any option
129135
* specified explicitly will be honored.
@@ -158,6 +164,16 @@ abstract Builder setHivePartitioningOptionsInner(
158164
@Nullable
159165
public abstract String getCompression();
160166

167+
/**
168+
* Returns the connection ID used to connect to external data source.
169+
*
170+
* @see <a
171+
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration">
172+
* ConnectionId</a>
173+
*/
174+
@Nullable
175+
public abstract String getConnectionId();
176+
161177
/**
162178
* Returns whether BigQuery should allow extra values that are not represented in the table
163179
* schema. If true, the extra values are ignored. If false, records with extra columns are treated
@@ -249,6 +265,9 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toExternalDataC
249265
if (getCompression() != null) {
250266
externalConfigurationPb.setCompression(getCompression());
251267
}
268+
if (getConnectionId() != null) {
269+
externalConfigurationPb.setConnectionId(getConnectionId());
270+
}
252271
if (ignoreUnknownValues() != null) {
253272
externalConfigurationPb.setIgnoreUnknownValues(ignoreUnknownValues());
254273
}
@@ -415,6 +434,9 @@ static ExternalTableDefinition fromPb(Table tablePb) {
415434
builder.setFormatOptions(FormatOptions.of(externalDataConfiguration.getSourceFormat()));
416435
}
417436
builder.setCompression(externalDataConfiguration.getCompression());
437+
if (externalDataConfiguration.getConnectionId() != null) {
438+
builder.setConnectionId(externalDataConfiguration.getConnectionId());
439+
}
418440
builder.setIgnoreUnknownValues(externalDataConfiguration.getIgnoreUnknownValues());
419441
if (externalDataConfiguration.getCsvOptions() != null) {
420442
builder.setFormatOptions(CsvOptions.fromPb(externalDataConfiguration.getCsvOptions()));
@@ -452,6 +474,9 @@ static ExternalTableDefinition fromExternalDataConfiguration(
452474
if (externalDataConfiguration.getCompression() != null) {
453475
builder.setCompression(externalDataConfiguration.getCompression());
454476
}
477+
if (externalDataConfiguration.getConnectionId() != null) {
478+
builder.setConnectionId(externalDataConfiguration.getConnectionId());
479+
}
455480
if (externalDataConfiguration.getIgnoreUnknownValues() != null) {
456481
builder.setIgnoreUnknownValues(externalDataConfiguration.getIgnoreUnknownValues());
457482
}

‎google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ExternalTableDefinitionTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class ExternalTableDefinitionTest {
4646
private static final Integer MAX_BAD_RECORDS = 42;
4747
private static final Boolean IGNORE_UNKNOWN_VALUES = true;
4848
private static final String COMPRESSION = "GZIP";
49+
private static final String CONNECTION_ID = "123456789";
4950
private static final Boolean AUTODETECT = true;
5051
private static final CsvOptions CSV_OPTIONS = CsvOptions.newBuilder().build();
5152
private static final HivePartitioningOptions HIVE_PARTITIONING_OPTIONS =
@@ -56,6 +57,7 @@ public class ExternalTableDefinitionTest {
5657
private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION =
5758
ExternalTableDefinition.newBuilder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS)
5859
.setCompression(COMPRESSION)
60+
.setConnectionId(CONNECTION_ID)
5961
.setIgnoreUnknownValues(IGNORE_UNKNOWN_VALUES)
6062
.setMaxBadRecords(MAX_BAD_RECORDS)
6163
.setAutodetect(AUTODETECT)
@@ -67,10 +69,19 @@ public void testToBuilder() {
6769
compareExternalTableDefinition(
6870
EXTERNAL_TABLE_DEFINITION, EXTERNAL_TABLE_DEFINITION.toBuilder().build());
6971
ExternalTableDefinition externalTableDefinition =
70-
EXTERNAL_TABLE_DEFINITION.toBuilder().setCompression("NONE").build();
72+
EXTERNAL_TABLE_DEFINITION
73+
.toBuilder()
74+
.setCompression("NONE")
75+
.setConnectionId("00000")
76+
.build();
7177
assertEquals("NONE", externalTableDefinition.getCompression());
78+
assertEquals("00000", externalTableDefinition.getConnectionId());
7279
externalTableDefinition =
73-
externalTableDefinition.toBuilder().setCompression(COMPRESSION).build();
80+
externalTableDefinition
81+
.toBuilder()
82+
.setCompression(COMPRESSION)
83+
.setConnectionId(CONNECTION_ID)
84+
.build();
7485
compareExternalTableDefinition(EXTERNAL_TABLE_DEFINITION, externalTableDefinition);
7586
}
7687

@@ -94,6 +105,7 @@ public void testTypeNullPointerException() {
94105
public void testBuilder() {
95106
assertEquals(TableDefinition.Type.EXTERNAL, EXTERNAL_TABLE_DEFINITION.getType());
96107
assertEquals(COMPRESSION, EXTERNAL_TABLE_DEFINITION.getCompression());
108+
assertEquals(CONNECTION_ID, EXTERNAL_TABLE_DEFINITION.getConnectionId());
97109
assertEquals(CSV_OPTIONS, EXTERNAL_TABLE_DEFINITION.getFormatOptions());
98110
assertEquals(IGNORE_UNKNOWN_VALUES, EXTERNAL_TABLE_DEFINITION.ignoreUnknownValues());
99111
assertEquals(MAX_BAD_RECORDS, EXTERNAL_TABLE_DEFINITION.getMaxBadRecords());
@@ -119,6 +131,7 @@ private void compareExternalTableDefinition(
119131
ExternalTableDefinition expected, ExternalTableDefinition value) {
120132
assertEquals(expected, value);
121133
assertEquals(expected.getCompression(), value.getCompression());
134+
assertEquals(expected.getConnectionId(), value.getConnectionId());
122135
assertEquals(expected.getFormatOptions(), value.getFormatOptions());
123136
assertEquals(expected.ignoreUnknownValues(), value.ignoreUnknownValues());
124137
assertEquals(expected.getMaxBadRecords(), value.getMaxBadRecords());

0 commit comments

Comments
 (0)