0%

常用工具类

hadoop 常用操作工具类

ParquetUtil

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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
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.parquet.example.data.Group;
import org.apache.parquet.format.converter.ParquetMetadataConverter;
import org.apache.parquet.hadoop.ParquetFileReader;
import org.apache.parquet.hadoop.ParquetReader;
import org.apache.parquet.hadoop.example.GroupReadSupport;
import org.apache.parquet.hadoop.metadata.BlockMetaData;
import org.apache.parquet.hadoop.metadata.ParquetMetadata;
import org.apache.parquet.io.api.Binary;
import org.apache.parquet.schema.OriginalType;
import org.apache.parquet.schema.PrimitiveType;
import org.apache.parquet.schema.Type;
import org.apache.spark.sql.catalyst.util.DateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author longzl
*/
public class ParquetUtil {

private static Logger logger = LoggerFactory.getLogger(ParquetUtil.class);

public final static String STRING_TYPE = "string";
public final static String LONG_TYPE = "long";
public final static String FLOAT_TYPE = "float";
public final static String INTEGER_TYPE = "integer";
public final static String BOOLEAN_TYPE = "boolean";
public final static String DOUBLE_TYPE = "double";

public final static String BYTE_TYPE = "byte";
public final static String SHORT_TYPE = "short";
public final static String DATE_TYPE = "date";
public final static String DECIMAL_TYPE = "decimal";
public final static String TIMESTAMP_TYPE = "timestamp";
public final static String BINARY_TYPE = "binary";

public static String convertParquetType2Normal(PrimitiveType.PrimitiveTypeName parquetType) {
switch (parquetType) {
case INT64:
return LONG_TYPE;
case BINARY:
return STRING_TYPE;
case FLOAT:
return FLOAT_TYPE;
case INT32:
return INTEGER_TYPE;
case INT96:
return STRING_TYPE;
case BOOLEAN:
return BOOLEAN_TYPE;
case DOUBLE:
return DOUBLE_TYPE;
default:
throw new InvalidRequestParamException("parquetType not supported");
}
}

public static String convertPrimitiveFiled(PrimitiveType fieldType) {
PrimitiveType.PrimitiveTypeName typeName = fieldType.getPrimitiveTypeName();
OriginalType originalType = fieldType.getOriginalType();

switch (typeName) {
case BOOLEAN: {
return BOOLEAN_TYPE;
}
case FLOAT: {
return FLOAT_TYPE;
}

case DOUBLE: {
return DOUBLE_TYPE;
}

case INT32: {
if (originalType == null) {
return INTEGER_TYPE;
}

switch (originalType) {
case INT_8:
return BYTE_TYPE;
case INT_16:
return SHORT_TYPE;
case INT_32:
return INTEGER_TYPE;
case DATE:
return DATE_TYPE;
default:
throw new InvalidRequestParamException("parquetType not supported");
}
}
case INT64: {
if (originalType == null) {
return LONG_TYPE;
}
switch (originalType) {
case INT_64:
return LONG_TYPE;
case DECIMAL:
return DECIMAL_TYPE;
default:
throw new InvalidRequestParamException("parquetType not supported");
}
}
case INT96: {
return TIMESTAMP_TYPE;
}
case BINARY: {
if (originalType == null) {
return STRING_TYPE;
}
switch (originalType) {
case UTF8:
case ENUM:
case JSON:
return STRING_TYPE;
case BSON:
return BINARY_TYPE;
case DECIMAL:
return DECIMAL_TYPE;
default:
throw new InvalidRequestParamException("parquetType not supported");
}
}
case FIXED_LEN_BYTE_ARRAY: {
if (originalType == null) {
throw new InvalidRequestParamException("parquetType not supported");
}
switch (originalType) {
case DECIMAL:
return DECIMAL_TYPE;
default:
throw new InvalidRequestParamException("parquetType not supported");
}
}

default: {
throw new InvalidRequestParamException("parquetType not supported");
}
}
}


/**
* 获取parquet文件夹的内容
*
* @param parentPath 文件夹
* @param config
* @param count 获取行数
* @return
* @throws IOException
*/
public static List<List<Object>> headRecord(String parentPath, Configuration config, Integer count) throws
IOException {
FileSystem fs = null;
Integer rowCount = 0;
List<List<Object>> records = new ArrayList<>();
try {
fs = FileSystem.get(config);
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path(parentPath), true);

while (listFiles.hasNext() && rowCount < count) {
LocatedFileStatus fileStatus = listFiles.next();
if (fileStatus.isFile()
&& !fileStatus.getPath().toString().toUpperCase().endsWith("_SUCCESS")
) {

ParquetReader reader = null;
try {
Path path = fileStatus.getPath();
reader = ParquetReader.builder(new GroupReadSupport(), path).withConf(config).build();
for (Group line = (Group) reader.read();
line != null && rowCount++ < count;
line = (Group) reader.read()) {
records.add(getRecordOneLine(line));
}
} catch (Exception e) {
logger.warn(e.getMessage());
continue;
} finally {
if (reader != null) {
reader.close();
}
}
}
}

} finally {
if (fs != null) {
fs.close();
}
}
return records;
}

/**
* @param parentPath 文件夹
* @param config
* @return
* @throws IOException
*/
public static List<Field> getSchema(String parentPath, Configuration config) throws IOException {
FileSystem fs = null;

try {
fs = FileSystem.get(config);
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path(parentPath), true);

while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
if (fileStatus.isFile()
&& !fileStatus.getPath().toString().toUpperCase().endsWith("_SUCCESS")
) {

try {
return getSchema(fileStatus.getPath(), config);
} catch (Exception e) {
logger.warn(e.getMessage());
continue;
}
}
}

} finally {
if (fs != null) {
fs.close();
}
}

return new ArrayList<>();
}


/**
* 获取某一paruqet 文件路径,获取的parquet文件
*
* @param parentPath 文件夹
* @param configuration
* @return
* @throws IOException
*/
@Deprecated
public static Path getOneParquetPath(String parentPath, Configuration configuration) throws IOException {
Path oneParquet = null;
Path maxCountPath = null;
long maxCount = 0;
FileSystem fs = null;
Long rowCount = 0L;
try {
fs = FileSystem.get(configuration);
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path(parentPath), true);

while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
if (fileStatus.isFile()
&& !fileStatus.getPath().toString().toUpperCase().endsWith("_SUCCESS")) {

rowCount = 0L;

ParquetMetadata parquetMetadata;
try {
parquetMetadata = ParquetFileReader.readFooter(configuration, fileStatus,
ParquetMetadataConverter.NO_FILTER);
} catch (RuntimeException e) {
logger.warn(e.getMessage());
continue;
}

// 统计parquet文件 行数,若文件行数大于30 则结束循环
for (BlockMetaData blockMetaData : parquetMetadata.getBlocks()) {
rowCount += blockMetaData.getRowCount();
}

if (rowCount > 30) {
oneParquet = fileStatus.getPath();
break;
} else {
if (maxCount < rowCount) {
maxCount = rowCount;
maxCountPath = fileStatus.getPath();
}
}
}
}

if (rowCount <= 30) {
oneParquet = maxCountPath;
}
} finally {
if (fs != null) {
fs.close();
}
}
return oneParquet;
}

/**
* @param parentPath 文件夹
* @param configuration
* @return
* @throws IOException
*/
public static List<Path> getParquetPathList(String parentPath, Configuration configuration) throws IOException {
FileSystem fs = null;
List<Path> pathList = new ArrayList<>();

try {
fs = FileSystem.get(configuration);
new Path(parentPath);
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path(parentPath), true);
while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
if (fileStatus.isFile()
&& !fileStatus.getPath().toString().toLowerCase().endsWith("_SUCCESS")) {
try {
ParquetFileReader.readFooter(configuration, fileStatus, ParquetMetadataConverter.NO_FILTER);
pathList.add(fileStatus.getPath());
} catch (RuntimeException e) {
if (!e.getMessage().contains("is not a Parquet file")) {
throw e;
}
}
}
}
} finally {
if (fs != null) {
fs.close();
}
}
return pathList;
}

/**
* 统计 文件的大小、列数和行数
*
* @param tablePath
* @return
*/
public static Map<String, Long> statisticsParquet(String tablePath, Configuration conf) {
logger.debug("统计表信息={} start", tablePath);
Map<String, Long> map = new HashMap<>(3);
Long rows = 0L;
Long columns = 0L;
Long fileSize = 0L;

FileSystem fs = null;

try {
fs = FileSystem.get(conf);
Path parquetFile;
boolean isFirst = true;

RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path(tablePath), true);
while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
if (fileStatus.isFile()
&& !fileStatus.getPath().toString().toUpperCase().endsWith("_SUCCESS")) {

parquetFile = fileStatus.getPath();
fileSize += fileStatus.getLen();

ParquetFileReader parquetFileReader = null;
try {
parquetFileReader = new ParquetFileReader(conf, parquetFile, ParquetMetadataConverter.NO_FILTER);
rows += parquetFileReader.getRecordCount();

if (isFirst) {
columns = (long) parquetFileReader.getFileMetaData().getSchema().getFieldCount();
isFirst = false;
}
} catch (IOException e) {
logger.warn(e.getMessage());
continue;
} finally {
if (parquetFileReader != null) {
parquetFileReader.close();
}
}

}
}

} catch (IOException e) {
logger.warn(e.getMessage());
map.put("rows", -1L);
map.put("columns", -1L);
map.put("fileSize", -1L);
} finally {
if (fs != null) {
try {
fs.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}

map.put("rows", rows);
map.put("columns", columns);
map.put("fileSize", fileSize);
logger.debug("统计表信息={} end", tablePath);
return map;
}

/**
* 获取文件 schema 信息
*
* @param parquetPath 单个文件
* @param config
* @return
*/
public static List<Field> getSchema(Path parquetPath, Configuration config) throws IOException {
List<Field> fields = new ArrayList<>();
ParquetMetadata parquetMetadata = ParquetFileReader.readFooter(config, parquetPath, ParquetMetadataConverter
.NO_FILTER);
for (Type type : parquetMetadata.getFileMetaData().getSchema().getFields()) {
if (!type.isPrimitive()) {
throw new InternalServerException("不支持 非 Primitive 类型");
}
String javaType = convertPrimitiveFiled(type.asPrimitiveType());

fields.add(new Field(type.getName(), javaType));
}

return fields;
}


/**
* 获取 parquet 的记录
*
* @param parquetPath 单个文件
* @param config
* @param number
* @return
* @throws IOException
*/
@Deprecated
public static List<List<Object>> headRecord(Path parquetPath, Configuration config, Integer number) throws
IOException {
if (number > 1000) {
throw new InternalServerException("读取行数超过1000,容易内存溢出");
}
List<List<Object>> records = new ArrayList<>();
ParquetReader reader = null;
try {
reader = ParquetReader.builder(new GroupReadSupport(), parquetPath).withConf(config).build();
for (Group line = (Group) reader.read();
line != null && number-- > 0L;
line = (Group) reader.read()) {
records.add(getRecordOneLine(line));
}
} catch (IOException e) {
if (reader != null) {
reader.close();
}
} finally {
if (reader != null) {
reader.close();
}
}
return records;
}

private static List<Object> getRecordOneLine(Group line) {
List<Object> objects = new ArrayList<>();
for (Type type : line.getType().getFields()) {
objects.add(getValue(type, line));
}
return objects;
}


public static Object getValue(Type type, Group line) {
Object o;
if (!type.isPrimitive()) {
throw new InternalServerException("不支持 非 Primitive 类型");
}
int fieldIndex = line.getType().getFieldIndex(type.getName());
int index = 0;
String javaType = convertPrimitiveFiled(type.asPrimitiveType());
try {
switch (javaType) {
case STRING_TYPE: {
o = (line.getString(fieldIndex, index));
break;
}
case INTEGER_TYPE: {
o = (line.getInteger(fieldIndex, index));
break;
}
case LONG_TYPE: {
o = (line.getLong(fieldIndex, index));
break;
}
case DOUBLE_TYPE: {
o = (line.getDouble(fieldIndex, index));
break;
}
case FLOAT_TYPE: {
o = (line.getFloat(fieldIndex, index));
break;
}
case BOOLEAN_TYPE: {
o = (line.getBoolean(fieldIndex, index));
break;
}
case BINARY_TYPE: {
o = (line.getBinary(fieldIndex, index));
break;
}
case TIMESTAMP_TYPE: {
Binary int96 = line.getInt96(fieldIndex, index);
ByteBuffer order = int96.toByteBuffer().order(ByteOrder.LITTLE_ENDIAN);
Long timeOfDayNanos = order.getLong();
Integer julianDay = order.getInt();
Long timeStamp = DateTimeUtils.fromJulianDay(julianDay, timeOfDayNanos);
o = DateTimeUtils.timestampToString(timeStamp);
break;
}
case DATE_TYPE: {
int integer = line.getInteger(fieldIndex, index);
o = DateTimeUtils.dateToString(integer);
break;
}
//todo DECIMAL_TYPE
//case DECIMAL_TYPE: {
// Binary binary = line.getBinary(fieldIndex, index);
//}
default:
throw new InternalServerException("暂不支持:" + javaType);
}
} catch (RuntimeException e) {
if (e.getMessage().contains("element number " + index + " in group")) {
o = null;
} else {
throw e;
}
}

return o;
}


}