Skip to content

Commit 7dbd0df

Browse files
author
Praful Makani
authored
feat: add labels for writechannelconfiguration (#347)
1 parent df0c3e6 commit 7dbd0df

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.common.primitives.Ints;
2929
import java.io.Serializable;
3030
import java.util.List;
31+
import java.util.Map;
3132
import java.util.Objects;
3233

3334
/**
@@ -53,6 +54,7 @@ public final class WriteChannelConfiguration implements LoadConfiguration, Seria
5354
private final TimePartitioning timePartitioning;
5455
private final Clustering clustering;
5556
private final Boolean useAvroLogicalTypes;
57+
private final Map<String, String> labels;
5658

5759
public static final class Builder implements LoadConfiguration.Builder {
5860

@@ -70,6 +72,7 @@ public static final class Builder implements LoadConfiguration.Builder {
7072
private TimePartitioning timePartitioning;
7173
private Clustering clustering;
7274
private Boolean useAvroLogicalTypes;
75+
private Map<String, String> labels;
7376

7477
private Builder() {}
7578

@@ -89,6 +92,7 @@ private Builder(WriteChannelConfiguration writeChannelConfiguration) {
8992
this.timePartitioning = writeChannelConfiguration.timePartitioning;
9093
this.clustering = writeChannelConfiguration.clustering;
9194
this.useAvroLogicalTypes = writeChannelConfiguration.useAvroLogicalTypes;
95+
this.labels = writeChannelConfiguration.labels;
9296
}
9397

9498
private Builder(com.google.api.services.bigquery.model.JobConfiguration configurationPb) {
@@ -162,6 +166,9 @@ private Builder(com.google.api.services.bigquery.model.JobConfiguration configur
162166
this.clustering = Clustering.fromPb(loadConfigurationPb.getClustering());
163167
}
164168
this.useAvroLogicalTypes = loadConfigurationPb.getUseAvroLogicalTypes();
169+
if (configurationPb.getLabels() != null) {
170+
this.labels = configurationPb.getLabels();
171+
}
165172
}
166173

167174
@Override
@@ -250,6 +257,11 @@ public Builder setUseAvroLogicalTypes(Boolean useAvroLogicalTypes) {
250257
return this;
251258
}
252259

260+
public Builder setLabels(Map<String, String> labels) {
261+
this.labels = labels;
262+
return this;
263+
}
264+
253265
@Override
254266
public WriteChannelConfiguration build() {
255267
return new WriteChannelConfiguration(this);
@@ -271,6 +283,7 @@ protected WriteChannelConfiguration(Builder builder) {
271283
this.timePartitioning = builder.timePartitioning;
272284
this.clustering = builder.clustering;
273285
this.useAvroLogicalTypes = builder.useAvroLogicalTypes;
286+
this.labels = builder.labels;
274287
}
275288

276289
@Override
@@ -355,6 +368,10 @@ public Boolean getUseAvroLogicalTypes() {
355368
return useAvroLogicalTypes;
356369
}
357370

371+
public Map<String, String> getLabels() {
372+
return labels;
373+
}
374+
358375
@Override
359376
public Builder toBuilder() {
360377
return new Builder(this);
@@ -375,7 +392,8 @@ MoreObjects.ToStringHelper toStringHelper() {
375392
.add("autodetect", autodetect)
376393
.add("timePartitioning", timePartitioning)
377394
.add("clustering", clustering)
378-
.add("useAvroLogicalTypes", useAvroLogicalTypes);
395+
.add("useAvroLogicalTypes", useAvroLogicalTypes)
396+
.add("labels", labels);
379397
}
380398

381399
@Override
@@ -405,7 +423,8 @@ public int hashCode() {
405423
autodetect,
406424
timePartitioning,
407425
clustering,
408-
useAvroLogicalTypes);
426+
useAvroLogicalTypes,
427+
labels);
409428
}
410429

411430
WriteChannelConfiguration setProjectId(String projectId) {
@@ -416,6 +435,8 @@ WriteChannelConfiguration setProjectId(String projectId) {
416435
}
417436

418437
com.google.api.services.bigquery.model.JobConfiguration toPb() {
438+
com.google.api.services.bigquery.model.JobConfiguration jobConfiguration =
439+
new com.google.api.services.bigquery.model.JobConfiguration();
419440
JobConfigurationLoad loadConfigurationPb = new JobConfigurationLoad();
420441
loadConfigurationPb.setDestinationTable(destinationTable.toPb());
421442
if (createDisposition != null) {
@@ -471,8 +492,11 @@ com.google.api.services.bigquery.model.JobConfiguration toPb() {
471492
loadConfigurationPb.setClustering(clustering.toPb());
472493
}
473494
loadConfigurationPb.setUseAvroLogicalTypes(useAvroLogicalTypes);
474-
return new com.google.api.services.bigquery.model.JobConfiguration()
475-
.setLoad(loadConfigurationPb);
495+
if (labels != null) {
496+
jobConfiguration.setLabels(labels);
497+
}
498+
jobConfiguration.setLoad(loadConfigurationPb);
499+
return jobConfiguration;
476500
}
477501

478502
static WriteChannelConfiguration fromPb(

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import com.google.cloud.bigquery.JobInfo.WriteDisposition;
2424
import com.google.cloud.bigquery.TimePartitioning.Type;
2525
import com.google.common.collect.ImmutableList;
26+
import com.google.common.collect.ImmutableMap;
2627
import java.nio.charset.StandardCharsets;
2728
import java.util.List;
29+
import java.util.Map;
2830
import org.junit.Test;
2931

3032
public class WriteChannelConfigurationTest {
@@ -55,6 +57,8 @@ public class WriteChannelConfigurationTest {
5557
private static final TimePartitioning TIME_PARTITIONING = TimePartitioning.of(Type.DAY);
5658
private static final Clustering CLUSTERING =
5759
Clustering.newBuilder().setFields(ImmutableList.of("Foo", "Bar")).build();
60+
private static final Map<String, String> LABELS =
61+
ImmutableMap.of("test-job-name", "test-write-channel");
5862
private static final WriteChannelConfiguration LOAD_CONFIGURATION_CSV =
5963
WriteChannelConfiguration.newBuilder(TABLE_ID)
6064
.setCreateDisposition(CREATE_DISPOSITION)
@@ -68,6 +72,7 @@ public class WriteChannelConfigurationTest {
6872
.setAutodetect(AUTODETECT)
6973
.setTimePartitioning(TIME_PARTITIONING)
7074
.setClustering(CLUSTERING)
75+
.setLabels(LABELS)
7176
.build();
7277

7378
private static final DatastoreBackupOptions BACKUP_OPTIONS =
@@ -151,6 +156,7 @@ public void testBuilder() {
151156
assertEquals(IGNORE_UNKNOWN_VALUES, LOAD_CONFIGURATION_CSV.ignoreUnknownValues());
152157
assertEquals(MAX_BAD_RECORDS, LOAD_CONFIGURATION_CSV.getMaxBadRecords());
153158
assertEquals(TABLE_SCHEMA, LOAD_CONFIGURATION_CSV.getSchema());
159+
assertEquals(LABELS, LOAD_CONFIGURATION_CSV.getLabels());
154160
assertEquals(BACKUP_OPTIONS, LOAD_CONFIGURATION_BACKUP.getDatastoreBackupOptions());
155161
assertEquals(SCHEMA_UPDATE_OPTIONS, LOAD_CONFIGURATION_CSV.getSchemaUpdateOptions());
156162
assertEquals(SCHEMA_UPDATE_OPTIONS, LOAD_CONFIGURATION_BACKUP.getSchemaUpdateOptions());
@@ -218,5 +224,6 @@ private void compareLoadConfiguration(
218224
assertEquals(expected.getTimePartitioning(), value.getTimePartitioning());
219225
assertEquals(expected.getClustering(), value.getClustering());
220226
assertEquals(expected.getUseAvroLogicalTypes(), value.getUseAvroLogicalTypes());
227+
assertEquals(expected.getLabels(), value.getLabels());
221228
}
222229
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,6 +2105,40 @@ public void testInsertFromFile() throws InterruptedException, IOException, Timeo
21052105
assertTrue(bigquery.delete(tableId));
21062106
}
21072107

2108+
@Test
2109+
public void testInsertFromFileWithLabels()
2110+
throws InterruptedException, IOException, TimeoutException {
2111+
String destinationTableName = "test_insert_from_file_table_with_labels";
2112+
TableId tableId = TableId.of(DATASET, destinationTableName);
2113+
WriteChannelConfiguration configuration =
2114+
WriteChannelConfiguration.newBuilder(tableId)
2115+
.setFormatOptions(FormatOptions.json())
2116+
.setCreateDisposition(JobInfo.CreateDisposition.CREATE_IF_NEEDED)
2117+
.setSchema(TABLE_SCHEMA)
2118+
.setLabels(LABELS)
2119+
.build();
2120+
TableDataWriteChannel channel = bigquery.writer(configuration);
2121+
try {
2122+
// A zero byte write should not throw an exception.
2123+
assertEquals(0, channel.write(ByteBuffer.wrap("".getBytes(StandardCharsets.UTF_8))));
2124+
} finally {
2125+
// Force the channel to flush by calling `close`.
2126+
channel.close();
2127+
}
2128+
channel = bigquery.writer(configuration);
2129+
try {
2130+
channel.write(ByteBuffer.wrap(JSON_CONTENT.getBytes(StandardCharsets.UTF_8)));
2131+
} finally {
2132+
channel.close();
2133+
}
2134+
Job job = channel.getJob().waitFor();
2135+
LoadJobConfiguration jobConfiguration = job.getConfiguration();
2136+
assertEquals(TABLE_SCHEMA, jobConfiguration.getSchema());
2137+
assertEquals(LABELS, jobConfiguration.getLabels());
2138+
assertNull(job.getStatus().getError());
2139+
assertTrue(bigquery.delete(tableId));
2140+
}
2141+
21082142
@Test
21092143
public void testLocation() throws Exception {
21102144
String location = "EU";

0 commit comments

Comments
 (0)