Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New parquet tools commands #132

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public final class Registry {
registry.put("schema", ShowSchemaCommand.class);
registry.put("meta", ShowMetaCommand.class);
registry.put("dump", DumpCommand.class);
registry.put("rowcount", RowCountCommand.class);
registry.put("size", SizeCommand.class);
}

public static Map<String,Command> allCommands() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright 2013 ARRIS, Inc.
*
* Licensed 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 parquet.tools.command;

import java.io.PrintWriter;
import java.util.List;

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.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.Path;

import parquet.hadoop.Footer;
import parquet.hadoop.ParquetFileReader;
import parquet.hadoop.metadata.BlockMetaData;
import parquet.tools.Main;

public class RowCountCommand extends ArgsOnlyCommand {
private FileStatus[] inputFileStatuses;
private Configuration conf;
private Path inputPath;
private PrintWriter out;
public static final String[] USAGE = new String[] {
"<input>",
"where <input> is the parquet file to count rows to stdout"
};

public static final Options OPTIONS;
static {
OPTIONS = new Options();
Option detailed = OptionBuilder.withLongOpt("detailed")
.withDescription("Detailed rowcount of each matching file")
.create('d');
OPTIONS.addOption(detailed);
}

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

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

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It looks like you're mixing spaces and tabs. The rest of the project uses 2-space indentation, which would really help the readability of this code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All tabs are removed.


String[] args = options.getArgs();
String input = args[0];
out = new PrintWriter(Main.out, true);
inputPath = new Path(input);
conf = new Configuration();
inputFileStatuses = inputPath.getFileSystem(conf).globStatus(inputPath);
long rowCount = 0;

for(FileStatus fs : inputFileStatuses){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should work as the other commands do, without globbing the input path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glob input path removed for rowcount command.

long fileRowCount=0;
for(Footer f : ParquetFileReader.readFooters(conf, fs, false)){
for(BlockMetaData b : f.getParquetMetadata().getBlocks()){
rowCount += b.getRowCount();
fileRowCount += b.getRowCount();
}
}
if(options.hasOption('d')){
out.format("%s row count: %d\n", fs.getPath().getName(), fileRowCount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect --detailed to return the row count for row groups, not for individual files. Maybe it isn't a needed option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just removed --detailed option for rowcount command. Same thing can be done externally so this is not really needed for rowcount

}
}
out.format("Total RowCount: %d", rowCount);
out.println();
}
}
133 changes: 133 additions & 0 deletions parquet-tools/src/main/java/parquet/tools/command/SizeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* Copyright 2013 ARRIS, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use the Apache copyright header? You can copy it from files in other parquet modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rdblue I had used header in other classes in parquet-tool. I have changed headers in my classes. Do you want to fix all other classes to use Apache copyright also? It is my first open source contribution so many common mistakes, won't repeat !!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swapnilushinde, don't worry about it, this is good stuff. There are always things to fix, which is why everything gets reviewed.

There's also no way you would know about the headers. Copying them from the other commands is reasonable, we are just moving to the Apache one so new files should use it. Thanks for offering to update the others, but we are already talking with ARRIS to get that done so we'll handle it separately.

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

import java.io.PrintWriter;
import java.util.List;

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.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.Path;

import parquet.hadoop.Footer;
import parquet.hadoop.ParquetFileReader;
import parquet.hadoop.metadata.BlockMetaData;
import parquet.tools.Main;

public class SizeCommand extends ArgsOnlyCommand {
private FileStatus[] inputFileStatuses;
private Configuration conf;
private Path inputPath;
private PrintWriter out;
public static final String[] USAGE = new String[] {
"<input>",
"where <input> is the parquet file to get size & human readable size to stdout"
};

public static final Options OPTIONS;
static {
OPTIONS = new Options();
Option help = OptionBuilder.withLongOpt("pretty")
.withDescription("Pretty size")
.create('p');
OPTIONS.addOption(help);
Option uncompressed = OptionBuilder.withLongOpt("uncompressed")
.withDescription("Uncompressed size")
.create('u');
OPTIONS.addOption(uncompressed);
Option detailed = OptionBuilder.withLongOpt("detailed")
.withDescription("Detailed size of each matching file")
.create('d');
OPTIONS.addOption(detailed);
}

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

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

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

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

String[] args = options.getArgs();
String input = args[0];
out = new PrintWriter(Main.out, true);
inputPath = new Path(input);
conf = new Configuration();
inputFileStatuses = inputPath.getFileSystem(conf).globStatus(inputPath);
long size = 0;
for(FileStatus fs : inputFileStatuses){
long fileSize = 0;
for(Footer f : ParquetFileReader.readFooters(conf, fs, false)){
for(BlockMetaData b : f.getParquetMetadata().getBlocks()){
size += (options.hasOption('u') ? b.getTotalByteSize() : b.getCompressedSize());
fileSize += (options.hasOption('u') ? b.getTotalByteSize() : b.getCompressedSize());
}
}
if(options.hasOption('d')){
if(options.hasOption('p')){
out.format("%s: %s\n", fs.getPath().getName(), getPrettySize(fileSize));
}
else{
out.format("%s: %d bytes\n", fs.getPath().getName(), fileSize);
}
}
}

if(options.hasOption('p')){
out.format("Total Size: %s", getPrettySize(size));
}
else{
out.format("Total Size: %d bytes", size);
}
out.println();
}

public String getPrettySize(long bytes){
double oneKB = 1024;
double oneMB = oneKB * 1024;
double oneGB = oneMB * 1014;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: should be multiplied by 1024.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rdblue typo is corrected and I have tested edge cases with prettySize method.
size command is still expecting glob path. I feel it is helpful but let me know if you find it otherwise.

double oneTB = oneGB * 1024;
double onePB = oneTB * 1024;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be constants rather than calculated each time the method runs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if (bytes/oneKB < 1){
return String.format("%.3f", bytes) + " bytes";
}
if (bytes/oneMB < 1){
return String.format("%.3f", bytes/oneKB) + " KB";
}
if (bytes/oneGB < 1){
return String.format("%.3f", bytes/oneMB) + " MB";
}
if (bytes/oneTB < 1){
return String.format("%.3f", bytes/oneGB) + " GB";
}
return String.valueOf(bytes/onePB) + " PB";
}
}
25 changes: 25 additions & 0 deletions parquet-tools/src/main/scripts/parquet-rowcount
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
#
# Copyright 2013 ARRIS, Inc.
#
# Licensed 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.
#

# The name of the top-level script
TOPSCRIPT="parquet-tools"

# Determine the path to the script's directory
APPPATH=$( cd "$(dirname "$0")" ; pwd -P )

# Run the application
exec "${APPPATH}/${TOPSCRIPT}" rowcount "$@"
25 changes: 25 additions & 0 deletions parquet-tools/src/main/scripts/parquet-size
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
#
# Copyright 2013 ARRIS, Inc.
#
# Licensed 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.
#

# The name of the top-level script
TOPSCRIPT="parquet-tools"

# Determine the path to the script's directory
APPPATH=$( cd "$(dirname "$0")" ; pwd -P )

# Run the application
exec "${APPPATH}/${TOPSCRIPT}" size "$@"