Skip to content

Commit

Permalink
Merge pull request #23638: [Spark dataset runner] Add direct translat…
Browse files Browse the repository at this point in the history
…ion of Reshuffle and Reshuffle.ViaRandomKey transforms.
  • Loading branch information
aromanenko-dev authored Oct 14, 2022
2 parents b784c98 + 92587e8 commit 45cc085
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.beam.sdk.transforms.Impulse;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.transforms.Reshuffle;
import org.apache.beam.sdk.transforms.View;
import org.apache.beam.sdk.transforms.windowing.Window;
import org.apache.beam.sdk.values.PInput;
Expand Down Expand Up @@ -66,8 +67,9 @@ public class PipelineTranslatorBatch extends PipelineTranslator {
TRANSFORM_TRANSLATORS.put(Combine.Globally.class, new CombineGloballyTranslatorBatch<>());
TRANSFORM_TRANSLATORS.put(GroupByKey.class, new GroupByKeyTranslatorBatch<>());

// TODO: Do we need to have a dedicated translator for {@code Reshuffle} if it's deprecated?
// TRANSFORM_TRANSLATORS.put(Reshuffle.class, new ReshuffleTranslatorBatch());
TRANSFORM_TRANSLATORS.put(Reshuffle.class, new ReshuffleTranslatorBatch<>());
TRANSFORM_TRANSLATORS.put(
Reshuffle.ViaRandomKey.class, new ReshuffleTranslatorBatch.ViaRandomKey<>());

TRANSFORM_TRANSLATORS.put(Flatten.PCollections.class, new FlattenTranslatorBatch<>());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.runners.spark.structuredstreaming.translation.batch;

import static org.apache.spark.sql.functions.col;

import java.io.IOException;
import org.apache.beam.runners.spark.structuredstreaming.translation.TransformTranslator;
import org.apache.beam.sdk.transforms.Reshuffle;
import org.apache.beam.sdk.util.WindowedValue;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.PCollection;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.internal.SQLConf;

public class ReshuffleTranslatorBatch<K, V>
extends TransformTranslator<PCollection<KV<K, V>>, PCollection<KV<K, V>>, Reshuffle<K, V>> {

@Override
protected void translate(Reshuffle<K, V> transform, Context cxt) throws IOException {
Dataset<WindowedValue<KV<K, V>>> input = cxt.getDataset(cxt.getInput());
cxt.putDataset(cxt.getOutput(), input.repartition(col("value.key")));
}

public static class ViaRandomKey<V>
extends TransformTranslator<PCollection<V>, PCollection<V>, Reshuffle.ViaRandomKey<V>> {

@Override
protected void translate(Reshuffle.ViaRandomKey<V> transform, Context cxt) throws IOException {
Dataset<WindowedValue<V>> input = cxt.getDataset(cxt.getInput());
// Reshuffle randomly
cxt.putDataset(cxt.getOutput(), input.repartition(SQLConf.get().numShufflePartitions()));
}
}
}

0 comments on commit 45cc085

Please sign in to comment.