/* 
 * 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.tools.command;

import java.io.PrintWriter;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.hadoop.fs.Path;

import org.apache.parquet.hadoop.ParquetReader;
import org.apache.parquet.tools.Main;
import org.apache.parquet.tools.read.SimpleReadSupport;
import org.apache.parquet.tools.read.SimpleRecord;

public class HeadCommand extends ArgsOnlyCommand {
  private static final long DEFAULT = 5;

  public static final String[] USAGE = new String[] {
    "<input>",
    "where <input> is the parquet file to print to stdout"
  };

  public static final Options OPTIONS;
  static {
    OPTIONS = new Options();
    Option help = OptionBuilder.withLongOpt("records")
                               .withDescription("The number of records to show (default: " + DEFAULT + ")")
                               .hasOptionalArg()
                               .create('n');
    OPTIONS.addOption(help);
  }

  public HeadCommand() {
    super(1, 1);
  }

  @Override
  public Options getOptions() {
    return OPTIONS;
  }

  @Override
  public String[] getUsageDescription() {
    return USAGE;
  }

  @Override
  public String getCommandDescription() {
    return "Prints the first n record of the Parquet file";
  }

  @Override
  public void execute(CommandLine options) throws Exception {
    super.execute(options);

    long num = DEFAULT;
    if (options.hasOption('n')) {
      num = Long.parseLong(options.getOptionValue('n'));
    }

    String[] args = options.getArgs();
    String input = args[0];

    ParquetReader<SimpleRecord> reader = null;
    try {
      PrintWriter writer = new PrintWriter(Main.out, true);
      reader = ParquetReader.builder(new SimpleReadSupport(), new Path(input)).build();
      for (SimpleRecord value = reader.read(); value != null && num-- > 0; value = reader.read()) {
        value.prettyPrint(writer);
        writer.println();
      }
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (Exception ex) {
        }
      }
    }
  }
}

@@ -993,23 +993,17 @@
 i.Option
-Builder
+s
 ;%0Aimport
@@ -1014,35 +1014,41 @@
 .apache.
-commons.cli.Op
+hadoop.conf.Configura
 tion
-s
 ;%0Aimport
@@ -1112,24 +1112,28 @@
 doop.Parquet
+File
 Reader;%0Aimpo
@@ -1158,18 +1158,235 @@
 uet.
-tools.Main
+hadoop.ParquetReader;%0Aimport org.apache.parquet.hadoop.metadata.FileMetaData;%0Aimport org.apache.parquet.hadoop.util.HadoopInputFile;%0Aimport org.apache.parquet.tools.Main;%0Aimport org.apache.parquet.tools.json.JsonRecordFormatter
 ;%0Aim
@@ -1635,17 +1635,16 @@
 String%5B%5D
- 
 %7B%0A    %22%3C
@@ -1756,16 +1756,17 @@
 PTIONS;%0A
+%0A
   static
@@ -1825,21 +1825,30 @@
 tion
-B
+.b
 uilder
-.withL
+(%22n%22)%0A      .l
 ongO
@@ -1871,49 +1871,13 @@
     
-                         .withDescription
+.desc
 (%22Th
@@ -1943,93 +1943,192 @@
     
-                         .hasOptionalArg()%0A                               .create('n'
+.optionalArg(true)%0A      .build();%0A%0A    Option json = Option.builder(%22j%22)%0A      .longOpt(%22json%22)%0A      .desc(%22Show records in JSON format.%22)%0A      .build();%0A%0A    OPTIONS.addOption(help
 );%0A 
@@ -2144,28 +2144,28 @@
 S.addOption(
-help
+json
 );%0A  %7D%0A%0A  pu
@@ -2980,102 +2980,539 @@
     
-for (SimpleRecord value = reader.read(); value != null && num-- %3E 0; value = reader.read()) %7B%0A
+ParquetFileReader fileReader = ParquetFileReader.open(HadoopInputFile.fromPath(new Path(input), new Configuration()));%0A      FileMetaData fileMetaData = fileReader.getFooter().getFileMetaData();%0A      JsonRecordFormatter.JsonGroupFormatter jsonFormatter = JsonRecordFormatter.fromSchema(fileMetaData.getSchema());%0A%0A      for (SimpleRecord value = reader.read(); value != null && num-- %3E 0; value = reader.read()) %7B%0A        if (options.hasOption('j')) %7B%0A          writer.write(jsonFormatter.formatRecord(value));%0A        %7D else %7B%0A  
     
@@ -3542,16 +3542,26 @@
 riter);%0A
+        %7D%0A
         

Add JSON support to `parquet-tools head` (#829)