Skip to content
Open
Show file tree
Hide file tree
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 @@ -22,17 +22,21 @@
import org.apache.hudi.common.config.SerializableSchema;
import org.apache.hudi.common.config.TypedProperties;
import org.apache.hudi.common.data.HoodieData;
import org.apache.hudi.common.data.HoodiePairData;
import org.apache.hudi.common.engine.HoodieEngineContext;
import org.apache.hudi.common.model.HoodieKey;
import org.apache.hudi.common.model.HoodieOperation;
import org.apache.hudi.common.model.HoodieRecord;
import org.apache.hudi.common.model.HoodieRecordMerger;
import org.apache.hudi.common.model.OverwriteWithLatestPartialUpdatesAvroPayload;
import org.apache.hudi.common.util.collection.Pair;
import org.apache.hudi.config.HoodieWriteConfig;
import org.apache.hudi.exception.HoodieException;
import org.apache.hudi.index.HoodieIndex;
import org.apache.hudi.table.HoodieTable;

import java.io.IOException;
import java.util.ArrayList;

public class HoodieWriteHelper<T, R> extends BaseWriteHelper<T, HoodieData<HoodieRecord<T>>,
HoodieData<HoodieKey>, HoodieData<WriteStatus>, R> {
Expand Down Expand Up @@ -60,25 +64,49 @@ public HoodieData<HoodieRecord<T>> deduplicateRecords(
HoodieData<HoodieRecord<T>> records, HoodieIndex<?, ?> index, int parallelism, String schemaStr, TypedProperties props, HoodieRecordMerger merger) {
boolean isIndexingGlobal = index.isGlobal();
final SerializableSchema schema = new SerializableSchema(schemaStr);
return records.mapToPair(record -> {
HoodiePairData<Object, HoodieRecord<T>> recordPairs = records.mapToPair(record -> {
HoodieKey hoodieKey = record.getKey();
// If index used is global, then records are expected to differ in their partitionPath
Object key = isIndexingGlobal ? hoodieKey.getRecordKey() : hoodieKey;
// NOTE: PLEASE READ CAREFULLY BEFORE CHANGING
// Here we have to make a copy of the incoming record, since it might be holding
// an instance of [[InternalRow]] pointing into shared, mutable buffer
return Pair.of(key, record.copy());
}).reduceByKey((rec1, rec2) -> {
HoodieRecord<T> reducedRecord;
try {
reducedRecord = merger.merge(rec1, schema.get(), rec2, schema.get(), props).get().getLeft();
} catch (IOException e) {
throw new HoodieException(String.format("Error to merge two records, %s, %s", rec1, rec2), e);
}
boolean choosePrev = rec1.getData().equals(reducedRecord.getData());
HoodieKey reducedKey = choosePrev ? rec1.getKey() : rec2.getKey();
HoodieOperation operation = choosePrev ? rec1.getOperation() : rec2.getOperation();
return reducedRecord.newInstance(reducedKey, operation);
}, parallelism).map(Pair::getRight);
});

if (props.containsKey(HoodieWriteConfig.WRITE_PAYLOAD_CLASS_NAME.key())
&& OverwriteWithLatestPartialUpdatesAvroPayload.class.getCanonicalName().equals(props.getString(HoodieWriteConfig.WRITE_PAYLOAD_CLASS_NAME.key()))) {
return recordPairs.groupByKeyAndReduce(pr -> {
ArrayList<HoodieRecord<T>> sortedRecords = new ArrayList<>();
pr.forEach(sortedRecords::add);
sortedRecords.sort(new SortedPrecombineComparator<>(schema.get(), props));
return sortedRecords.stream().reduce((rec1, rec2) -> {
HoodieRecord<T> reducedRecord;
try {
reducedRecord = merger.merge(rec1, schema.get(), rec2, schema.get(), props).get().getLeft();
} catch (IOException e) {
throw new HoodieException(String.format("Error to merge two records, %s, %s", rec1, rec2), e);
}
boolean choosePrev = rec1.getData().equals(reducedRecord.getData());
HoodieKey reducedKey = choosePrev ? rec1.getKey() : rec2.getKey();
HoodieOperation operation = choosePrev ? rec1.getOperation() : rec2.getOperation();
return reducedRecord.newInstance(reducedKey, operation);

}).get();
}).map(Pair::getRight);
} else {
return recordPairs.reduceByKey((rec1, rec2) -> {
HoodieRecord<T> reducedRecord;
try {
reducedRecord = merger.merge(rec1, schema.get(), rec2, schema.get(), props).get().getLeft();
} catch (IOException e) {
throw new HoodieException(String.format("Error to merge two records, %s, %s", rec1, rec2), e);
}
boolean choosePrev = rec1.getData().equals(reducedRecord.getData());
HoodieKey reducedKey = choosePrev ? rec1.getKey() : rec2.getKey();
HoodieOperation operation = choosePrev ? rec1.getOperation() : rec2.getOperation();
return reducedRecord.newInstance(reducedKey, operation);
}, parallelism).map(Pair::getRight);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.table.action.commit;

import org.apache.avro.Schema;
import org.apache.hudi.common.config.TypedProperties;
import org.apache.hudi.common.model.HoodieRecord;

import java.io.Serializable;
import java.util.Comparator;

class SortedPrecombineComparator<T> implements Comparator<HoodieRecord<T>>, Serializable {

Schema schema;
TypedProperties props;

public SortedPrecombineComparator(Schema schema, TypedProperties props) {
this.schema = schema;
this.props = props;
}

@Override
public int compare(HoodieRecord<T> o1, HoodieRecord<T> o2) {
return ((Comparable) o1.getOrderingValue(schema, props)).compareTo(o2.getOrderingValue(schema, props));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import org.apache.hudi.common.model.HoodieOperation;
import org.apache.hudi.common.model.HoodieRecord;
import org.apache.hudi.common.model.HoodieRecordMerger;
import org.apache.hudi.common.model.OverwriteWithLatestPartialUpdatesAvroPayload;
import org.apache.hudi.common.model.WriteOperationType;
import org.apache.hudi.config.HoodieWriteConfig;
import org.apache.hudi.exception.HoodieException;
import org.apache.hudi.exception.HoodieUpsertException;
import org.apache.hudi.index.HoodieIndex;
Expand All @@ -41,6 +43,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Overrides the {@link #write} method to not look up index and partition the records, because
Expand Down Expand Up @@ -99,24 +102,35 @@ public List<HoodieRecord<T>> deduplicateRecords(

// caution that the avro schema is not serializable
final Schema schema = new Schema.Parser().parse(schemaStr);
return keyedRecords.values().stream().map(x -> x.stream().reduce((rec1, rec2) -> {
HoodieRecord<T> reducedRecord;
try {
// Precombine do not need schema and do not return null
reducedRecord = merger.merge(rec1, schema, rec2, schema, props).get().getLeft();
} catch (IOException e) {
throw new HoodieException(String.format("Error to merge two records, %s, %s", rec1, rec2), e);
boolean sortBeforePrecombine = props.containsKey(HoodieWriteConfig.WRITE_PAYLOAD_CLASS_NAME.key())
&& OverwriteWithLatestPartialUpdatesAvroPayload.class.getCanonicalName().equals(props.getString(HoodieWriteConfig.WRITE_PAYLOAD_CLASS_NAME.key()));
return keyedRecords.values().stream().map(hoodieRecords -> {
Stream<HoodieRecord<T>> recordStream;
if (sortBeforePrecombine) {
recordStream = hoodieRecords.stream().sorted(new SortedPrecombineComparator(schema, props));
} else {
recordStream = hoodieRecords.stream();
}
// we cannot allow the user to change the key or partitionPath, since that will affect
// everything
// so pick it from one of the records.
boolean choosePrev = rec1.getData() == reducedRecord.getData();
HoodieKey reducedKey = choosePrev ? rec1.getKey() : rec2.getKey();
HoodieOperation operation = choosePrev ? rec1.getOperation() : rec2.getOperation();
HoodieRecord<T> hoodieRecord = reducedRecord.newInstance(reducedKey, operation);
// reuse the location from the first record.
hoodieRecord.setCurrentLocation(rec1.getCurrentLocation());
return hoodieRecord;
}).orElse(null)).filter(Objects::nonNull).collect(Collectors.toList());

return recordStream.reduce((rec1, rec2) -> {
HoodieRecord<T> reducedRecord;
try {
// Precombine do not need schema and do not return null
reducedRecord = merger.merge(rec1, schema, rec2, schema, props).get().getLeft();
} catch (IOException e) {
throw new HoodieException(String.format("Error to merge two records, %s, %s", rec1, rec2), e);
}
// we cannot allow the user to change the key or partitionPath, since that will affect
// everything
// so pick it from one of the records.
boolean choosePrev = rec1.getData() == reducedRecord.getData();
HoodieKey reducedKey = choosePrev ? rec1.getKey() : rec2.getKey();
HoodieOperation operation = choosePrev ? rec1.getOperation() : rec2.getOperation();
HoodieRecord<T> hoodieRecord = reducedRecord.newInstance(reducedKey, operation);
// reuse the location from the first record.
hoodieRecord.setCurrentLocation(rec1.getCurrentLocation());
return hoodieRecord;
}).orElse(null);
}).filter(Objects::nonNull).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import org.apache.hudi.common.model.HoodieKey;
import org.apache.hudi.common.model.HoodieRecord;
import org.apache.hudi.common.model.HoodieRecordMerger;
import org.apache.hudi.common.model.OverwriteWithLatestPartialUpdatesAvroPayload;
import org.apache.hudi.common.util.collection.Pair;
import org.apache.hudi.config.HoodieWriteConfig;
import org.apache.hudi.exception.HoodieException;
import org.apache.hudi.index.HoodieIndex;
import org.apache.hudi.table.HoodieTable;
Expand All @@ -36,6 +38,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class JavaWriteHelper<T,R> extends BaseWriteHelper<T, List<HoodieRecord<T>>,
List<HoodieKey>, List<WriteStatus>, R> {
Expand Down Expand Up @@ -69,17 +72,27 @@ public List<HoodieRecord<T>> deduplicateRecords(
}).collect(Collectors.groupingBy(Pair::getLeft));

final Schema schema = new Schema.Parser().parse(schemaStr);
return keyedRecords.values().stream().map(x -> x.stream().map(Pair::getRight).reduce((rec1, rec2) -> {
HoodieRecord<T> reducedRecord;
try {
reducedRecord = merger.merge(rec1, schema, rec2, schema, props).get().getLeft();
} catch (IOException e) {
throw new HoodieException(String.format("Error to merge two records, %s, %s", rec1, rec2), e);
boolean sortBeforePrecombine = props.containsKey(HoodieWriteConfig.WRITE_PAYLOAD_CLASS_NAME.key())
&& OverwriteWithLatestPartialUpdatesAvroPayload.class.getCanonicalName().equals(props.getString(HoodieWriteConfig.WRITE_PAYLOAD_CLASS_NAME.key()));
return keyedRecords.values().stream().map(hoodieRecords -> {
Stream<HoodieRecord<T>> recordStream;
if (sortBeforePrecombine) {
recordStream = hoodieRecords.stream().map(Pair::getRight).sorted(new SortedPrecombineComparator(schema, props));
} else {
recordStream = hoodieRecords.stream().map(Pair::getRight);
}
// we cannot allow the user to change the key or partitionPath, since that will affect
// everything
// so pick it from one of the records.
return reducedRecord.newInstance(rec1.getKey(), rec1.getOperation());
}).orElse(null)).filter(Objects::nonNull).collect(Collectors.toList());
return recordStream.reduce((rec1, rec2) -> {
HoodieRecord<T> reducedRecord;
try {
reducedRecord = merger.merge(rec1, schema, rec2, schema, props).get().getLeft();
} catch (IOException e) {
throw new HoodieException(String.format("Error to merge two records, %s, %s", rec1, rec2), e);
}
// we cannot allow the user to change the key or partitionPath, since that will affect
// everything
// so pick it from one of the records.
return reducedRecord.newInstance(rec1.getKey(), rec1.getOperation());
}).orElse(null);
}).filter(Objects::nonNull).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public HoodiePairData<K, Iterable<V>> groupByKey() {
return new HoodieJavaPairRDD<>(pairRDDData.groupByKey());
}

@Override
public HoodiePairData<K, V> groupByKeyAndReduce(SerializableFunction<Iterable<V>,V> function) {
return new HoodieJavaPairRDD<>(pairRDDData.groupByKey().mapValues(function::apply));
}

@Override
public HoodiePairData<K, V> reduceByKey(SerializableBiFunction<V, V, V> combiner, int parallelism) {
return HoodieJavaPairRDD.of(pairRDDData.reduceByKey(combiner::apply, parallelism));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ public HoodiePairData<K, Iterable<V>> groupByKey() {
);
}

@Override
public HoodiePairData<K, V> groupByKeyAndReduce(SerializableFunction<Iterable<V>,V> function) {
Collector<Pair<K, V>, ?, List<V>> mappingCollector = Collectors.mapping(Pair::getValue, Collectors.toList());
Collector<Pair<K, V>, ?, Map<K, List<V>>> groupingCollector =
Collectors.groupingBy(Pair::getKey, mappingCollector);

Map<K, List<V>> groupedByKey = asStream().collect(groupingCollector);
return new HoodieListPairData<>(
groupedByKey.entrySet().stream().map(e -> {
V data;
try {
data = function.apply(e.getValue());
} catch (Exception exception) {
data = null;
}
return Pair.of(e.getKey(), data);
}),
lazy
);
}

@Override
public HoodiePairData<K, V> reduceByKey(SerializableBiFunction<V, V, V> combiner, int parallelism) {
Map<K, java.util.Optional<V>> reducedMap = asStream().collect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public interface HoodiePairData<K, V> extends Serializable {
*/
HoodiePairData<K, Iterable<V>> groupByKey();

/**
* Groups the values for each key in the dataset into a single sequence and applies a reduce function
*/
HoodiePairData<K, V> groupByKeyAndReduce(SerializableFunction<Iterable<V>,V> function);

/**
* Reduces original sequence by de-duplicating the pairs w/ the same key, using provided
* binary operator {@code combiner}. Returns an instance of {@link HoodiePairData} holding
Expand Down
Loading
Loading