pratyakshsharma commented on a change in pull request #1554: URL: https://github.com/apache/incubator-hudi/pull/1554#discussion_r415281069
########## File path: hudi-cli/src/test/java/org/apache/hudi/cli/integ/ITTestRepairsCommand.java ########## @@ -0,0 +1,170 @@ +/* + * 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.cli.integ; + +import org.apache.avro.Schema; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.Path; +import org.apache.hudi.avro.HoodieAvroUtils; +import org.apache.hudi.cli.AbstractShellIntegrationTest; +import org.apache.hudi.cli.HoodieCLI; +import org.apache.hudi.cli.commands.RepairsCommand; +import org.apache.hudi.cli.commands.TableCommand; +import org.apache.hudi.common.HoodieClientTestUtils; +import org.apache.hudi.common.HoodieTestDataGenerator; +import org.apache.hudi.common.fs.FSUtils; +import org.apache.hudi.common.model.HoodieLogFile; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.model.HoodieTableType; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.timeline.versioning.TimelineLayoutVersion; +import org.apache.hudi.common.table.view.HoodieTableFileSystemView; +import org.apache.hudi.common.util.SchemaTestUtil; +import org.apache.spark.sql.Dataset; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.shell.core.CommandResult; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +import static org.apache.spark.sql.functions.lit; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Integration test class for {@link RepairsCommand#deduplicate}. + */ +public class ITTestRepairsCommand extends AbstractShellIntegrationTest { + String duplicatedPartitionPath; + String repairedOutputPath; + + @Before + public void init() throws IOException, URISyntaxException { + String tablePath = basePath + File.separator + "test_table"; + duplicatedPartitionPath = tablePath + File.separator + HoodieTestDataGenerator.DEFAULT_FIRST_PARTITION_PATH; + repairedOutputPath = basePath + File.separator + "tmp"; + + HoodieCLI.conf = jsc.hadoopConfiguration(); + + // Create table and connect + new TableCommand().createTable( + tablePath, "test_table", HoodieTableType.COPY_ON_WRITE.name(), + "", TimelineLayoutVersion.VERSION_1, "org.apache.hudi.common.model.HoodieAvroPayload"); + + // generate 200 records + Schema schema = HoodieAvroUtils.addMetadataFields(SchemaTestUtil.getSimpleSchema()); + + String fileName1 = "1_0_20160401010101.parquet"; + String fileName2 = "2_0_20160401010101.parquet"; + + List<HoodieRecord> hoodieRecords1 = SchemaTestUtil.generateHoodieTestRecords(0, 100, schema); + HoodieClientTestUtils.writeParquetFile(tablePath, HoodieTestDataGenerator.DEFAULT_FIRST_PARTITION_PATH, + fileName1, hoodieRecords1, schema, null, false); + List<HoodieRecord> hoodieRecords2 = SchemaTestUtil.generateHoodieTestRecords(100, 100, schema); + HoodieClientTestUtils.writeParquetFile(tablePath, HoodieTestDataGenerator.DEFAULT_FIRST_PARTITION_PATH, + fileName2, hoodieRecords2, schema, null, false); + + // generate commit file + String fileId1 = UUID.randomUUID().toString(); + String testWriteToken = "1-0-1"; + String commitTime = FSUtils.getCommitTime(fileName1); + new File(duplicatedPartitionPath + "/" + + FSUtils.makeLogFileName(fileId1, HoodieLogFile.DELTA_EXTENSION, commitTime, 1, testWriteToken)) + .createNewFile(); + new File(tablePath + "/.hoodie/" + commitTime + ".commit").createNewFile(); + + // read records and get 10 to generate duplicates + Dataset df = sqlContext.read().parquet(duplicatedPartitionPath); + + String fileName3 = "3_0_20160401010101.parquet"; + df.limit(10).withColumn("_hoodie_commit_time", lit("20160401010202")) + .write().parquet(duplicatedPartitionPath + File.separator + fileName3); + + metaClient = HoodieTableMetaClient.reload(HoodieCLI.getTableMetaClient()); + } + + /** + * Test case for dry run deduplicate. + */ + @Test + public void testDeduplicate() throws IOException { + // get fs and check number of latest files + HoodieTableFileSystemView fsView = new HoodieTableFileSystemView(metaClient, + metaClient.getActiveTimeline().getCommitTimeline().filterCompletedInstants(), + fs.listStatus(new Path(duplicatedPartitionPath))); + List<String> filteredStatuses = fsView.getLatestBaseFiles().map(f -> f.getPath()).collect(Collectors.toList()); + assertEquals("There should be 3 files.", 3, filteredStatuses.size()); + + // Before deduplicate, all files contain 210 records + String[] files = filteredStatuses.toArray(new String[filteredStatuses.size()]); + Dataset df = sqlContext.read().parquet(files); + assertEquals(210, df.count()); + + String partitionPath = "2016/03/15"; Review comment: Let us use the constant from HoodieTestDataGenerator rather than using this string when running actual command. ---------------------------------------------------------------- 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