Skip to content

Commit 975df05

Browse files
feat: Add support for ObjectMetadata (#3217)
* feat: Add support for ObjectMetadata Fixes #3216 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent d45d168 commit 975df05

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

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

+50
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ public Builder setHivePartitioningOptions(HivePartitioningOptions hivePartitioni
180180
abstract Builder setHivePartitioningOptionsInner(
181181
HivePartitioningOptions hivePartitioningOptions);
182182

183+
public Builder setObjectMetadata(String objectMetadata) {
184+
return setObjectMetadataInner(objectMetadata);
185+
}
186+
187+
abstract Builder setObjectMetadataInner(String objectMetadata);
188+
183189
/** Creates an {@code ExternalTableDefinition} object. */
184190
@Override
185191
public abstract ExternalTableDefinition build();
@@ -255,6 +261,21 @@ public String getFileSetSpecType() {
255261
@Nullable
256262
public abstract ImmutableList<String> getSourceUrisImmut();
257263

264+
/**
265+
* Returns the object metadata.
266+
*
267+
* @see <a
268+
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration">
269+
* ObjectMetadata</a>
270+
*/
271+
@Nullable
272+
public String getObjectMetadata() {
273+
return getObjectMetadataInner();
274+
}
275+
276+
@Nullable
277+
abstract String getObjectMetadataInner();
278+
258279
/**
259280
* Returns the source format, and possibly some parsing options, of the external data. Supported
260281
* formats are {@code CSV} and {@code NEWLINE_DELIMITED_JSON}.
@@ -362,6 +383,10 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toExternalDataC
362383
externalConfigurationPb.setFileSetSpecType(getFileSetSpecType());
363384
}
364385

386+
if (getObjectMetadata() != null) {
387+
externalConfigurationPb.setObjectMetadata(getObjectMetadata());
388+
}
389+
365390
return externalConfigurationPb;
366391
}
367392

@@ -426,6 +451,24 @@ public static Builder newBuilder(String sourceUri, FormatOptions format) {
426451
return newBuilder().setSourceUris(ImmutableList.of(sourceUri)).setFormatOptions(format);
427452
}
428453

454+
/**
455+
* Creates a builder for an ExternalTableDefinition object.
456+
*
457+
* @param sourceUri the fully-qualified URIs that point to your data in Google Cloud. For Google
458+
* Cloud Bigtable URIs: Exactly one URI can be specified and it has be a fully specified and
459+
* valid HTTPS URL for a Google Cloud Bigtable table. Size limits related to load jobs apply
460+
* to external data sources, plus an additional limit of 10 GB maximum size across all URIs.
461+
* @return a builder for an ExternalTableDefinition object given source URIs and format
462+
* @see <a href="https://cloud.google.com/bigquery/loading-data-into-bigquery#quota">Quota</a>
463+
* @see <a
464+
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
465+
* Source Format</a>
466+
*/
467+
public static Builder newBuilder(String sourceUri) {
468+
checkArgument(!isNullOrEmpty(sourceUri), "Provided sourceUri is null or empty");
469+
return newBuilder().setSourceUris(ImmutableList.of(sourceUri));
470+
}
471+
429472
/**
430473
* Creates an ExternalTableDefinition object.
431474
*
@@ -534,6 +577,9 @@ static ExternalTableDefinition fromPb(Table tablePb) {
534577
if (externalDataConfiguration.getFileSetSpecType() != null) {
535578
builder.setFileSetSpecType(externalDataConfiguration.getFileSetSpecType());
536579
}
580+
if (externalDataConfiguration.getObjectMetadata() != null) {
581+
builder.setObjectMetadata(externalDataConfiguration.getObjectMetadata());
582+
}
537583
}
538584
return builder.build();
539585
}
@@ -597,6 +643,10 @@ static ExternalTableDefinition fromExternalDataConfiguration(
597643
builder.setFileSetSpecType(externalDataConfiguration.getFileSetSpecType());
598644
}
599645

646+
if (externalDataConfiguration.getObjectMetadata() != null) {
647+
builder.setObjectMetadata(externalDataConfiguration.getObjectMetadata());
648+
}
649+
600650
return builder.build();
601651
}
602652
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class ExternalTableDefinitionTest {
5858
.setMode("AUTO")
5959
.setSourceUriPrefix(SOURCE_URIS.get(0))
6060
.build();
61+
private static final String OBJECT_METADATA = "SIMPLE";
6162
private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION =
6263
ExternalTableDefinition.newBuilder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS)
6364
.setFileSetSpecType("FILE_SET_SPEC_TYPE_FILE_SYSTEM_MATCH")
@@ -68,6 +69,7 @@ public class ExternalTableDefinitionTest {
6869
.setMaxBadRecords(MAX_BAD_RECORDS)
6970
.setAutodetect(AUTODETECT)
7071
.setHivePartitioningOptions(HIVE_PARTITIONING_OPTIONS)
72+
.setObjectMetadata(OBJECT_METADATA)
7173
.build();
7274

7375
private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION_AVRO =
@@ -167,5 +169,6 @@ private void compareExternalTableDefinition(
167169
assertEquals(expected.hashCode(), value.hashCode());
168170
assertEquals(expected.getAutodetect(), value.getAutodetect());
169171
assertEquals(expected.getHivePartitioningOptions(), value.getHivePartitioningOptions());
172+
assertEquals(expected.getObjectMetadata(), value.getObjectMetadata());
170173
}
171174
}

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

+43
Original file line numberDiff line numberDiff line change
@@ -6579,6 +6579,49 @@ public void testExternalTableMetadataCachingNotEnable() throws InterruptedExcept
65796579
assertTrue(remoteTable.delete());
65806580
}
65816581

6582+
@Test
6583+
public void testObjectTable() throws InterruptedException {
6584+
String tableName = "test_object_table";
6585+
TableId tableId = TableId.of(DATASET, tableName);
6586+
6587+
String sourceUri = "gs://" + BUCKET + "/" + JSON_LOAD_FILE;
6588+
ExternalTableDefinition externalTableDefinition =
6589+
ExternalTableDefinition.newBuilder(sourceUri)
6590+
.setConnectionId(
6591+
"projects/java-docs-samples-testing/locations/us/connections/DEVREL_TEST_CONNECTION")
6592+
.setObjectMetadata("SIMPLE")
6593+
.build();
6594+
TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition);
6595+
Table createdTable = bigquery.create(tableInfo);
6596+
assertNotNull(createdTable);
6597+
assertEquals(DATASET, createdTable.getTableId().getDataset());
6598+
assertEquals(tableName, createdTable.getTableId().getTable());
6599+
Table remoteTable = bigquery.getTable(DATASET, tableName);
6600+
assertNotNull(remoteTable);
6601+
6602+
try {
6603+
assertTrue(remoteTable.getDefinition() instanceof ExternalTableDefinition);
6604+
assertEquals(createdTable.getTableId(), remoteTable.getTableId());
6605+
assertEquals(
6606+
"SIMPLE", ((ExternalTableDefinition) remoteTable.getDefinition()).getObjectMetadata());
6607+
assertNotNull(remoteTable.getDefinition().getSchema().getFields().get("uri"));
6608+
6609+
String query = String.format("SELECT * FROM %s.%s", DATASET, tableName);
6610+
QueryJobConfiguration config = QueryJobConfiguration.newBuilder(query).build();
6611+
6612+
Job remoteJob = bigquery.create(JobInfo.of(config));
6613+
remoteJob = remoteJob.waitFor();
6614+
assertNull(remoteJob.getStatus().getError());
6615+
6616+
Job queryJob = bigquery.getJob(remoteJob.getJobId());
6617+
JobStatistics.QueryStatistics statistics = queryJob.getStatistics();
6618+
assertNotNull(statistics);
6619+
assertThat(statistics.getTotalBytesProcessed()).isGreaterThan(0);
6620+
} finally {
6621+
assertTrue(remoteTable.delete());
6622+
}
6623+
}
6624+
65826625
static GoogleCredentials loadCredentials(String credentialFile) {
65836626
try {
65846627
InputStream keyStream = new ByteArrayInputStream(credentialFile.getBytes());

0 commit comments

Comments
 (0)