nsivabalan commented on a change in pull request #1402: [WIP][HUDI-407] Adding 
Simple Index
URL: https://github.com/apache/incubator-hudi/pull/1402#discussion_r399408154
 
 

 ##########
 File path: 
hudi-client/src/main/java/org/apache/hudi/index/bloom/HoodieSimpleIndex.java
 ##########
 @@ -0,0 +1,211 @@
+/*
+ * 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.index.bloom;
+
+import org.apache.hudi.WriteStatus;
+import org.apache.hudi.common.model.HoodieDataFile;
+import org.apache.hudi.common.model.HoodieKey;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.model.HoodieRecordLocation;
+import org.apache.hudi.common.model.HoodieRecordPayload;
+import org.apache.hudi.common.util.ParquetUtils;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.config.HoodieWriteConfig;
+import org.apache.hudi.func.LazyIterableIterator;
+import org.apache.hudi.table.HoodieTable;
+
+import com.clearspring.analytics.util.Lists;
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.hadoop.fs.Path;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.spark.api.java.JavaPairRDD;
+import org.apache.spark.api.java.JavaRDD;
+import org.apache.spark.api.java.JavaSparkContext;
+import org.apache.spark.api.java.function.FlatMapFunction;
+import org.apache.spark.api.java.function.PairFunction;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import scala.Tuple2;
+
+import static java.util.stream.Collectors.toList;
+
+/**
+ * A simple index which reads interested fields(record key and partition path) 
from parquet and joins with incoming records to find the
+ * tagged location
+ *
+ * @param <T>
+ */
+public class HoodieSimpleIndex<T extends HoodieRecordPayload> extends 
HoodieBloomIndex<T> {
+
+  private static final Logger LOG = 
LogManager.getLogger(HoodieSimpleIndex.class);
+
+  public HoodieSimpleIndex(HoodieWriteConfig config) {
+    super(config);
+  }
+
+  @Override
+  public JavaRDD<HoodieRecord<T>> tagLocation(JavaRDD<HoodieRecord<T>> 
recordRDD, JavaSparkContext jsc,
+                                              HoodieTable<T> hoodieTable) {
+
+    // Step 0: cache the input record RDD
+    if (config.getSimpleIndexUseCaching()) {
+      recordRDD.persist(config.getSimpleIndexInputStorageLevel());
+    }
+
+    // Step 1: Extract out thinner JavaPairRDD of (partitionPath, recordKey)
+    JavaPairRDD<String, String> partitionRecordKeyPairRDD =
+        recordRDD.mapToPair(record -> new Tuple2<>(record.getPartitionPath(), 
record.getRecordKey()));
+
+    // Step 2: Load all involved files as <Partition, filename> pairs
+    List<String> affectedPartitionPathList = 
partitionRecordKeyPairRDD.map(tuple -> tuple._1).distinct().collect();
+    JavaRDD<Tuple2<String, String>> fileInfoList = jsc.parallelize(
+        loadAllFilesForPartitions(affectedPartitionPathList, jsc, 
hoodieTable)).sortBy(Tuple2::_1, true, config.getSimpleIndexParallelism());
+
+    // Step 3: Lookup indexes for all the partition/recordkey pair
+    JavaPairRDD<HoodieKey, HoodieRecordLocation> keyFilenamePairRDD = 
findMatchingFilesForRecordKeys(fileInfoList, partitionRecordKeyPairRDD, 
hoodieTable);
+
+    // Step 4: Tag the incoming records, as inserts or updates, by joining 
with existing record keys
+    JavaRDD<HoodieRecord<T>> taggedRecordRDD = 
tagLocationBacktoRecords(keyFilenamePairRDD, recordRDD);
+
+    if (config.getSimpleIndexUseCaching()) {
+      recordRDD.unpersist(); // unpersist the input Record RDD
+    }
+    return taggedRecordRDD;
+  }
+
+  @Override
+  public JavaRDD<WriteStatus> updateLocation(JavaRDD<WriteStatus> 
writeStatusRDD, JavaSparkContext jsc,
+                                             HoodieTable<T> hoodieTable) {
+    return writeStatusRDD;
+  }
+
+  /**
+   * Lookup the location for each record key and return the 
pair<record_key,location> for all record keys already
+   * present and drop the record keys if not present.
+   */
+  @Override
+  protected JavaPairRDD<HoodieKey, HoodieRecordLocation> lookupIndex(
+      JavaPairRDD<String, String> partitionRecordKeyPairRDD, final 
JavaSparkContext jsc,
+      final HoodieTable hoodieTable) {
+    // Obtain records per partition, in the incoming records
+    Map<String, Long> recordsPerPartition = 
partitionRecordKeyPairRDD.countByKey();
+    List<String> affectedPartitionPathList = new 
ArrayList<>(recordsPerPartition.keySet());
+
+    // Step 2: Load all involved files as <Partition, filename> pairs
+    JavaRDD<Tuple2<String, String>> fileInfoList =
+        jsc.parallelize(loadAllFilesForPartitions(affectedPartitionPathList, 
jsc, hoodieTable));
+
+    return findMatchingFilesForRecordKeys(fileInfoList, 
partitionRecordKeyPairRDD, hoodieTable);
+  }
+
+  /**
+   * Load all involved files as <Partition, filename> pair RDD.
+   */
+  @VisibleForTesting
+  List<Tuple2<String, String>> loadAllFilesForPartitions(List<String> 
partitions, final JavaSparkContext jsc,
+                                                         final HoodieTable 
hoodieTable) {
+
+    // Obtain the latest data files from all the partitions.
+    List<Pair<String, String>> partitionPathFileIDList = 
loadLatestDataFilesForAllPartitions(partitions,
+        jsc, hoodieTable);
+    return partitionPathFileIDList.stream()
+        .map(pf -> new Tuple2<>(pf.getKey(), pf.getValue())).collect(toList());
+  }
+
+  /**
+   * Find <HoodieKey, HoodieRecordLocation> for all incoming HoodieKeys
+   */
+  private JavaPairRDD<HoodieKey, HoodieRecordLocation> 
findMatchingFilesForRecordKeys(JavaRDD<Tuple2<String, String>> 
partitionToFileIndexInfo,
+                                                                               
       JavaPairRDD<String, String> partitionRecordKeyPairRDD, HoodieTable 
hoodieTable) {
+    // Step 1: for each <partition, fileId> pair, co locate records to be 
searched for
+    JavaPairRDD<String, String> partitionFileIndexInfoPairRDD = 
partitionToFileIndexInfo.mapToPair(entry -> new Tuple2<>(entry._1, entry._2));
+    JavaPairRDD<String, List<String>> partitionToRecords = 
partitionRecordKeyPairRDD.groupByKey().mapToPair(entry -> new Tuple2(entry._1, 
Lists.newArrayList(entry._2)));
+    JavaPairRDD<String, Tuple2<String, List<String>>> 
partitionToFileIdAndRecordsPairRDD = 
partitionFileIndexInfoPairRDD.join(partitionToRecords);
+
+    // Step 2: For each partition, fileId, List<recordKeys> Triplet -> Fetch 
RDD of triplets ( Tuple2< HoodieKey, HoodieRecordLocation >)
+
+    /*Option1:
+    return partitionToFileIdAndRecordsPairRDD.flatMapToPair(
+        (PairFlatMapFunction<Tuple2<String, Tuple2<String, List<String>>>, 
HoodieKey, HoodieRecordLocation>) stringTuple2Tuple2 -> {
+          HoodieDataFile latestDataFile = getLatestDataFile(hoodieTable, 
Pair.of(stringTuple2Tuple2._1, stringTuple2Tuple2._2._1));
+          List<Pair<HoodieKey, HoodieRecordLocation>> resultList = 
ParquetUtils.fetchRecordKeyPartitionPathFromParquet(hoodieTable.getHadoopConf(),
 new Path(latestDataFile.getPath()),
+              latestDataFile.getCommitTime(), stringTuple2Tuple2._2._1, 
stringTuple2Tuple2._2._2);
+          List<Tuple2<HoodieKey, HoodieRecordLocation>> toReturn = new 
ArrayList<>();
+          resultList.forEach(rec -> toReturn.add(new Tuple2<>(rec.getLeft(), 
rec.getRight())));
+          return toReturn.iterator();
+        });*/
+
+    // Option2:
+    return partitionToFileIdAndRecordsPairRDD.mapPartitions(
 
 Review comment:
   Since the incoming records are files, I am not sure how much value we get in 
introducing a LazyIterator. I have both options listed here(inline vs lazy 
iterator). Let me know your thoughts. Interested in understanding the nuances. 

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to