You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by GitBox <gi...@apache.org> on 2022/07/12 19:46:27 UTC

[GitHub] [hudi] alexeykudinkin commented on a diff in pull request #5855: [HUDI-4249] Fixing in-memory `HoodieData` implementation to operate lazily

alexeykudinkin commented on code in PR #5855:
URL: https://github.com/apache/hudi/pull/5855#discussion_r919354390


##########
hudi-common/src/main/java/org/apache/hudi/common/data/HoodieListPairData.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.common.data;
+
+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.ValidationUtils;
+import org.apache.hudi.common.util.collection.Pair;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.apache.hudi.common.function.FunctionWrapper.throwingMapToPairWrapper;
+import static org.apache.hudi.common.function.FunctionWrapper.throwingMapWrapper;
+
+/**
+ * In-memory implementation of {@link HoodiePairData} holding internally a {@link Stream} of {@link Pair}s.
+ *
+ * NOTE: This is an in-memory counterpart for {@code HoodieJavaPairRDD}, and it strives to provide
+ *       similar semantic as RDD container -- all intermediate (non-terminal, not de-referencing
+ *       the stream like "collect", "groupBy", etc) operations are executed *lazily*.
+ *       This allows to make sure that compute/memory churn is minimal since only necessary
+ *       computations will ultimately be performed.
+ *
+ * @param <K> type of the key in the pair
+ * @param <V> type of the value in the pair
+ */
+public class HoodieListPairData<K, V> extends HoodiePairData<K, V> {
+
+  private final Stream<Pair<K, V>> dataStream;
+
+  public HoodieListPairData(List<Pair<K, V>> data) {
+    this.dataStream = data.stream().parallel();
+  }
+
+  HoodieListPairData(Stream<Pair<K, V>> dataStream) {
+    this.dataStream = dataStream;
+  }
+
+  @Override
+  public List<Pair<K, V>> get() {
+    return dataStream.collect(Collectors.toList());
+  }
+
+  @Override
+  public void persist(String cacheConfig) {
+    // no-op
+  }
+
+  @Override
+  public void unpersist() {
+    // no-op
+  }
+
+  @Override
+  public HoodieData<K> keys() {
+    return new HoodieListData<>(dataStream.map(Pair::getKey));
+  }
+
+  @Override
+  public HoodieData<V> values() {
+    return new HoodieListData<>(dataStream.map(Pair::getValue));
+  }
+
+  @Override
+  public long count() {
+    return dataStream.count();
+  }
+
+  @Override
+  public Map<K, Long> countByKey() {
+    return dataStream.collect(Collectors.groupingBy(Pair::getKey, Collectors.counting()));
+  }
+
+  @Override
+  public HoodiePairData<K, Iterable<V>> groupByKey() {
+    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 = dataStream.collect(groupingCollector);
+    return new HoodieListPairData<>(groupedByKey.entrySet().stream()
+        .map(e -> Pair.of(e.getKey(), e.getValue())));
+  }
+
+  @Override
+  public HoodiePairData<K, V> reduceByKey(SerializableBiFunction<V, V, V> combiner, int parallelism) {
+    Map<K, java.util.Optional<V>> reducedMap = dataStream.collect(
+        Collectors.groupingBy(
+            Pair::getKey,
+            HashMap::new,
+            Collectors.mapping(Pair::getValue, Collectors.reducing(combiner::apply))));

Review Comment:
   Explicit vars are necessary in some places since 1.8 javac can't infer types properly 



##########
hudi-common/src/main/java/org/apache/hudi/common/data/HoodieListPairData.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.common.data;
+
+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.ValidationUtils;
+import org.apache.hudi.common.util.collection.Pair;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.apache.hudi.common.function.FunctionWrapper.throwingMapToPairWrapper;
+import static org.apache.hudi.common.function.FunctionWrapper.throwingMapWrapper;
+
+/**
+ * In-memory implementation of {@link HoodiePairData} holding internally a {@link Stream} of {@link Pair}s.
+ *
+ * NOTE: This is an in-memory counterpart for {@code HoodieJavaPairRDD}, and it strives to provide
+ *       similar semantic as RDD container -- all intermediate (non-terminal, not de-referencing
+ *       the stream like "collect", "groupBy", etc) operations are executed *lazily*.
+ *       This allows to make sure that compute/memory churn is minimal since only necessary
+ *       computations will ultimately be performed.
+ *
+ * @param <K> type of the key in the pair
+ * @param <V> type of the value in the pair
+ */
+public class HoodieListPairData<K, V> extends HoodiePairData<K, V> {

Review Comment:
   It does have an UT



-- 
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: commits-unsubscribe@hudi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org