-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathColumnSizes.java
More file actions
245 lines (227 loc) · 8.05 KB
/
ColumnSizes.java
File metadata and controls
245 lines (227 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* 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.orc.tools;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.orc.ColumnStatistics;
import org.apache.orc.OrcFile;
import org.apache.orc.Reader;
import org.apache.orc.StripeInformation;
import org.apache.orc.TypeDescription;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
/**
* Given a set of paths, finds all of the "*.orc" files under them and
* prints the sizes of each column, both as a percentage and the number of
* bytes per a row.
*/
public class ColumnSizes {
final Configuration conf;
final TypeDescription schema;
final long[] columnSizes;
int goodFiles = 0;
long rows = 0;
long padding = 0;
long totalSize = 0;
long stripeFooterSize = 0;
long fileFooterSize = 0;
long stripeIndex = 0;
// data bytes that aren't assigned to a specific column
long stripeData = 0;
public ColumnSizes(Configuration conf,
LocatedFileStatus file) throws IOException {
this.conf = conf;
try (Reader reader = OrcFile.createReader(file.getPath(),
OrcFile.readerOptions(conf))) {
this.schema = reader.getSchema();
columnSizes = new long[schema.getMaximumId() + 1];
addReader(file, reader);
}
}
private void checkStripes(LocatedFileStatus file,
Reader reader) {
// Count the magic as file overhead
long offset = OrcFile.MAGIC.length();
fileFooterSize += offset;
for (StripeInformation stripe: reader.getStripes()) {
padding += stripe.getOffset() - offset;
stripeIndex += stripe.getIndexLength();
stripeData += stripe.getDataLength();
stripeFooterSize += stripe.getFooterLength();
offset = stripe.getOffset() + stripe.getLength();
}
// Add everything else as the file footer
fileFooterSize += file.getLen() - offset;
}
private boolean addReader(LocatedFileStatus file,
Reader reader) {
// Validate that the schemas are the same
TypeDescription newSchema = reader.getSchema();
if (schema.equals(newSchema)) {
goodFiles += 1;
rows += reader.getNumberOfRows();
totalSize += file.getLen();
checkStripes(file, reader);
ColumnStatistics[] colStats = reader.getStatistics();
for (int c = 0; c < colStats.length && c < columnSizes.length; c++) {
columnSizes[c] += colStats[c].getBytesOnDisk();
// Don't double count. Either count the bytes as stripe data or as
// part of a column.
stripeData -= colStats[c].getBytesOnDisk();
}
} else {
System.err.println("Ignoring " + file.getPath()
+ " because of schema mismatch: " + newSchema);
return false;
}
return true;
}
public boolean addFile(LocatedFileStatus file) throws IOException {
try (Reader reader = OrcFile.createReader(file.getPath(),
OrcFile.readerOptions(conf))) {
return addReader(file, reader);
}
}
private static class StringLongPair {
final String name;
final long size;
StringLongPair(String name, long size) {
this.name = name;
this.size = size;
}
}
private void printResults(PrintStream out, boolean summary) {
List<StringLongPair> sizes = new ArrayList<>(columnSizes.length + 5);
for(int column = 0; column < columnSizes.length; ++column) {
if (columnSizes[column] > 0) {
sizes.add(new StringLongPair(
schema.findSubtype(column).getFullFieldName(),
columnSizes[column]));
}
}
if (padding > 0) {
sizes.add(new StringLongPair("_padding", padding));
}
if (stripeFooterSize > 0) {
sizes.add(new StringLongPair("_stripe_footer", stripeFooterSize));
}
if (fileFooterSize > 0) {
sizes.add(new StringLongPair("_file_footer", fileFooterSize));
}
if (stripeIndex > 0) {
sizes.add(new StringLongPair("_index", stripeIndex));
}
if (stripeData > 0) {
sizes.add(new StringLongPair("_data", stripeData));
}
// sort by descending size, ascending name
sizes.sort((x, y) -> x.size != y.size ?
Long.compare(y.size, x.size) : x.name.compareTo(y.name));
if (summary) {
out.printf("Total Sizes: %d%n", totalSize);
out.printf("Total Rows: %d%n", rows);
}
out.println("Percent Bytes/Row Name");
for (StringLongPair item: sizes) {
out.println(String.format(" %-5.2f %-9.2f %s",
100.0 * item.size / totalSize, (double) item.size / rows, item.name));
}
}
public static void main(Configuration conf, String[] args) throws Exception {
Options opts = createOptions();
CommandLine cli = new DefaultParser().parse(opts, args);
if (cli.hasOption('h')) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("sizes", opts);
return;
}
boolean ignoreExtension = cli.hasOption("ignoreExtension");
boolean summary = cli.hasOption("summary");
String[] files = cli.getArgs();
ColumnSizes result = null;
int totalFiles = 0;
int badFiles = 0;
for(String root: files) {
Path rootPath = new Path(root);
FileSystem fs = rootPath.getFileSystem(conf);
for(RemoteIterator<LocatedFileStatus> itr = fs.listFiles(rootPath, true); itr.hasNext(); ) {
LocatedFileStatus status = itr.next();
if (status.isFile() && (ignoreExtension || status.getPath().getName().endsWith(".orc"))) {
totalFiles += 1;
try {
if (result == null) {
result = new ColumnSizes(conf, status);
} else {
if (!result.addFile(status)) {
badFiles += 1;
}
}
} catch (IOException err) {
badFiles += 1;
System.err.println("Failed to read " + status.getPath());
}
}
}
}
if (result == null) {
System.err.println("No files found");
} else {
if (summary) {
System.out.printf("Total Files: %d%n", totalFiles);
}
result.printResults(System.out, summary);
}
if (badFiles > 0) {
System.err.println(badFiles + " bad ORC files found.");
System.exit(1);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
if (Runtime.version().feature() > 21) {
conf.setIfUnset("fs.file.impl.disable.cache", "true");
}
main(conf, args);
}
private static Options createOptions() {
Options result = new Options();
result.addOption(Option.builder("i")
.longOpt("ignoreExtension")
.desc("Ignore ORC file extension")
.build());
result.addOption(Option.builder("s")
.longOpt("summary")
.desc("Summarize the number of files, file sizes, and file rows")
.build());
result.addOption(Option.builder("h")
.longOpt("help")
.desc("Print help message")
.build());
return result;
}
}