一、parquet文件
Parquet是Apache Hadoop生态系统的一种免费的开源面向列的数据存储格式。 类似于Hadoop中可用的其他列存储文件格式,如RCFile格式和ORC格式。Parquet有高效编码压缩及更少IO,性能优势明显。
Parquet文件是由一个header以及一个或多个block块组成,以一个footer结尾。header中只包含一个4个字节的数字PAR1用来识别整个Parquet文件格式。数据存在block中,block包括行组等。metadata存在于footer中,包含了版本信息,schema、key-value paris以及所有block中的metadata信息。
二、java解析
1.读取数据
hdfs上的parquet文件,读取文件中每个行组中的数据
private void readParquet(String filePath) throws IOException{
Path file = new Path(filePath);
ParquetReader.Builder<Group> builder = ParquetReader.builder(new GroupReadSupport(),file);
ParquetReader<Group> reader = builder.build();
SimpleGroup group =null;
GroupType groupType = null;
int count =0;
while((group = (SimpleGroup)reader.read())!=null){
if(groupType == null){
groupType = group.getType();
}
if(count <100){
Map<String,Object> resultMap = new LinkedHashMap<>();
for(int i=0;i<groupType.getFieldCount();i++){
String tmpName = groupType.getFieldName(i);
try{
String tmp =group.getValueToString(i,0);
System.out.println(tmpName+":"+tmp);
}catch (Exception e){
System.out.println(tmpName+":"+"");
}
}
count++;
}
}
}
2.读取列名及类型
获取footer中的metadata ,获取schema解析
private void readPartForSchema(String filePath, FileSystem fs)throws Exception{
Path file = new Path(filePath);
ParquetMetadata metadata = ParquetFileReader.readFooter(HdfsUtil.hdfsConfigInstance(),file,NO_FILTER);
MessageType schema = metadata.getFileMetaData().getSchema();
//遍历字段
for(Type field:schema.getFields()){
Map<String,Object> map = new LinkedHashMap<>();
//获取基本类型字段名称
String typeName = field.asPrimitiveType().getPrimitiveTypeName().name();
OriginalType originalType = field.asPrimitiveType().getOriginalType();
String originalName = originalType !=null? originalType.name():"";
System.out.println(field.getName()+":"+typeName+":"+originalName);
}
}
参考:
[1] https://www.cnblogs.com/panpanwelcome/p/10250172.html
[2] https://blog.csdn.net/yu616568/article/details/50993491
[3] https://zhuanlan.zhihu.com/p/111822325
[4] https://blog.csdn.net/qq_41946557/article/details/103075802
[5] https://zhuanlan.zhihu.com/p/356667093
[6] https://www.cnblogs.com/tonglin0325/p/10244676.html
[7] https://blog.csdn.net/zeng6325998/article/details/106425965
[8] https://parquet.apache.org/documentation/latest/