tomstepp commented on code in PR #36112:
URL: https://github.com/apache/beam/pull/36112#discussion_r2379493408


##########
sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/KafkaReadRedistribute.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.io.kafka;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.Redistribute;
+import org.apache.beam.sdk.transforms.Values;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.hash.Hashing;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.primitives.UnsignedInteger;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+public class KafkaReadRedistribute<K, V>
+    extends PTransform<PCollection<KafkaRecord<K, V>>, 
PCollection<KafkaRecord<K, V>>> {
+  public static <K, V> KafkaReadRedistribute<K, V> byOffsetShard(@Nullable 
Integer numBuckets) {
+    return new KafkaReadRedistribute<>(numBuckets, false);
+  }
+
+  public static <K, V> KafkaReadRedistribute<K, V> byRecordKey(@Nullable 
Integer numBuckets) {
+    return new KafkaReadRedistribute<>(numBuckets, true);
+  }
+
+  // The number of buckets to shard into.
+  private @Nullable Integer numBuckets = null;
+  // When redistributing, group records by the Kafka record's key instead of 
by offset hash.
+  private boolean byRecordKey = false;
+
+  private KafkaReadRedistribute(@Nullable Integer numBuckets, boolean 
byRecordKey) {
+    this.numBuckets = numBuckets;
+    this.byRecordKey = byRecordKey;
+  }
+
+  @Override
+  public PCollection<KafkaRecord<K, V>> expand(PCollection<KafkaRecord<K, V>> 
input) {
+
+    if (byRecordKey) {
+      return input
+          .apply("Pair with record key", ParDo.of(new AssignRecordKeyFn<K, 
V>(numBuckets)))

Review Comment:
   Done.



-- 
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