From commits-return-12367-archive-asf-public=cust-asf.ponee.io@hudi.apache.org Sun Mar 1 10:39:17 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id C9F7018069C for ; Sun, 1 Mar 2020 11:39:16 +0100 (CET) Received: (qmail 28862 invoked by uid 500); 1 Mar 2020 10:39:16 -0000 Mailing-List: contact commits-help@hudi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hudi.apache.org Delivered-To: mailing list commits@hudi.apache.org Received: (qmail 28792 invoked by uid 99); 1 Mar 2020 10:39:16 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 01 Mar 2020 10:39:16 +0000 From: GitBox To: commits@hudi.apache.org Subject: [GitHub] [incubator-hudi] xushiyan commented on a change in pull request #1360: [HUDI-344][RFC-09] Hudi Dataset Snapshot Exporter Message-ID: <158305915608.19334.17635354819042967297.gitbox@gitbox.apache.org> References: In-Reply-To: Date: Sun, 01 Mar 2020 10:39:16 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit xushiyan commented on a change in pull request #1360: [HUDI-344][RFC-09] Hudi Dataset Snapshot Exporter URL: https://github.com/apache/incubator-hudi/pull/1360#discussion_r386096648 ########## File path: hudi-utilities/src/test/java/org/apache/hudi/utilities/TestHoodieSnapshotExporter.java ########## @@ -0,0 +1,227 @@ +/* + * 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; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hudi.DataSourceWriteOptions; +import org.apache.hudi.common.HoodieTestDataGenerator; +import org.apache.hudi.common.model.HoodieTestUtils; +import org.apache.hudi.common.util.FSUtils; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.spark.api.java.JavaSparkContext; +import org.apache.spark.sql.Dataset; +import org.apache.spark.sql.Row; +import org.apache.spark.sql.SaveMode; +import org.apache.spark.sql.SparkSession; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class TestHoodieSnapshotExporter { + private static String TEST_WRITE_TOKEN = "1-0-1"; + + private SparkSession spark = null; + private HoodieTestDataGenerator dataGen = null; + private String basePath = null; + private String outputPath = null; + private String rootPath = null; + private FileSystem fs = null; + private Map commonOpts; + private HoodieSnapshotExporter.Config cfg; + private JavaSparkContext jsc = null; + + @Before + public void initialize() throws IOException { + spark = SparkSession.builder() + .appName("Hoodie Datasource test") + .master("local[2]") + .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer") + .getOrCreate(); + jsc = new JavaSparkContext(spark.sparkContext()); + dataGen = new HoodieTestDataGenerator(); + TemporaryFolder folder = new TemporaryFolder(); + folder.create(); + basePath = folder.getRoot().getAbsolutePath(); + fs = FSUtils.getFs(basePath, spark.sparkContext().hadoopConfiguration()); + commonOpts = new HashMap(); + + commonOpts.put("hoodie.insert.shuffle.parallelism", "4"); + commonOpts.put("hoodie.upsert.shuffle.parallelism", "4"); + commonOpts.put(DataSourceWriteOptions.RECORDKEY_FIELD_OPT_KEY(), "_row_key"); + commonOpts.put(DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY(), "partition"); + commonOpts.put(DataSourceWriteOptions.PRECOMBINE_FIELD_OPT_KEY(), "timestamp"); + commonOpts.put(HoodieWriteConfig.TABLE_NAME, "hoodie_test"); + + + cfg = new HoodieSnapshotExporter.Config(); + + cfg.sourceBasePath = basePath; + cfg.targetOutputPath = outputPath = basePath + "/target"; + cfg.outputFormat = "json"; + cfg.outputPartitionField = "partition"; + + } + + @After + public void cleanup() throws Exception { + if (spark != null) { + spark.stop(); + } + } + + @Test + public void testSnapshotExporter() throws IOException { + // Insert Operation + List records = DataSourceTestUtils.convertToStringList(dataGen.generateInserts("000", 100)); + Dataset inputDF = spark.read().json(new JavaSparkContext(spark.sparkContext()).parallelize(records, 2)); + inputDF.write().format("hudi") + .options(commonOpts) + .option(DataSourceWriteOptions.OPERATION_OPT_KEY(), DataSourceWriteOptions.INSERT_OPERATION_OPT_VAL()) + .mode(SaveMode.Overwrite) + .save(basePath); + long sourceCount = inputDF.count(); + + HoodieSnapshotExporter hoodieSnapshotExporter = new HoodieSnapshotExporter(); + hoodieSnapshotExporter.export(spark, cfg); + + long targetCount = spark.read().json(outputPath).count(); + + assertTrue(sourceCount == targetCount); + + // Test snapshotPrefix + long filterCount = inputDF.where("partition == '2015/03/16'").count(); + cfg.snapshotPrefix = "2015/03/16"; + hoodieSnapshotExporter.export(spark, cfg); + long targetFilterCount = spark.read().json(outputPath).count(); + assertTrue(filterCount == targetFilterCount); + + } + + // for testEmptySnapshotCopy + public void init() throws IOException { + TemporaryFolder folder = new TemporaryFolder(); + folder.create(); + rootPath = "file://" + folder.getRoot().getAbsolutePath(); + basePath = rootPath + "/" + HoodieTestUtils.RAW_TRIPS_TEST_NAME; + outputPath = rootPath + "/output"; + + final Configuration hadoopConf = HoodieTestUtils.getDefaultHadoopConf(); + fs = FSUtils.getFs(basePath, hadoopConf); + HoodieTestUtils.init(hadoopConf, basePath); + } + + @Test + public void testEmptySnapshotCopy() throws IOException { + init(); + // There is no real data (only .hoodie directory) + assertEquals(fs.listStatus(new Path(basePath)).length, 1); + assertFalse(fs.exists(new Path(outputPath))); + + // Do the snapshot + HoodieSnapshotCopier copier = new HoodieSnapshotCopier(); + copier.snapshot(jsc, basePath, outputPath, true); + + // Nothing changed; we just bail out + assertEquals(fs.listStatus(new Path(basePath)).length, 1); + assertFalse(fs.exists(new Path(outputPath + "/_SUCCESS"))); + } + + // TODO - uncomment this after fixing test failures + // @Test Review comment: Notice this is from original test for Snapshot copier, not sure about the historical issue for skipping it. I would slightly favor fixing it while you're at this stage so that we can claim the new "Exporter" is better tested than the old "Copier" :) ---------------------------------------------------------------- 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: users@infra.apache.org With regards, Apache Git Services