parquet文件的一些操作

/**
* 获取schema信息
* @throws IOException
*/
@Test
public void testGetSchema() throws IOException {
Configuration configuration = new Configuration(true);
configuration.set("fs.defaultFS","hdfs://10.0.1.xx:9000");
ParquetMetadata readFooter = null;
Path parquetFilePath = new Path("/user/yanglei/parquet/douban.parquet");
readFooter = ParquetFileReader.readFooter(configuration, parquetFilePath, ParquetMetadataConverter.NO_FILTER);
MessageType schema =readFooter.getFileMetaData().getSchema();
System.out.println(schema.toString());
}

public static final MessageType FILE_SCHEMA = Types.buildMessage()
.required(PrimitiveType.PrimitiveTypeName.BINARY).named("user_name")
.required(PrimitiveType.PrimitiveTypeName.INT64).named("bookid")
.required(PrimitiveType.PrimitiveTypeName.INT32).named("bookscore")
.named("douban");

/**
* 写parquet文件
* @throws Exception
*/
@Test
public void testWriteParquet() throws Exception {
Configuration conf = new Configuration(true);
conf.set("fs.defaultFS","hdfs://10.0.1.xx:9000");

String file = "/user/yanglei/parquet/douban.parquet";
Path path = new Path(file);
FileSystem fs = path.getFileSystem(conf);
if (fs.exists(path)) {
fs.delete(path, true);
}
GroupWriteSupport.setSchema(FILE_SCHEMA, conf);
SimpleGroupFactory f = new SimpleGroupFactory(FILE_SCHEMA);
ParquetWriter<Group> writer = new ParquetWriter<>(path, new GroupWriteSupport(),
CompressionCodecName.GZIP, 1024, 1024, 512, true, false, ParquetProperties.WriterVersion.PARQUET_2_0, conf);

for (int i = 0; i < 1000; i++) {

writer.write(
f.newGroup()
.append("user_name", String.valueOf(i))
.append("bookid", 64l)
.append("bookscore",i));
}
writer.close();
}

转载于:https://www.cnblogs.com/ylcoder/p/6417744.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值