Java Apache Beam:未能序列化和反序列化属性';awsCredentialsProvider&x27;

Java Apache Beam:未能序列化和反序列化属性';awsCredentialsProvider&x27;,java,amazon-s3,serialization,deserialization,apache-beam,Java,Amazon S3,Serialization,Deserialization,Apache Beam,我从https://github.com/apache/beam。我正在做一个更改,以便它可以作为接收器写入AWS S3。在我的第一次迭代中,我能够使用以下代码使其工作 public static void main(String[] args) { Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class); options.set

我从
https://github.com/apache/beam
。我正在做一个更改,以便它可以作为接收器写入AWS S3。在我的第一次迭代中,我能够使用以下代码使其工作

    public static void main(String[] args) {
        Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);

        options.setAwsCredentialsProvider(
                new AWSStaticCredentialsProvider(
                        new BasicAWSCredentials(options.getAwsAccessKey().get(), options.getAwsSecretKey().get())));

        runBigQueryTornadoes(options);
    }
    public static void main(String[] args) {
        Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);

        AWSCredentialsProvider provider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(options.getAwsAccessKey().get(), options.getAwsSecretKey().get()));
        AWSSecurityTokenServiceClientBuilder stsBuilder = AWSSecurityTokenServiceClientBuilder.standard().withCredentials(provider);
        AWSSecurityTokenService sts = stsBuilder.build();

        AWSCredentialsProvider credentialsProvider = new STSAssumeRoleSessionCredentialsProvider.Builder(options.getAwsRoleArn().get(), options.getAwsRoleSession().get())
                .withExternalId(options.getAwsExternalId().get())
                .withStsClient(sts)
                .build();
        options.setAwsCredentialsProvider(credentialsProvider);

        runBigQueryTornadoes(options);
    }
在我的第二次迭代中,我希望使用
STSAssumeRoleSessionCredentialsProvider
来支持跨帐户IAM角色。我有以下代码

    public static void main(String[] args) {
        Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);

        options.setAwsCredentialsProvider(
                new AWSStaticCredentialsProvider(
                        new BasicAWSCredentials(options.getAwsAccessKey().get(), options.getAwsSecretKey().get())));

        runBigQueryTornadoes(options);
    }
    public static void main(String[] args) {
        Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);

        AWSCredentialsProvider provider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(options.getAwsAccessKey().get(), options.getAwsSecretKey().get()));
        AWSSecurityTokenServiceClientBuilder stsBuilder = AWSSecurityTokenServiceClientBuilder.standard().withCredentials(provider);
        AWSSecurityTokenService sts = stsBuilder.build();

        AWSCredentialsProvider credentialsProvider = new STSAssumeRoleSessionCredentialsProvider.Builder(options.getAwsRoleArn().get(), options.getAwsRoleSession().get())
                .withExternalId(options.getAwsExternalId().get())
                .withStsClient(sts)
                .build();
        options.setAwsCredentialsProvider(credentialsProvider);

        runBigQueryTornadoes(options);
    }
当我运行上面的代码时,我得到以下异常

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Unexpected IOException (of type java.io.IOException): Failed to serialize and deserialize property 'awsCredentialsProvider' with value 'com.amazonaws.auth.STSAssumeRoleSessionCredentialsProvider@4edb24da'
    at com.fasterxml.jackson.databind.JsonMappingException.fromUnexpectedIOE (JsonMappingException.java:338)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsBytes (ObjectMapper.java:3432)
    at org.apache.beam.runners.direct.DirectRunner.run (DirectRunner.java:163)
    at org.apache.beam.runners.direct.DirectRunner.run (DirectRunner.java:67)
    at org.apache.beam.sdk.Pipeline.run (Pipeline.java:317)
    at org.apache.beam.sdk.Pipeline.run (Pipeline.java:303)
    at org.apache.beam.examples.cookbook.BigQueryTornadoesS3STS.runBigQueryTornadoes (BigQueryTornadoesS3STS.java:251)
    at org.apache.beam.examples.cookbook.BigQueryTornadoesS3STS.main (BigQueryTornadoesS3STS.java:267)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
    at java.lang.Thread.run (Thread.java:748)
我使用以下
mvn
命令运行

mvn compile exec:java -Dexec.mainClass=org.apache.beam.examples.cookbook.BigQueryTornadoesS3STS "-Dexec.args=..." -P direct-runner
我在网上看到了类似的帖子。但我面临的问题是没有将其打包为jar。

这篇文章帮助我使代码正常工作。通过下面的代码,我能够承担跨帐户IAM角色,并写入另一个AWS帐户拥有的S3存储桶

        Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);

        AWSCredentialsProvider provider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(options.getAwsAccessKey().get(), options.getAwsSecretKey().get()));
        AWSSecurityTokenServiceClientBuilder stsBuilder = AWSSecurityTokenServiceClientBuilder.standard().withCredentials(provider);
        AWSSecurityTokenService sts = stsBuilder.build();

        STSAssumeRoleSessionCredentialsProvider credentials = new STSAssumeRoleSessionCredentialsProvider.Builder(options.getAwsRoleArn().get(), options.getAwsRoleSession().get())
                .withExternalId(options.getAwsExternalId().get())
                .withStsClient(sts)
                .build();

        options.setAwsCredentialsProvider(
                new AWSStaticCredentialsProvider(
                        credentials.getCredentials()));

        runBigQueryTornadoes(options);
    }
注意:代码基于
bigQueryTornados
中的示例https://github.com/apache/beam