这个问题是对this one的跟进。 我正在尝试使用apache beam从google spanner表中读取数据(然后进行一些数据处理)。我使用java SDK编写了以下最小示例:
package com.google.cloud.dataflow.examples;
import java.io.IOException;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.PipelineResult;
import org.apache.beam.sdk.io.gcp.spanner.SpannerIO;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.values.PCollection;
import com.google.cloud.spanner.Struct;
public class backup {
public static void main(String[] args) throws IOException {
PipelineOptions options = PipelineOptionsFactory.create();
Pipeline p = Pipeline.create(options);
PCollection<Struct> rows = p.apply(
SpannerIO.read()
.withInstanceId("my_instance")
.withDatabaseId("my_db")
.withQuery("SELECT t.table_name FROM information_schema.tables AS t")
);
PipelineResult result = p.run();
try {
result.waitUntilFinish();
} catch (Exception exc) {
result.cancel();
}
}
}
当我尝试使用DirectRunner执行代码时,我得到了 以下错误消息:
org.apache.beam.runners.direct.repackaged.com.google.common.util.concurrent.UncheckedExecutionException:
org.apache.beam.sdk.util.UserCodeException: java.lang.NoClassDefFoundError:无法初始化类 com.google.cloud.spanner.spi.v1.SpannerErrorInterceptor
[...]引起: org.apache.beam.sdk.util.UserCodeException: java.lang.NoClassDefFoundError:无法初始化类 com.google.cloud.spanner.spi.v1.SpannerErrorInterceptor
[...]引起: java.lang.NoClassDefFoundError:无法初始化类 com.google.cloud.spanner.spi.v1.SpannerErrorInterceptor
或者,使用DataflowRunner:
org.apache.beam.runners.direct.repackaged.com.google.common.util.concurrent.UncheckedExecutionException: org.apache.beam.sdk.util.UserCodeException: java.lang.NoSuchFieldError的: internal_static_google_rpc_LocalizedMessage_fieldAccessorTable
[...]引起:org.apache.beam.sdk.util.UserCodeException: java.lang.NoSuchFieldError的: internal_static_google_rpc_LocalizedMessage_fieldAccessorTable
[...]引起:java.lang.NoSuchFieldError: internal_static_google_rpc_LocalizedMessage_fieldAccessorTable
在这两种情况下,错误信息都相当神秘,我找不到任何明确的想法,因为谷歌搜索会导致错误。我也找不到使用SpannerIO模块的任何示例脚本。
这个错误是由于我的代码中的明显错误造成的,还是由于Google云工具安装不当造成的?
答案 0 :(得分:5)
此问题很可能是由此处描述的依赖项兼容性问题引起的:BEAM-2837。以下是JIRA问题中其中一条评论中描述的快速解决方法:
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>grpc-google-common-protos</artifactId>
<version>0.1.9</version>
</dependency>
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-io-google-cloud-platform</artifactId>
<version>${beam.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.api.grpc</groupId>
<artifactId>grpc-google-common-protos</artifactId>
</exclusion>
</exclusions>
</dependency>
明确定义所需的com.google.api.grpc
依赖关系,并从org.apache.beam
中排除该版本。
答案 1 :(得分:1)
您需要指定ProjectID:
SpannerIO.read()
.withProjectId("my_project")
.withInstanceId("my_instance")
.withDatabaseId("my_db")
您需要为Spanner项目设置凭据。由于SpannerIO的API不允许您设置任何自定义凭据,因此必须使用环境变量GOOGLE_APPLICATION_CREDENTIALS设置全局应用程序凭据。
您还可以使用JDBC读取(并写入)Cloud Spanner。阅读是这样完成的:
PCollection<KV<String, Long>> words = p2.apply(JdbcIO.<KV<String, Long>> read()
.withDataSourceConfiguration(JdbcIO.DataSourceConfiguration.create("nl.topicus.jdbc.CloudSpannerDriver",
"jdbc:cloudspanner://localhost;Project=my-project-id;Instance=instance-id;Database=database;PvtKeyPath=C:\\Users\\MyUserName\\Documents\\CloudSpannerKeys\\cloudspanner-key.json"))
.withQuery("SELECT t.table_name FROM information_schema.tables AS t").withCoder(KvCoder.of(StringUtf8Coder.of(), BigEndianLongCoder.of()))
.withRowMapper(new JdbcIO.RowMapper<KV<String, Long>>()
{
private static final long serialVersionUID = 1L;
@Override
public KV<String, Long> mapRow(ResultSet resultSet) throws Exception
{
return KV.of(resultSet.getString(1), resultSet.getLong(2));
}
}));
此方法还允许您通过设置PvtKeyPath来使用自定义凭据。您也可以使用JDBC写入Google Cloud Spanner。请看这里的示例:http://www.googlecloudspanner.com/2017/10/google-cloud-spanner-with-apache-beam.html