org.apache.hudi.data.HoodieJavaPairRDD Maven / Gradle / Ivy
The newest version!
/*
* 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.hudi.data;
import org.apache.hudi.common.data.HoodieData;
import org.apache.hudi.common.data.HoodiePairData;
import org.apache.hudi.common.function.SerializableBiFunction;
import org.apache.hudi.common.function.SerializableFunction;
import org.apache.hudi.common.function.SerializablePairFunction;
import org.apache.hudi.common.util.Option;
import org.apache.hudi.common.util.collection.ImmutablePair;
import org.apache.hudi.common.util.collection.Pair;
import org.apache.spark.Partitioner;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.Optional;
import org.apache.spark.sql.internal.SQLConf;
import org.apache.spark.storage.StorageLevel;
import java.util.List;
import java.util.Map;
import scala.Tuple2;
/**
* Implementation of {@link HoodiePairData} using Spark {@link JavaPairRDD}.
*
* @param type of key.
* @param type of value.
*/
public class HoodieJavaPairRDD implements HoodiePairData {
private final JavaPairRDD pairRDDData;
private HoodieJavaPairRDD(JavaPairRDD pairRDDData) {
this.pairRDDData = pairRDDData;
}
/**
* @param pairRDDData a {@link JavaPairRDD} of pairs.
* @param type of key.
* @param type of value.
* @return a new instance containing the {@link JavaPairRDD} reference.
*/
public static HoodieJavaPairRDD of(JavaPairRDD pairRDDData) {
return new HoodieJavaPairRDD<>(pairRDDData);
}
/**
* @param hoodiePairData {@link HoodieJavaPairRDD } instance containing the {@link JavaPairRDD} of pairs.
* @param type of key.
* @param type of value.
* @return the {@link JavaPairRDD} of pairs.
*/
public static JavaPairRDD getJavaPairRDD(HoodiePairData hoodiePairData) {
return ((HoodieJavaPairRDD) hoodiePairData).get();
}
@Override
public JavaPairRDD get() {
return pairRDDData;
}
@Override
public void persist(String storageLevel) {
pairRDDData.persist(StorageLevel.fromString(storageLevel));
}
@Override
public void unpersist() {
pairRDDData.unpersist();
}
@Override
public HoodieData keys() {
return HoodieJavaRDD.of(pairRDDData.keys());
}
@Override
public HoodieData values() {
return HoodieJavaRDD.of(pairRDDData.values());
}
@Override
public long count() {
return pairRDDData.count();
}
@Override
public Map countByKey() {
return pairRDDData.countByKey();
}
@Override
public HoodiePairData> groupByKey() {
return new HoodieJavaPairRDD<>(pairRDDData.groupByKey());
}
@Override
public HoodiePairData reduceByKey(SerializableBiFunction combiner, int parallelism) {
return HoodieJavaPairRDD.of(pairRDDData.reduceByKey(combiner::apply, parallelism));
}
@Override
public HoodieData map(SerializableFunction, O> func) {
return HoodieJavaRDD.of(pairRDDData.map(
tuple -> func.apply(new ImmutablePair<>(tuple._1, tuple._2))));
}
@Override
public HoodiePairData mapValues(SerializableFunction func) {
return HoodieJavaPairRDD.of(pairRDDData.mapValues(func::apply));
}
@Override
public HoodiePairData mapToPair(SerializablePairFunction, L, W> mapToPairFunc) {
return HoodieJavaPairRDD.of(pairRDDData.mapToPair(pair -> {
Pair newPair = mapToPairFunc.call(new ImmutablePair<>(pair._1, pair._2));
return new Tuple2<>(newPair.getLeft(), newPair.getRight());
}));
}
@Override
public HoodiePairData>> leftOuterJoin(HoodiePairData other) {
return HoodieJavaPairRDD.of(JavaPairRDD.fromJavaRDD(
pairRDDData.leftOuterJoin(HoodieJavaPairRDD.getJavaPairRDD(other))
.map(tuple -> new Tuple2<>(tuple._1,
new ImmutablePair<>(tuple._2._1, Option.ofNullable(tuple._2._2.orElse(null)))))));
}
@Override
public List> collectAsList() {
return pairRDDData.map(t -> Pair.of(t._1, t._2)).collect();
}
@Override
public int deduceNumPartitions() {
// for source rdd, the partitioner is None
final Optional partitioner = pairRDDData.partitioner();
if (partitioner.isPresent()) {
int partPartitions = partitioner.get().numPartitions();
if (partPartitions > 0) {
return partPartitions;
}
}
if (SQLConf.get().contains(SQLConf.SHUFFLE_PARTITIONS().key())) {
return Integer.parseInt(SQLConf.get().getConfString(SQLConf.SHUFFLE_PARTITIONS().key()));
} else if (pairRDDData.context().conf().contains("spark.default.parallelism")) {
return pairRDDData.context().defaultParallelism();
} else {
return pairRDDData.getNumPartitions();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy