yanghua commented on a change in pull request #1522:
URL: https://github.com/apache/incubator-hudi/pull/1522#discussion_r414502524



##########
File path: 
hudi-cli/src/test/java/org/apache/hudi/cli/commands/TestHoodieLogFileCommand.java
##########
@@ -0,0 +1,210 @@
+/*
+ * 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.commands;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.avro.Schema;
+import org.apache.avro.generic.IndexedRecord;
+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.HoodieTableHeaderFields;
+import org.apache.hudi.cli.HoodiePrintHelper;
+import org.apache.hudi.cli.TableHeader;
+import org.apache.hudi.cli.common.HoodieTestCommitMetadataGenerator;
+import org.apache.hudi.common.model.HoodieLogFile;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.model.HoodieRecordPayload;
+import org.apache.hudi.common.model.HoodieTableType;
+import org.apache.hudi.common.table.log.HoodieLogFormat;
+import org.apache.hudi.common.table.log.HoodieMergedLogRecordScanner;
+import org.apache.hudi.common.table.log.block.HoodieAvroDataBlock;
+import org.apache.hudi.common.table.log.block.HoodieLogBlock;
+import org.apache.hudi.common.table.timeline.versioning.TimelineLayoutVersion;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.common.util.SchemaTestUtil;
+import org.apache.hudi.config.HoodieCompactionConfig;
+import org.apache.hudi.config.HoodieMemoryConfig;
+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.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static org.apache.hudi.common.util.SchemaTestUtil.getSimpleSchema;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test Cases for {@link HoodieLogFileCommand}.
+ */
+public class TestHoodieLogFileCommand extends AbstractShellIntegrationTest {
+
+  private String partitionPath;
+  private HoodieAvroDataBlock dataBlock;
+  private String tablePath;
+
+  private static final String INSTANT_TIME = "100";
+
+  @Before
+  public void init() throws IOException, InterruptedException, 
URISyntaxException {
+    HoodieCLI.conf = jsc.hadoopConfiguration();
+
+    // Create table and connect
+    String tableName = "test_table";
+    tablePath = basePath + File.separator + tableName;
+    partitionPath = tablePath + File.separator + 
HoodieTestCommitMetadataGenerator.DEFAULT_FIRST_PARTITION_PATH;
+    new TableCommand().createTable(
+        tablePath, tableName, HoodieTableType.MERGE_ON_READ.name(),
+        "", TimelineLayoutVersion.VERSION_1, 
"org.apache.hudi.common.model.HoodieAvroPayload");
+
+    new File(partitionPath).mkdirs();
+    HoodieLogFormat.Writer writer =
+        HoodieLogFormat.newWriterBuilder().onParentPath(new 
Path(partitionPath))
+            .withFileExtension(HoodieLogFile.DELTA_EXTENSION)
+            
.withFileId("test-log-fileid1").overBaseCommit("100").withFs(fs).build();
+
+    // write data to file
+    List<IndexedRecord> records = SchemaTestUtil.generateTestRecords(0, 100);
+    Map<HoodieLogBlock.HeaderMetadataType, String> header = new HashMap<>();
+    header.put(HoodieLogBlock.HeaderMetadataType.INSTANT_TIME, INSTANT_TIME);
+    header.put(HoodieLogBlock.HeaderMetadataType.SCHEMA, 
getSimpleSchema().toString());
+    dataBlock = new HoodieAvroDataBlock(records, header);
+    writer = writer.appendBlock(dataBlock);
+    writer.close();

Review comment:
       We should protect the resource leak here.

##########
File path: 
hudi-cli/src/test/java/org/apache/hudi/cli/commands/TestHoodieLogFileCommand.java
##########
@@ -0,0 +1,210 @@
+/*
+ * 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.commands;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.avro.Schema;
+import org.apache.avro.generic.IndexedRecord;
+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.HoodieTableHeaderFields;
+import org.apache.hudi.cli.HoodiePrintHelper;
+import org.apache.hudi.cli.TableHeader;
+import org.apache.hudi.cli.common.HoodieTestCommitMetadataGenerator;
+import org.apache.hudi.common.model.HoodieLogFile;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.model.HoodieRecordPayload;
+import org.apache.hudi.common.model.HoodieTableType;
+import org.apache.hudi.common.table.log.HoodieLogFormat;
+import org.apache.hudi.common.table.log.HoodieMergedLogRecordScanner;
+import org.apache.hudi.common.table.log.block.HoodieAvroDataBlock;
+import org.apache.hudi.common.table.log.block.HoodieLogBlock;
+import org.apache.hudi.common.table.timeline.versioning.TimelineLayoutVersion;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.common.util.SchemaTestUtil;
+import org.apache.hudi.config.HoodieCompactionConfig;
+import org.apache.hudi.config.HoodieMemoryConfig;
+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.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static org.apache.hudi.common.util.SchemaTestUtil.getSimpleSchema;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test Cases for {@link HoodieLogFileCommand}.
+ */
+public class TestHoodieLogFileCommand extends AbstractShellIntegrationTest {
+
+  private String partitionPath;
+  private HoodieAvroDataBlock dataBlock;
+  private String tablePath;
+
+  private static final String INSTANT_TIME = "100";
+
+  @Before
+  public void init() throws IOException, InterruptedException, 
URISyntaxException {
+    HoodieCLI.conf = jsc.hadoopConfiguration();
+
+    // Create table and connect
+    String tableName = "test_table";
+    tablePath = basePath + File.separator + tableName;
+    partitionPath = tablePath + File.separator + 
HoodieTestCommitMetadataGenerator.DEFAULT_FIRST_PARTITION_PATH;
+    new TableCommand().createTable(
+        tablePath, tableName, HoodieTableType.MERGE_ON_READ.name(),
+        "", TimelineLayoutVersion.VERSION_1, 
"org.apache.hudi.common.model.HoodieAvroPayload");
+
+    new File(partitionPath).mkdirs();
+    HoodieLogFormat.Writer writer =
+        HoodieLogFormat.newWriterBuilder().onParentPath(new 
Path(partitionPath))
+            .withFileExtension(HoodieLogFile.DELTA_EXTENSION)
+            
.withFileId("test-log-fileid1").overBaseCommit("100").withFs(fs).build();
+
+    // write data to file
+    List<IndexedRecord> records = SchemaTestUtil.generateTestRecords(0, 100);
+    Map<HoodieLogBlock.HeaderMetadataType, String> header = new HashMap<>();
+    header.put(HoodieLogBlock.HeaderMetadataType.INSTANT_TIME, INSTANT_TIME);
+    header.put(HoodieLogBlock.HeaderMetadataType.SCHEMA, 
getSimpleSchema().toString());
+    dataBlock = new HoodieAvroDataBlock(records, header);
+    writer = writer.appendBlock(dataBlock);
+    writer.close();
+  }
+
+  /**
+   * Test case for 'show logfile metadata'.
+   */
+  @Test
+  public void testShowLogFileCommits() throws JsonProcessingException {
+    CommandResult cr = getShell().executeCommand("show logfile metadata 
--logFilePathPattern " + partitionPath + "/*");
+    assertTrue(cr.isSuccess());
+
+    TableHeader header = new 
TableHeader().addTableHeaderField(HoodieTableHeaderFields.HEADER_INSTANT_TIME)
+        .addTableHeaderField(HoodieTableHeaderFields.HEADER_RECORD_COUNT)
+        .addTableHeaderField(HoodieTableHeaderFields.HEADER_BLOCK_TYPE)
+        .addTableHeaderField(HoodieTableHeaderFields.HEADER_HEADER_METADATA)
+        .addTableHeaderField(HoodieTableHeaderFields.HEADER_FOOTER_METADATA);
+
+    // construct expect result, there is only 1 line.
+    List<Comparable[]> rows = new ArrayList<>();
+    ObjectMapper objectMapper = new ObjectMapper();
+    String headerStr = 
objectMapper.writeValueAsString(dataBlock.getLogBlockHeader());
+    String footerStr = 
objectMapper.writeValueAsString(dataBlock.getLogBlockFooter());
+    Comparable[] output = new Comparable[]{INSTANT_TIME, 100, 
dataBlock.getBlockType(), headerStr, footerStr};
+    rows.add(output);
+
+    String expected = HoodiePrintHelper.print(header, new HashMap<>(), "", 
false, -1, false, rows);
+
+    assertEquals(expected, cr.getResult().toString());
+  }
+
+  /**
+   * Test case for 'show logfile records'.
+   */
+  @Test
+  public void testShowLogFileRecords() throws IOException, URISyntaxException {
+    CommandResult cr = getShell().executeCommand("show logfile records 
--logFilePathPattern " + partitionPath + "/*");
+    assertTrue(cr.isSuccess());
+
+    // construct expect result, get 10 records.
+    List<IndexedRecord> records = SchemaTestUtil.generateTestRecords(0, 10);
+    String[][] rows = records.stream().map(r -> new 
String[]{r.toString()}).toArray(String[][]::new);
+    String expected = HoodiePrintHelper.print(new String[] 
{HoodieTableHeaderFields.HEADER_RECORDS}, rows);
+
+    assertEquals(expected, cr.getResult().toString());
+  }
+
+  /**
+   * Test case for 'show logfile records' with merge.
+   */
+  @Test
+  public void testShowLogFileRecordsWithMerge() throws IOException, 
URISyntaxException, InterruptedException {
+    // create commit instant
+    HoodieTestCommitMetadataGenerator.createCommitFile(tablePath, 
INSTANT_TIME, HoodieCLI.conf);
+
+    // write to path '2015/03/16'.
+    Schema schema = HoodieAvroUtils.addMetadataFields(getSimpleSchema());
+    partitionPath = tablePath + File.separator + 
HoodieTestCommitMetadataGenerator.DEFAULT_SECOND_PARTITION_PATH;
+    new File(partitionPath).mkdirs();
+
+    // set little threshold to split file.
+    HoodieLogFormat.Writer writer =
+        HoodieLogFormat.newWriterBuilder().onParentPath(new 
Path(partitionPath))
+            .withFileExtension(HoodieLogFile.DELTA_EXTENSION)
+            
.withFileId("test-log-fileid1").overBaseCommit(INSTANT_TIME).withFs(fs).withSizeThreshold(500).build();
+
+    //write1
+    List<IndexedRecord> records1 = SchemaTestUtil.generateHoodieTestRecords(0, 
100);
+    Map<HoodieLogBlock.HeaderMetadataType, String> header = new HashMap<>();
+    header.put(HoodieLogBlock.HeaderMetadataType.INSTANT_TIME, INSTANT_TIME);
+    header.put(HoodieLogBlock.HeaderMetadataType.SCHEMA, schema.toString());
+    HoodieAvroDataBlock dataBlock = new HoodieAvroDataBlock(records1, header);
+    writer = writer.appendBlock(dataBlock);
+
+    //write2
+    List<IndexedRecord> records2 = SchemaTestUtil.generateHoodieTestRecords(0, 
100);
+    header.put(HoodieLogBlock.HeaderMetadataType.SCHEMA, schema.toString());
+    dataBlock = new HoodieAvroDataBlock(records2, header);
+    writer = writer.appendBlock(dataBlock);
+    writer.close();

Review comment:
       ditto




----------------------------------------------------------------
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]


Reply via email to