Data Source for GCP Spanner

☆樱花仙子☆ 提交于 2020-01-23 21:42:09

问题


I am going to create batch for GCP spanner am planning to use JdbcCursorItemReader that needs datasource, So i need to create datasource for my GCP Spanner instance, Can you please suggest me on this?


回答1:


You need to add the Cloud Spanner JDBC driver to your build path like this:

    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-spanner-jdbc</artifactId>
      <version>1.9.0</version>
    </dependency>

Then you can define a Spring data source in the normal way. Doing it programmatically would look like this:

    @Bean
    public DataSource spannerDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.google.cloud.spanner.jdbc.JdbcDriver");
        dataSource.setUrl("jdbc:cloudspanner:/projects/<YOUR-PROJECT-ID>/instances/<YOUR-INSTANCE-ID>/databases/<YOUR-DATABASE-ID>?credentials=<PATH-TO-YOUR-SERVICE-CREDENTIALS>");
        return dataSource;
    }

---- Additional information after comment ----

Google Cloud Spanner is not a database that is supported by default by Spring Batch. You therefore need to explicitly set the database type to one of the supported database types. Have a look at this answer to see how that is done.

You need to select one of the supported databases, even though you are using another database. This might cause other compatibility problems, especially if you let Spring Batch automatically generate your data model. If you create the data model by hand and only use Spring Batch for reading data, it should be less of a problem. I would recommend to try to set the database type to POSTGRES and see if it works.



来源:https://stackoverflow.com/questions/58743610/data-source-for-gcp-spanner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!