MapReduce 中獲取Parquet 格式檔案的schema
阿新 • • 發佈:2019-01-05
在MapReduce中讀取Parquet格式檔案時,預設會自動獲取檔案的Schema,不需要進行額外的設定即可讀取。如果想獲取指定檔案的Schema,應該怎麼做呢?
1、匯入相關的類包
import org.apache.parquet.hadoop.ParquetFileReader; import org.apache.parquet.hadoop.metadata.ParquetMetadata; import org.apache.parquet.schema.MessageType; import org.apache.parquet.schema.Type; import static org.apache.parquet.format.converter.ParquetMetadataConverter.NO_FILTER;
2、在Job、Map或者Reduce中獲取指定檔案的Schema
Configuration conf = getConf(); // 檔案路徑 String filePath = ".../part-r-00002.snappy.parquet"; ParquetMetadata metadata = ParquetFileReader.readFooter(conf, new Path(filePath), NO_FILTER); // 獲取 parquet 格式檔案的全部 schema MessageType schema = metadata.getFileMetaData().getSchema(); // 獲取某個欄位(如url)的 schema 型別 Type type = schema.getType("url");
怎麼樣,是不是很簡單呢!