codope commented on code in PR #9336: URL: https://github.com/apache/hudi/pull/9336#discussion_r1285242254
########## hudi-utilities/src/test/java/org/apache/hudi/utilities/sources/TestS3EventsHoodieIncrSource.java: ########## @@ -0,0 +1,320 @@ +/* + * 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.utilities.sources; + +import org.apache.hudi.client.SparkRDDWriteClient; +import org.apache.hudi.client.WriteStatus; +import org.apache.hudi.common.config.HoodieMetadataConfig; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.model.HoodieAvroPayload; +import org.apache.hudi.common.model.HoodieAvroRecord; +import org.apache.hudi.common.model.HoodieKey; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.timeline.versioning.TimelineLayoutVersion; +import org.apache.hudi.common.testutils.SchemaTestUtil; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.common.util.collection.Triple; +import org.apache.hudi.config.HoodieArchivalConfig; +import org.apache.hudi.config.HoodieCleanConfig; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.testutils.SparkClientFunctionalTestHarness; +import org.apache.hudi.utilities.schema.SchemaProvider; +import org.apache.hudi.utilities.sources.helpers.CloudDataFetcher; +import org.apache.hudi.utilities.sources.helpers.IncrSourceHelper; +import org.apache.hudi.utilities.sources.helpers.QueryRunner; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericRecord; +import org.apache.spark.api.java.JavaRDD; +import org.apache.spark.api.java.JavaSparkContext; +import org.apache.spark.sql.Dataset; +import org.apache.spark.sql.Row; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.stream.Collectors; + +import static org.apache.hudi.testutils.Assertions.assertNoWriteErrors; +import static org.apache.hudi.utilities.sources.helpers.IncrSourceHelper.MissingCheckpointStrategy.READ_UPTO_LATEST_COMMIT; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class TestS3EventsHoodieIncrSource extends SparkClientFunctionalTestHarness { + private static final Schema S3_METADATA_SCHEMA = SchemaTestUtil.getSchemaFromResource( + TestS3EventsHoodieIncrSource.class, "/streamer-config/s3-metadata.avsc", true); + + private ObjectMapper mapper = new ObjectMapper(); + + private static final String MY_BUCKET = "some-bucket"; + + @Mock + private SchemaProvider mockSchemaProvider; + @Mock + QueryRunner mockQueryRunner; + @Mock + CloudDataFetcher mockCloudDataFetcher; + private JavaSparkContext jsc; + private HoodieTableMetaClient metaClient; + + @BeforeEach + public void setUp() throws IOException { + jsc = JavaSparkContext.fromSparkContext(spark().sparkContext()); + metaClient = getHoodieMetaClient(hadoopConf(), basePath()); + } + + private List<String> getSampleS3ObjectKeys(List<Triple<String, Long, String>> filePathSizeAndCommitTime) { + return filePathSizeAndCommitTime.stream().map(f -> { + try { + return generateS3EventMetadata(f.getMiddle(), MY_BUCKET, f.getLeft(), f.getRight()); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + }).collect(Collectors.toList()); + } + + private Dataset<Row> generateDataset(List<Triple<String, Long, String>> filePathSizeAndCommitTime) { + JavaRDD<String> testRdd = jsc.parallelize(getSampleS3ObjectKeys(filePathSizeAndCommitTime), 2); + Dataset<Row> inputDs = spark().read().json(testRdd); + return inputDs; + } + + /** + * Generates simple Json structure like below + * <p> + * s3 : { + * object : { + * size: + * key: + * } + * bucket: { + * name: + * } + */ + private String generateS3EventMetadata(Long objectSize, String bucketName, String objectKey, String commitTime) + throws JsonProcessingException { + Map<String, Object> objectMetadata = new HashMap<>(); + objectMetadata.put("size", objectSize); + objectMetadata.put("key", objectKey); + Map<String, String> bucketMetadata = new HashMap<>(); + bucketMetadata.put("name", bucketName); + Map<String, Object> s3Metadata = new HashMap<>(); + s3Metadata.put("object", objectMetadata); + s3Metadata.put("bucket", bucketMetadata); + Map<String, Object> eventMetadata = new HashMap<>(); + eventMetadata.put("s3", s3Metadata); + eventMetadata.put("_hoodie_commit_time", commitTime); + return mapper.writeValueAsString(eventMetadata); + } + + private HoodieRecord generateS3EventMetadata(String commitTime, String bucketName, String objectKey, Long objectSize) { + String partitionPath = bucketName; + Schema schema = S3_METADATA_SCHEMA; + GenericRecord rec = new GenericData.Record(schema); + Schema.Field s3Field = schema.getField("s3"); + Schema s3Schema = s3Field.schema().getTypes().get(1); // Assuming the record schema is the second type + // Create a generic record for the "s3" field + GenericRecord s3Record = new GenericData.Record(s3Schema); + + Schema.Field s3BucketField = s3Schema.getField("bucket"); + Schema s3Bucket = s3BucketField.schema().getTypes().get(1); // Assuming the record schema is the second type + GenericRecord s3BucketRec = new GenericData.Record(s3Bucket); + s3BucketRec.put("name", bucketName); + + + Schema.Field s3ObjectField = s3Schema.getField("object"); + Schema s3Object = s3ObjectField.schema().getTypes().get(1); // Assuming the record schema is the second type + GenericRecord s3ObjectRec = new GenericData.Record(s3Object); + s3ObjectRec.put("key", objectKey); + s3ObjectRec.put("size", objectSize); + + s3Record.put("bucket", s3BucketRec); + s3Record.put("object", s3ObjectRec); + rec.put("s3", s3Record); + rec.put("_hoodie_commit_time", commitTime); + + HoodieAvroPayload payload = new HoodieAvroPayload(Option.of(rec)); + return new HoodieAvroRecord(new HoodieKey(objectKey, partitionPath), payload); + } + + private TypedProperties setProps(IncrSourceHelper.MissingCheckpointStrategy missingCheckpointStrategy) { + Properties properties = new Properties(); + properties.setProperty("hoodie.deltastreamer.source.hoodieincr.path", basePath()); + properties.setProperty("hoodie.deltastreamer.source.hoodieincr.missing.checkpoint.strategy", + missingCheckpointStrategy.name()); + properties.setProperty("hoodie.deltastreamer.source.hoodieincr.file.format", "json"); Review Comment: Cool, please file a JIRA. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
