|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.parquet.cli.commands; |
| 20 | + |
| 21 | +import com.beust.jcommander.Parameter; |
| 22 | +import com.beust.jcommander.Parameters; |
| 23 | +import com.google.common.base.Preconditions; |
| 24 | +import com.google.common.collect.Lists; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.List; |
| 27 | +import org.apache.commons.text.TextStringBuilder; |
| 28 | +import org.apache.parquet.cli.BaseCommand; |
| 29 | +import org.apache.parquet.column.statistics.geometry.GeospatialStatistics; |
| 30 | +import org.apache.parquet.hadoop.ParquetFileReader; |
| 31 | +import org.apache.parquet.hadoop.metadata.BlockMetaData; |
| 32 | +import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData; |
| 33 | +import org.apache.parquet.hadoop.metadata.ParquetMetadata; |
| 34 | +import org.apache.parquet.schema.MessageType; |
| 35 | +import org.slf4j.Logger; |
| 36 | + |
| 37 | +@Parameters(commandDescription = "Print geospatial statistics for a Parquet file") |
| 38 | +public class ShowGeospatialStatisticsCommand extends BaseCommand { |
| 39 | + |
| 40 | + public ShowGeospatialStatisticsCommand(Logger console) { |
| 41 | + super(console); |
| 42 | + } |
| 43 | + |
| 44 | + @Parameter(description = "<parquet path>") |
| 45 | + List<String> targets; |
| 46 | + |
| 47 | + @Override |
| 48 | + @SuppressWarnings("unchecked") |
| 49 | + public int run() throws IOException { |
| 50 | + Preconditions.checkArgument(targets != null && !targets.isEmpty(), "A Parquet file is required."); |
| 51 | + Preconditions.checkArgument(targets.size() == 1, "Cannot process multiple Parquet files."); |
| 52 | + |
| 53 | + String source = targets.get(0); |
| 54 | + try (ParquetFileReader reader = ParquetFileReader.open(getConf(), qualifiedPath(source))) { |
| 55 | + ParquetMetadata footer = reader.getFooter(); |
| 56 | + MessageType schema = footer.getFileMetaData().getSchema(); |
| 57 | + |
| 58 | + console.info("\nFile path: {}", source); |
| 59 | + |
| 60 | + List<BlockMetaData> rowGroups = footer.getBlocks(); |
| 61 | + for (int index = 0, n = rowGroups.size(); index < n; index++) { |
| 62 | + printRowGroupGeospatialStats(console, index, rowGroups.get(index), schema); |
| 63 | + console.info(""); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return 0; |
| 68 | + } |
| 69 | + |
| 70 | + private void printRowGroupGeospatialStats(Logger console, int index, BlockMetaData rowGroup, MessageType schema) { |
| 71 | + int maxColumnWidth = Math.max( |
| 72 | + "column".length(), |
| 73 | + rowGroup.getColumns().stream() |
| 74 | + .map(col -> col.getPath().toString().length()) |
| 75 | + .max(Integer::compare) |
| 76 | + .orElse(0)); |
| 77 | + |
| 78 | + console.info(String.format("\nRow group %d\n%s", index, new TextStringBuilder(80).appendPadding(80, '-'))); |
| 79 | + |
| 80 | + String formatString = String.format("%%-%ds %%-15s %%-40s", maxColumnWidth); |
| 81 | + console.info(String.format(formatString, "column", "bounding box", "geospatial types")); |
| 82 | + |
| 83 | + for (ColumnChunkMetaData column : rowGroup.getColumns()) { |
| 84 | + printColumnGeospatialStats(console, column, schema, maxColumnWidth); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void printColumnGeospatialStats( |
| 89 | + Logger console, ColumnChunkMetaData column, MessageType schema, int columnWidth) { |
| 90 | + GeospatialStatistics stats = column.getGeospatialStatistics(); |
| 91 | + |
| 92 | + if (stats != null && stats.isValid()) { |
| 93 | + String boundingBox = |
| 94 | + stats.getBoundingBox() != null ? stats.getBoundingBox().toString() : "-"; |
| 95 | + String geospatialTypes = stats.getGeospatialTypes() != null |
| 96 | + ? stats.getGeospatialTypes().toString() |
| 97 | + : "-"; |
| 98 | + String formatString = String.format("%%-%ds %%-15s %%-40s", columnWidth); |
| 99 | + console.info(String.format(formatString, column.getPath(), boundingBox, geospatialTypes)); |
| 100 | + } else { |
| 101 | + String formatString = String.format("%%-%ds %%-15s %%-40s", columnWidth); |
| 102 | + console.info(String.format(formatString, column.getPath(), "-", "-")); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public List<String> getExamples() { |
| 108 | + return Lists.newArrayList("# Show geospatial statistics for a Parquet file", "sample.parquet"); |
| 109 | + } |
| 110 | +} |
0 commit comments