You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@parquet.apache.org by GitBox <gi...@apache.org> on 2021/02/10 11:36:47 UTC

[GitHub] [parquet-mr] gszadovszky opened a new pull request #867: PARQUET-1978: Provide a tool to show the complete footer

gszadovszky opened a new pull request #867:
URL: https://github.com/apache/parquet-mr/pull/867


   Make sure you have checked _all_ steps below.
   
   ### Jira
   
   - [ ] My PR addresses the following [Parquet Jira](https://issues.apache.org/jira/browse/PARQUET/) issues and references them in the PR title. For example, "PARQUET-1234: My Parquet PR"
     - https://issues.apache.org/jira/browse/PARQUET-XXX
     - In case you are adding a dependency, check if the license complies with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   
   ### Tests
   
   - [ ] My PR adds the following unit tests __OR__ does not need testing for this extremely good reason:
   
   ### Commits
   
   - [ ] My commits all reference Jira issues in their subject lines. In addition, my commits follow the guidelines from "[How to write a good git commit message](http://chris.beams.io/posts/git-commit/)":
     1. Subject is separated from body by a blank line
     1. Subject is limited to 50 characters (not including Jira issue reference)
     1. Subject does not end with a period
     1. Subject uses the imperative mood ("add", not "adding")
     1. Body wraps at 72 characters
     1. Body explains "what" and "why", not "how"
   
   ### Documentation
   
   - [ ] In case of new functionality, my PR adds documentation that describes how to use it.
     - All the public functions and the classes in the PR contain Javadoc that explain what it does
   


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



[GitHub] [parquet-mr] ggershinsky commented on a change in pull request #867: PARQUET-1978: Provide a tool to show the complete footer

Posted by GitBox <gi...@apache.org>.
ggershinsky commented on a change in pull request #867:
URL: https://github.com/apache/parquet-mr/pull/867#discussion_r573915631



##########
File path: parquet-cli/src/main/java/org/apache/parquet/cli/commands/ShowFooterCommand.java
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.parquet.cli.commands;
+
+import static org.apache.parquet.bytes.BytesUtils.readIntLittleEndian;
+import static org.apache.parquet.hadoop.ParquetFileWriter.EFMAGIC;
+import static org.apache.parquet.hadoop.ParquetFileWriter.MAGIC;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.parquet.bytes.ByteBufferInputStream;
+import org.apache.parquet.cli.BaseCommand;
+import org.apache.parquet.format.CliUtils;
+import org.apache.parquet.format.Util;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.metadata.ParquetMetadata;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.io.InputFile;
+import org.apache.parquet.io.SeekableInputStream;
+import org.slf4j.Logger;
+
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.Parameters;
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+@Parameters(commandDescription = "Print the Parquet file footer in json format")
+public class ShowFooterCommand extends BaseCommand {
+
+  public ShowFooterCommand(Logger console) {
+    super(console);
+  }
+
+  @Parameter(description = "<parquet path>", required = true)
+  String target;
+
+  @Parameter(names = { "-r", "--raw" }, description = "Print the raw thrift object of the footer")
+  boolean raw = false;
+
+  @Override
+  public int run() throws IOException {
+    InputFile inputFile = HadoopInputFile.fromPath(qualifiedPath(target), getConf());
+
+    String json;
+    if (raw) {
+      json = readRawFooter(inputFile);
+    } else {
+      json = readFooter(inputFile);
+    }
+    console.info(json);
+
+    return 0;
+  }
+
+  private String readFooter(InputFile inputFile) throws JsonProcessingException, IOException {
+    String json;
+    try (ParquetFileReader reader = ParquetFileReader.open(inputFile)) {
+      ParquetMetadata footer = reader.getFooter();
+      ObjectMapper mapper = createObjectMapper();
+      mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
+      mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
+      json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(footer);
+    }
+    return json;
+  }
+
+  private ObjectMapper createObjectMapper() {
+    ObjectMapper mapper = new ObjectMapper();
+    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+    mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
+    return mapper;
+  }
+
+  private String readRawFooter(InputFile file) throws IOException {
+    long fileLen = file.getLength();
+
+    int FOOTER_LENGTH_SIZE = 4;
+    if (fileLen < MAGIC.length + FOOTER_LENGTH_SIZE + MAGIC.length) { // MAGIC + data + footer + footerIndex + MAGIC
+      throw new RuntimeException("Not a Parquet file (length is too low: " + fileLen + ")");
+    }
+
+    try (SeekableInputStream f = file.newStream()) {
+      // Read footer length and magic string - with a single seek
+      byte[] magic = new byte[MAGIC.length];
+      long fileMetadataLengthIndex = fileLen - magic.length - FOOTER_LENGTH_SIZE;
+      f.seek(fileMetadataLengthIndex);
+      int fileMetadataLength = readIntLittleEndian(f);
+      f.readFully(magic);
+
+      if (Arrays.equals(EFMAGIC, magic)) {
+        throw new RuntimeException("Encrypted Parquet files are not supported.");

Review comment:
       technically, EFMAGIC is for encrypted files with encrypted footer. Encrypted files can also have the regular MAGIC, if created in plaintext footer mode.




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



[GitHub] [parquet-mr] gszadovszky merged pull request #867: PARQUET-1978: Provide a tool to show the complete footer

Posted by GitBox <gi...@apache.org>.
gszadovszky merged pull request #867:
URL: https://github.com/apache/parquet-mr/pull/867


   


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



[GitHub] [parquet-mr] gszadovszky commented on a change in pull request #867: PARQUET-1978: Provide a tool to show the complete footer

Posted by GitBox <gi...@apache.org>.
gszadovszky commented on a change in pull request #867:
URL: https://github.com/apache/parquet-mr/pull/867#discussion_r595298980



##########
File path: parquet-cli/src/main/java/org/apache/parquet/cli/commands/ShowFooterCommand.java
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.parquet.cli.commands;
+
+import static org.apache.parquet.bytes.BytesUtils.readIntLittleEndian;
+import static org.apache.parquet.hadoop.ParquetFileWriter.EFMAGIC;
+import static org.apache.parquet.hadoop.ParquetFileWriter.MAGIC;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.parquet.bytes.ByteBufferInputStream;
+import org.apache.parquet.cli.BaseCommand;
+import org.apache.parquet.format.CliUtils;
+import org.apache.parquet.format.Util;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.metadata.ParquetMetadata;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.io.InputFile;
+import org.apache.parquet.io.SeekableInputStream;
+import org.slf4j.Logger;
+
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.Parameters;
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+@Parameters(commandDescription = "Print the Parquet file footer in json format")
+public class ShowFooterCommand extends BaseCommand {
+
+  public ShowFooterCommand(Logger console) {
+    super(console);
+  }
+
+  @Parameter(description = "<parquet path>", required = true)
+  String target;
+
+  @Parameter(names = { "-r", "--raw" }, description = "Print the raw thrift object of the footer")
+  boolean raw = false;
+
+  @Override
+  public int run() throws IOException {
+    InputFile inputFile = HadoopInputFile.fromPath(qualifiedPath(target), getConf());
+
+    String json;
+    if (raw) {
+      json = readRawFooter(inputFile);
+    } else {
+      json = readFooter(inputFile);
+    }
+    console.info(json);
+
+    return 0;
+  }
+
+  private String readFooter(InputFile inputFile) throws JsonProcessingException, IOException {
+    String json;
+    try (ParquetFileReader reader = ParquetFileReader.open(inputFile)) {
+      ParquetMetadata footer = reader.getFooter();
+      ObjectMapper mapper = createObjectMapper();
+      mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
+      mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
+      json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(footer);
+    }
+    return json;
+  }
+
+  private ObjectMapper createObjectMapper() {
+    ObjectMapper mapper = new ObjectMapper();
+    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+    mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
+    return mapper;
+  }
+
+  private String readRawFooter(InputFile file) throws IOException {
+    long fileLen = file.getLength();
+
+    int FOOTER_LENGTH_SIZE = 4;
+    if (fileLen < MAGIC.length + FOOTER_LENGTH_SIZE + MAGIC.length) { // MAGIC + data + footer + footerIndex + MAGIC
+      throw new RuntimeException("Not a Parquet file (length is too low: " + fileLen + ")");
+    }
+
+    try (SeekableInputStream f = file.newStream()) {
+      // Read footer length and magic string - with a single seek
+      byte[] magic = new byte[MAGIC.length];
+      long fileMetadataLengthIndex = fileLen - magic.length - FOOTER_LENGTH_SIZE;
+      f.seek(fileMetadataLengthIndex);
+      int fileMetadataLength = readIntLittleEndian(f);
+      f.readFully(magic);
+
+      if (Arrays.equals(EFMAGIC, magic)) {
+        throw new RuntimeException("Encrypted Parquet files are not supported.");

Review comment:
       @shangxinli, just checked it offline. It works fine. (The unit test I've added lately does not contain encrypted file because it would require much more efforts and I think it does not worth it.)




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



[GitHub] [parquet-mr] shangxinli commented on a change in pull request #867: PARQUET-1978: Provide a tool to show the complete footer

Posted by GitBox <gi...@apache.org>.
shangxinli commented on a change in pull request #867:
URL: https://github.com/apache/parquet-mr/pull/867#discussion_r595268064



##########
File path: parquet-cli/src/main/java/org/apache/parquet/cli/commands/ShowFooterCommand.java
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.parquet.cli.commands;
+
+import static org.apache.parquet.bytes.BytesUtils.readIntLittleEndian;
+import static org.apache.parquet.hadoop.ParquetFileWriter.EFMAGIC;
+import static org.apache.parquet.hadoop.ParquetFileWriter.MAGIC;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.parquet.bytes.ByteBufferInputStream;
+import org.apache.parquet.cli.BaseCommand;
+import org.apache.parquet.format.CliUtils;
+import org.apache.parquet.format.Util;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.metadata.ParquetMetadata;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.io.InputFile;
+import org.apache.parquet.io.SeekableInputStream;
+import org.slf4j.Logger;
+
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.Parameters;
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+@Parameters(commandDescription = "Print the Parquet file footer in json format")
+public class ShowFooterCommand extends BaseCommand {
+
+  public ShowFooterCommand(Logger console) {
+    super(console);
+  }
+
+  @Parameter(description = "<parquet path>", required = true)
+  String target;
+
+  @Parameter(names = { "-r", "--raw" }, description = "Print the raw thrift object of the footer")
+  boolean raw = false;
+
+  @Override
+  public int run() throws IOException {
+    InputFile inputFile = HadoopInputFile.fromPath(qualifiedPath(target), getConf());
+
+    String json;
+    if (raw) {
+      json = readRawFooter(inputFile);
+    } else {
+      json = readFooter(inputFile);
+    }
+    console.info(json);
+
+    return 0;
+  }
+
+  private String readFooter(InputFile inputFile) throws JsonProcessingException, IOException {
+    String json;
+    try (ParquetFileReader reader = ParquetFileReader.open(inputFile)) {
+      ParquetMetadata footer = reader.getFooter();
+      ObjectMapper mapper = createObjectMapper();
+      mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
+      mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
+      json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(footer);
+    }
+    return json;
+  }
+
+  private ObjectMapper createObjectMapper() {
+    ObjectMapper mapper = new ObjectMapper();
+    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+    mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
+    return mapper;
+  }
+
+  private String readRawFooter(InputFile file) throws IOException {
+    long fileLen = file.getLength();
+
+    int FOOTER_LENGTH_SIZE = 4;
+    if (fileLen < MAGIC.length + FOOTER_LENGTH_SIZE + MAGIC.length) { // MAGIC + data + footer + footerIndex + MAGIC
+      throw new RuntimeException("Not a Parquet file (length is too low: " + fileLen + ")");
+    }
+
+    try (SeekableInputStream f = file.newStream()) {
+      // Read footer length and magic string - with a single seek
+      byte[] magic = new byte[MAGIC.length];
+      long fileMetadataLengthIndex = fileLen - magic.length - FOOTER_LENGTH_SIZE;
+      f.seek(fileMetadataLengthIndex);
+      int fileMetadataLength = readIntLittleEndian(f);
+      f.readFully(magic);
+
+      if (Arrays.equals(EFMAGIC, magic)) {
+        throw new RuntimeException("Encrypted Parquet files are not supported.");

Review comment:
       @gszadovszky Did you try the plaintext footer encrypted file to see if this change work? 

##########
File path: parquet-cli/src/main/java/org/apache/parquet/cli/commands/ShowFooterCommand.java
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.parquet.cli.commands;
+
+import static org.apache.parquet.bytes.BytesUtils.readIntLittleEndian;
+import static org.apache.parquet.hadoop.ParquetFileWriter.EFMAGIC;
+import static org.apache.parquet.hadoop.ParquetFileWriter.MAGIC;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.parquet.bytes.ByteBufferInputStream;
+import org.apache.parquet.cli.BaseCommand;
+import org.apache.parquet.format.CliUtils;
+import org.apache.parquet.format.Util;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.metadata.ParquetMetadata;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.io.InputFile;
+import org.apache.parquet.io.SeekableInputStream;
+import org.slf4j.Logger;
+
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.Parameters;
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+@Parameters(commandDescription = "Print the Parquet file footer in json format")
+public class ShowFooterCommand extends BaseCommand {
+
+  public ShowFooterCommand(Logger console) {
+    super(console);
+  }
+
+  @Parameter(description = "<parquet path>", required = true)
+  String target;
+
+  @Parameter(names = { "-r", "--raw" }, description = "Print the raw thrift object of the footer")
+  boolean raw = false;
+
+  @Override
+  public int run() throws IOException {
+    InputFile inputFile = HadoopInputFile.fromPath(qualifiedPath(target), getConf());
+
+    String json;

Review comment:
       Can we do 'String json = raw ? readRawFooter(inputFile) : readFooter(inputFile);' ? It would save couple of lines code. 




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



[GitHub] [parquet-mr] shangxinli commented on pull request #867: PARQUET-1978: Provide a tool to show the complete footer

Posted by GitBox <gi...@apache.org>.
shangxinli commented on pull request #867:
URL: https://github.com/apache/parquet-mr/pull/867#issuecomment-801578095


   LGTM


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



[GitHub] [parquet-mr] gszadovszky commented on a change in pull request #867: PARQUET-1978: Provide a tool to show the complete footer

Posted by GitBox <gi...@apache.org>.
gszadovszky commented on a change in pull request #867:
URL: https://github.com/apache/parquet-mr/pull/867#discussion_r576060940



##########
File path: parquet-cli/src/main/java/org/apache/parquet/cli/commands/ShowFooterCommand.java
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.parquet.cli.commands;
+
+import static org.apache.parquet.bytes.BytesUtils.readIntLittleEndian;
+import static org.apache.parquet.hadoop.ParquetFileWriter.EFMAGIC;
+import static org.apache.parquet.hadoop.ParquetFileWriter.MAGIC;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.parquet.bytes.ByteBufferInputStream;
+import org.apache.parquet.cli.BaseCommand;
+import org.apache.parquet.format.CliUtils;
+import org.apache.parquet.format.Util;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.metadata.ParquetMetadata;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.io.InputFile;
+import org.apache.parquet.io.SeekableInputStream;
+import org.slf4j.Logger;
+
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.Parameters;
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+@Parameters(commandDescription = "Print the Parquet file footer in json format")
+public class ShowFooterCommand extends BaseCommand {
+
+  public ShowFooterCommand(Logger console) {
+    super(console);
+  }
+
+  @Parameter(description = "<parquet path>", required = true)
+  String target;
+
+  @Parameter(names = { "-r", "--raw" }, description = "Print the raw thrift object of the footer")
+  boolean raw = false;
+
+  @Override
+  public int run() throws IOException {
+    InputFile inputFile = HadoopInputFile.fromPath(qualifiedPath(target), getConf());
+
+    String json;
+    if (raw) {
+      json = readRawFooter(inputFile);
+    } else {
+      json = readFooter(inputFile);
+    }
+    console.info(json);
+
+    return 0;
+  }
+
+  private String readFooter(InputFile inputFile) throws JsonProcessingException, IOException {
+    String json;
+    try (ParquetFileReader reader = ParquetFileReader.open(inputFile)) {
+      ParquetMetadata footer = reader.getFooter();
+      ObjectMapper mapper = createObjectMapper();
+      mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
+      mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
+      json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(footer);
+    }
+    return json;
+  }
+
+  private ObjectMapper createObjectMapper() {
+    ObjectMapper mapper = new ObjectMapper();
+    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+    mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
+    return mapper;
+  }
+
+  private String readRawFooter(InputFile file) throws IOException {
+    long fileLen = file.getLength();
+
+    int FOOTER_LENGTH_SIZE = 4;
+    if (fileLen < MAGIC.length + FOOTER_LENGTH_SIZE + MAGIC.length) { // MAGIC + data + footer + footerIndex + MAGIC
+      throw new RuntimeException("Not a Parquet file (length is too low: " + fileLen + ")");
+    }
+
+    try (SeekableInputStream f = file.newStream()) {
+      // Read footer length and magic string - with a single seek
+      byte[] magic = new byte[MAGIC.length];
+      long fileMetadataLengthIndex = fileLen - magic.length - FOOTER_LENGTH_SIZE;
+      f.seek(fileMetadataLengthIndex);
+      int fileMetadataLength = readIntLittleEndian(f);
+      f.readFully(magic);
+
+      if (Arrays.equals(EFMAGIC, magic)) {
+        throw new RuntimeException("Encrypted Parquet files are not supported.");

Review comment:
       Thanks, @ggershinsky for the correction. Updated the message.




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



[GitHub] [parquet-mr] gszadovszky commented on pull request #867: PARQUET-1978: Provide a tool to show the complete footer

Posted by GitBox <gi...@apache.org>.
gszadovszky commented on pull request #867:
URL: https://github.com/apache/parquet-mr/pull/867#issuecomment-779924667


   @shangxinli, however this one is certainly not a blocker for the release it might make debugging easier. If you have some time, please check. If you don't, it's fine and I'll push the release forward without this one.


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