如何为org.apache.parquet.avro.AvroParquetReader配置S3访问?

2022-05-11 00:00:00 amazon-s3 parquet java

我在这个问题上挣扎了一段时间,想分享我的解决方案。AvroParquetReader是一个很好的阅读Parquet的工具,但它对S3访问的默认设置很弱:

java.io.InterruptedIOException: doesBucketExist on MY_BUCKET: com.amazonaws.AmazonClientException: No AWS Credentials provided by BasicAWSCredentialsProvider EnvironmentVariableCredentialsProvider SharedInstanceProfileCredentialsProvider : com.amazonaws.AmazonClientException: Unable to load credentials from service endpoint

我想使用类似于com.amazonaws.auth.profile.ProfileCredentialsProvider,使用的凭据提供程序,它用于访问我的S3存储桶,但从AvroParquetReader的类定义或文档中不清楚我将如何实现这一点。


解决方案

此代码适用于我。它允许AvroParquetReader使用ProfileCredentialsProvider访问S3。

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import org.apache.parquet.avro.AvroParquetReader;
import org.apache.parquet.hadoop.ParquetReader;
import org.apache.hadoop.fs.Path;
import org.apache.avro.generic.GenericRecord;
import org.apache.hadoop.conf.Configuration;

...

final String path = "s3a://"+bucketName+"/"+pathName;
final Configuration configuration = new Configuration();
configuration.setClass("fs.s3a.aws.credentials.provider", ProfileCredentialsProvider.class,
        AWSCredentialsProvider.class);
ParquetReader<GenericRecord> parquetReader =
        AvroParquetReader.<GenericRecord>builder(new Path(path)).withConf(configuration).build();

相关文章