bhasudha commented on a change in pull request #929: [HUDI-271] Create 
QuickstartUtils class towards simplifying quickstar…
URL: https://github.com/apache/incubator-hudi/pull/929#discussion_r329338800
 
 

 ##########
 File path: hudi-spark/src/main/java/org/apache/hudi/QuickstartUtils.java
 ##########
 @@ -0,0 +1,319 @@
+/*
+ * 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;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import java.util.zip.Deflater;
+import java.util.zip.DeflaterOutputStream;
+import java.util.zip.InflaterInputStream;
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.generic.IndexedRecord;
+import org.apache.hudi.avro.MercifulJsonConverter;
+import org.apache.hudi.common.model.HoodieKey;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.model.HoodieRecordPayload;
+import org.apache.hudi.common.util.FileIOUtils;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.exception.HoodieIOException;
+
+/**
+ * Class to be used in quickstart guide for generating inserts and updates 
against a corpus.
+ * <p>
+ * Test data uses a toy Uber trips, data model.
+ *
+ */
+public class QuickstartUtils {
+
+  public static class DataGenerator {
+    private static final String DEFAULT_FIRST_PARTITION_PATH = "2019/09/15";
+    private static final String DEFAULT_SECOND_PARTITION_PATH = "2018/09/16";
+    private static final String DEFAULT_THIRD_PARTITION_PATH = "2018/09/17";
+
+    private static final String[] DEFAULT_PARTITION_PATHS = {
+        DEFAULT_FIRST_PARTITION_PATH,
+        DEFAULT_SECOND_PARTITION_PATH,
+        DEFAULT_THIRD_PARTITION_PATH
+    };
+    static String TRIP_EXAMPLE_SCHEMA = "{\"type\": \"record\"," + "\"name\": 
\"triprec\"," + "\"fields\": [ "
+        + "{\"name\": \"ts\",\"type\": \"double\"},"
+        + "{\"name\": \"uuid\", \"type\": \"string\"},"
+        + "{\"name\": \"rider\", \"type\": \"string\"},"
+        + "{\"name\": \"driver\", \"type\": \"string\"},"
+        + "{\"name\": \"begin_lat\", \"type\": \"double\"},"
+        + "{\"name\": \"begin_lon\", \"type\": \"double\"},"
+        + "{\"name\": \"end_lat\", \"type\": \"double\"},"
+        + "{\"name\": \"end_lon\", \"type\": \"double\"},"
+        + "{\"name\":\"fare\",\"type\": \"double\"}]}";
+    static Schema avroSchema = new Schema.Parser().parse(TRIP_EXAMPLE_SCHEMA);
+
+    private static Random rand = new Random(46474747);
+
+    private final Map<Integer, KeyPartition> existingKeys;
+    private final String[] partitionPaths;
+    private int numExistingKeys;
+
+    public DataGenerator() {
+      this(DEFAULT_PARTITION_PATHS, new HashMap<>());
+    }
+
+    private DataGenerator(String[] partitionPaths, Map<Integer, KeyPartition> 
keyPartitionMap) {
+      this.partitionPaths = Arrays.copyOf(partitionPaths, 
partitionPaths.length);
+      this.existingKeys = keyPartitionMap;
+    }
+
+    private static String generateRandomCommitMsg() {
+      int leftLimit = 48; // ascii for 0
+      int rightLimit = 57; // ascii for 9
+      int stringLength = 3;
+      StringBuilder buffer = new StringBuilder(stringLength);
+      for (int i = 0; i < stringLength; i++) {
+        int randomLimitedInt = leftLimit + (int)
+            (rand.nextFloat() * (rightLimit - leftLimit + 1));
+        buffer.append((char) randomLimitedInt);
+      }
+      return buffer.toString();
+    }
+
+    public int getNumExistingKeys() {
+      return numExistingKeys;
+    }
+
+    public static GenericRecord generateGenericRecord(String rowKey, String 
riderName, String driverName,
+                                                      double timestamp) {
+      GenericRecord rec = new GenericData.Record(avroSchema);
+      rec.put("uuid", rowKey);
+      rec.put("ts", timestamp);
+      rec.put("rider", riderName);
+      rec.put("driver", driverName);
+      rec.put("begin_lat", rand.nextDouble());
+      rec.put("begin_lon", rand.nextDouble());
+      rec.put("end_lat", rand.nextDouble());
+      rec.put("end_lon", rand.nextDouble());
+      rec.put("fare", rand.nextDouble() * 100);
+      return rec;
+    }
+
+    /**
+     * Generates a new avro record of the above schema format, retaining the 
key if optionally provided.
+     */
+    public static TestRawTripPayload generateRandomValue(HoodieKey key, String 
commitTime) throws IOException {
+      GenericRecord rec = generateGenericRecord(key.getRecordKey(), "rider-" + 
commitTime, "driver-" + commitTime, 0.0);
+      return new TestRawTripPayload(rec.toString(), key.getRecordKey(), 
key.getPartitionPath(), TRIP_EXAMPLE_SCHEMA);
+    }
+
+    /**
+     * Generates new inserts, uniformly across the partition paths above. It 
also updates the list of existing keys.
+     */
+    public Stream<HoodieRecord> generateInsertsStream(String commitTime, 
Integer n) {
+      int currSize = getNumExistingKeys();
+
+      return IntStream.range(0, n).boxed().map(i -> {
+        String partitionPath = 
partitionPaths[rand.nextInt(partitionPaths.length)];
+        HoodieKey key = new HoodieKey(UUID.randomUUID().toString(), 
partitionPath);
+        KeyPartition kp = new KeyPartition();
+        kp.key = key;
+        kp.partitionPath = partitionPath;
+        existingKeys.put(currSize + i, kp);
+        numExistingKeys++;
+        try {
+          return new HoodieRecord(key, generateRandomValue(key, commitTime));
+        } catch (IOException e) {
+          throw new HoodieIOException(e.getMessage(), e);
+        }
+      });
+    }
+
+    /**
+     * Generates new inserts, uniformly across the partition paths above. It 
also updates the list of existing keys.
+     */
+    public List<HoodieRecord> generateInserts(Integer n) throws IOException {
+      String commitTime = generateRandomCommitMsg();
+      return generateInsertsStream(commitTime, n).collect(Collectors.toList());
+    }
+
+    public HoodieRecord generateUpdateRecord(HoodieKey key, String commitTime) 
throws IOException {
+      return new HoodieRecord(key, generateRandomValue(key, commitTime));
+    }
+
+    /**
+     * Generates new updates, randomly distributed across the keys above. 
There can be duplicates within the returned
+     * list
+     *
+     * @param n Number of updates (including dups)
+     * @return list of hoodie record updates
+     */
+    public List<HoodieRecord> generateUpdates(Integer n) throws IOException {
+      String commitTime = generateRandomCommitMsg();
+      List<HoodieRecord> updates = new ArrayList<>();
+      for (int i = 0; i < n; i++) {
+        KeyPartition kp = existingKeys.get(rand.nextInt(numExistingKeys - 1));
+        HoodieRecord record = generateUpdateRecord(kp.key, commitTime);
+        updates.add(record);
+      }
+      return updates;
+    }
+
+    public static class KeyPartition implements Serializable {
+      HoodieKey key;
+      String partitionPath;
 
 Review comment:
   sure.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to