Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.bigquery.storage.v1.AppendRowsRequest;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Predicate;
Expand All @@ -38,6 +39,7 @@
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.transforms.Redistribute;
import org.apache.beam.sdk.transforms.SerializableFunction;
import org.apache.beam.sdk.transforms.Values;
import org.apache.beam.sdk.transforms.errorhandling.BadRecord;
import org.apache.beam.sdk.transforms.errorhandling.BadRecordRouter;
import org.apache.beam.sdk.transforms.errorhandling.BadRecordRouter.ThrowingBadRecordRouter;
Expand All @@ -50,6 +52,7 @@
import org.apache.beam.sdk.values.PCollectionList;
import org.apache.beam.sdk.values.PCollectionTuple;
import org.apache.beam.sdk.values.TupleTag;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.hash.Hashing;
import org.joda.time.Duration;

/** This {@link PTransform} manages loads into BigQuery using the Storage API. */
Expand Down Expand Up @@ -379,12 +382,21 @@ public WriteResult expandUntriggered(
PCollection<KV<DestinationT, StorageApiWritePayload>> successfulConvertedRows =
convertMessagesResult.get(successfulConvertedRowsTag);

if (numShards > 0) {
if (numShards > 0 && input.isBounded() == PCollection.IsBounded.UNBOUNDED) {
successfulConvertedRows =
successfulConvertedRows.apply(
"ResdistibuteNumShards",
Redistribute.<KV<DestinationT, StorageApiWritePayload>>arbitrarily()
.withNumBuckets(numShards));
} else if (numShards > 0 && input.isBounded() == PCollection.IsBounded.BOUNDED) {
successfulConvertedRows =
successfulConvertedRows
.apply(
"AddKeyWithSideInputs",
ParDo.of(new AddShardKeyFn<>(dynamicDestinations, numShards))
.withSideInputs(dynamicDestinations.getSideInputs()))
.apply("RedistributeNumShards", Redistribute.byKey())
.apply("Remove shard", Values.create());
}

PCollectionTuple writeRecordsResult =
Expand Down Expand Up @@ -457,6 +469,37 @@ private void addErrorCollections(
}
}

private static class AddShardKeyFn<DestT, ElemT>
extends DoFn<
KV<DestT, StorageApiWritePayload>, KV<Integer, KV<DestT, StorageApiWritePayload>>> {

private final StorageApiDynamicDestinations<ElemT, DestT> dynamicDestinations;
private final int numShards;

public AddShardKeyFn(
StorageApiDynamicDestinations<ElemT, DestT> dynamicDestinations, int numShards) {
this.dynamicDestinations = dynamicDestinations;
this.numShards = numShards;
}

@ProcessElement
public void processElement(
ProcessContext c,
@Element KV<DestT, StorageApiWritePayload> element,
OutputReceiver<KV<Integer, KV<DestT, StorageApiWritePayload>>> outputReceiver) {
dynamicDestinations.setSideInputAccessorFromProcessContext(c);

String tableUrn = dynamicDestinations.getTable(element.getKey()).getShortTableUrn();

int hash = Hashing.murmur3_32_fixed().hashString(tableUrn, StandardCharsets.UTF_8).asInt();

int shardKey =
Math.floorMod(hash + ThreadLocalRandom.current().nextInt(numShards), numShards);

outputReceiver.output(KV.of(shardKey, element));
}
}
Comment thread
stankiewicz marked this conversation as resolved.

private static class ConvertInsertErrorToBadRecord
extends DoFn<BigQueryStorageApiInsertError, BadRecord> {

Expand Down
Loading