尝试使用JsonCredentials从Apache Beam查询BigTable的NotSerializableException

时间:2017-06-22 13:01:13

标签: apache-beam google-cloud-bigtable

我正在尝试使用以下方式连接到Google BigTable:

 BigtableOptions.Builder bigTableOptions = new BigtableOptions.Builder()
            .setProjectId(options.getProjectId())
            .setInstanceId(options.getInstanceId())
            .setCredentialOptions(
                    CredentialOptions.jsonCredentials(
                                    new FileInputStream(systemResource.toFile())
                    ));

但是,我得到了:

Caused by: java.io.NotSerializableException: java.io.FileInputStream
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)

我试过了  2.1.0-SNAPSHOT 和  2.0.0。

还有其他人遇到过这个问题吗?

感谢。

1 个答案:

答案 0 :(得分:0)

我最终这样做了:

// Load the keyfile
final String keyFile = "service-account-keyfile.json"
final Path path = Paths.get(ClassLoader.getSystemResource(keyFile).getPath());
// Load and Convert key to a CredentialOptions object
final GoogleCredentials credentials = GoogleCredentials.fromStream(Files.newInputStream(path))
                .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
final CredentialOptions bigTableCredentials = CredentialOptions.credential(credentials);

//Use the credentials
final BigtableOptions bigtableOptions =
            new BigtableOptions.Builder()
                    .setCredentialOptions(bigTableCredentials)