xushiyan commented on code in PR #8675:
URL: https://github.com/apache/hudi/pull/8675#discussion_r1191050917


##########
hudi-common/src/test/java/org/apache/hudi/common/testutils/HoodieAdaptablePayloadDataGenerator.java:
##########
@@ -0,0 +1,228 @@
+/*
+ * 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.testutils;
+
+import org.apache.hudi.avro.HoodieAvroUtils;
+import org.apache.hudi.common.model.AWSDmsAvroPayload;
+import org.apache.hudi.common.model.DefaultHoodieRecordPayload;
+import org.apache.hudi.common.model.HoodieAvroIndexedRecord;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.model.MetadataValues;
+import org.apache.hudi.common.model.OverwriteNonDefaultsWithLatestAvroPayload;
+import org.apache.hudi.common.model.OverwriteWithLatestAvroPayload;
+import org.apache.hudi.common.model.PartialUpdateAvroPayload;
+import org.apache.hudi.common.model.debezium.DebeziumConstants;
+import org.apache.hudi.common.model.debezium.MySqlDebeziumAvroPayload;
+import org.apache.hudi.common.model.debezium.PostgresDebeziumAvroPayload;
+import org.apache.hudi.common.table.HoodieTableConfig;
+import org.apache.hudi.common.util.Option;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericRecord;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static 
org.apache.hudi.common.model.HoodieRecord.HOODIE_IS_DELETED_FIELD;
+import static org.apache.hudi.common.util.ValidationUtils.checkArgument;
+
+public class HoodieAdaptablePayloadDataGenerator {
+
+  public static final Schema SCHEMA = 
SchemaTestUtil.getSchemaFromResource(HoodieAdaptablePayloadDataGenerator.class, 
"/adaptable-payload.avsc");
+  public static final Schema SCHEMA_WITH_METAFIELDS = 
HoodieAvroUtils.addMetadataFields(SCHEMA, false);
+  public static final String SCHEMA_STR = SCHEMA.toString();
+
+  public static Properties getKeyGenProps(Class<?> payloadClass) {
+    String orderingField = new RecordGen(payloadClass).getOrderingField();
+    Properties props = new Properties();
+    props.put("hoodie.datasource.write.recordkey.field", "id");
+    props.put("hoodie.datasource.write.partitionpath.field", "pt");
+    props.put("hoodie.datasource.write.precombine.field", orderingField);
+    props.put(HoodieTableConfig.RECORDKEY_FIELDS.key(), "id");
+    props.put(HoodieTableConfig.PARTITION_FIELDS.key(), "pt");
+    props.put(HoodieTableConfig.PRECOMBINE_FIELD.key(), orderingField);
+    return props;
+  }
+
+  public static Properties getPayloadProps(Class<?> payloadClass) {
+    String orderingField = new RecordGen(payloadClass).getOrderingField();
+    Properties props = new Properties();
+    props.put("hoodie.compaction.payload.class", payloadClass.getName());
+    props.put("hoodie.payload.event.time.field", orderingField);
+    props.put("hoodie.payload.ordering.field", orderingField);
+    return props;
+  }
+
+  public static List<HoodieRecord> getInserts(int n, String partition, long 
ts, Class<?> payloadClass) throws IOException {
+    return getInserts(n, new String[] {partition}, ts, payloadClass);
+  }
+
+  public static List<HoodieRecord> getInserts(int n, String[] partitions, long 
ts, Class<?> payloadClass) throws IOException {
+    List<HoodieRecord> inserts = new ArrayList<>();
+    RecordGen recordGen = new RecordGen(payloadClass);
+    for (GenericRecord r : getInserts(n, partitions, ts, recordGen)) {
+      inserts.add(getHoodieRecord(r, recordGen.getPayloadClass()));
+    }
+    return inserts;
+  }
+
+  private static List<GenericRecord> getInserts(int n, String[] partitions, 
long ts, RecordGen recordGen) {
+    return IntStream.range(0, n).mapToObj(id -> {
+      String pt = partitions.length == 0 ? "" : partitions[id % 
partitions.length];
+      return getInsert(id, pt, ts, recordGen);
+    }).collect(Collectors.toList());
+  }
+
+  private static GenericRecord getInsert(int id, String pt, long ts, RecordGen 
recordGen) {
+    GenericRecord r = new GenericData.Record(SCHEMA);
+    r.put("id", id);
+    r.put("pt", pt);
+    return recordGen.populateForInsert(r, ts);
+  }
+
+  public static List<HoodieRecord> getUpdates(List<HoodieRecord> baseRecords, 
long ts, Class<?> payloadClass) throws IOException {
+    RecordGen recordGen = new RecordGen(payloadClass);
+    List<HoodieRecord> updates = new ArrayList<>();
+    Properties props = new Properties();
+    for (HoodieRecord r : baseRecords) {
+      GenericRecord gr = (GenericRecord) r.toIndexedRecord(SCHEMA, 
props).get().getData();
+      GenericRecord updated = 
getUpdate(Integer.parseInt(gr.get("id").toString()), gr.get("pt").toString(), 
ts, recordGen);
+      updates.add(getHoodieRecord(updated, recordGen.getPayloadClass()));
+    }
+    return updates;
+  }
+
+  private static GenericRecord getUpdate(int id, String pt, long ts, RecordGen 
recordGen) {
+    GenericRecord r = new GenericData.Record(SCHEMA);
+    r.put("id", id);

Review Comment:
   @danny0405 there are payloads like BaseAvroPayload 
(org.apache.hudi.common.model.BaseAvroPayload#isDeleteRecord) and subclasses 
actually check the delete marker field to determine if it's delete record. Also 
like Debezium payload checks `op=d` to determine deletion. so this is to 
emulate that situation. 



-- 
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-unsubscr...@hudi.apache.org

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

Reply via email to