Naireen commented on code in PR #16795:
URL: https://github.com/apache/beam/pull/16795#discussion_r955187954


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/StorageApiLoads.java:
##########
@@ -154,43 +154,61 @@ public WriteResult expandTriggered(
                     successfulRowsTag,
                     BigQueryStorageApiInsertErrorCoder.of(),
                     successCoder));
-    PCollection<KV<ShardedKey<DestinationT>, StorageApiWritePayload>> 
shardedRecords =
-        result
-            .get(successfulRowsTag)
-            .apply(
-                "AddShard",
-                ParDo.of(
-                    new DoFn<
-                        KV<DestinationT, StorageApiWritePayload>,
-                        KV<ShardedKey<DestinationT>, 
StorageApiWritePayload>>() {
-                      int shardNumber;
-
-                      @Setup
-                      public void setup() {
-                        shardNumber = 
ThreadLocalRandom.current().nextInt(numShards);
-                      }
-
-                      @ProcessElement
-                      public void processElement(
-                          @Element KV<DestinationT, StorageApiWritePayload> 
element,
-                          OutputReceiver<KV<ShardedKey<DestinationT>, 
StorageApiWritePayload>> o) {
-                        DestinationT destination = element.getKey();
-                        ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
-                        buffer.putInt(++shardNumber % numShards);
-                        o.output(
-                            KV.of(ShardedKey.of(destination, buffer.array()), 
element.getValue()));
-                      }
-                    }))
-            .setCoder(KvCoder.of(ShardedKey.Coder.of(destinationCoder), 
payloadCoder));
-
-    PCollection<KV<ShardedKey<DestinationT>, 
Iterable<StorageApiWritePayload>>> groupedRecords =
-        shardedRecords.apply(
-            "GroupIntoBatches",
-            GroupIntoBatches.<ShardedKey<DestinationT>, 
StorageApiWritePayload>ofByteSize(
-                    MAX_BATCH_SIZE_BYTES,
-                    (StorageApiWritePayload e) -> (long) e.getPayload().length)
-                .withMaxBufferingDuration(triggeringFrequency));
 
+    PCollection<KV<ShardedKey<DestinationT>, 
Iterable<StorageApiWritePayload>>> groupedRecords;
+
+    if (this.allowAutosharding) {
+      groupedRecords =
+          result
+              .get(successfulRowsTag)
+              .apply(
+                  "GroupIntoBatches",
+                  GroupIntoBatches.<DestinationT, 
StorageApiWritePayload>ofByteSize(
+                          MAX_BATCH_SIZE_BYTES,
+                          (StorageApiWritePayload e) -> (long) 
e.getPayload().length)
+                      .withMaxBufferingDuration(triggeringFrequency)
+                      .withShardedKey());
+
+    } else {
+      PCollection<KV<ShardedKey<DestinationT>, StorageApiWritePayload>> 
shardedRecords =

Review Comment:
   Refactored, PTAL.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to