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

How do I configure S3 access for org.apache.parquet.avro.AvroParquetReader?

我为此苦苦挣扎了一段时间,想分享我的解决方案。 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 的 class 定义或文档中不清楚我将如何实现这一点.

这段代码对我有用。它允许 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();

对于其他遇到此问题的人,我发现@jd_free 的回答对我不起作用。为了使它起作用,我唯一需要更改的是传递给 AvroParquetReader 的关于所用 AWSCredentialsProvider 类型的配置设置:

Configuration configuration = new Configuration();
        configuration.set("fs.s3a.aws.credentials.provider", "org.apache.hadoop.fs.s3a.SimpleAWSCredentialsProvider");
        configuration.set("fs.s3a.access.key", "KEY");
        configuration.set("fs.s3a.secret.key", "KEY");`

问题是给定的凭据提供者,以及给配置的方式。有关不同凭证提供程序的更多信息,您可以查看 this page。它解释了可用于不同场景的不同类型,包括如何从环境变量中获取凭据。