SpringBatch从网络数据库中读取

huangapple go评论76阅读模式
英文:

SpringBatch Read from Networked Database

问题

我有一个Spring Batch应用程序,我将在其中连接到一个网络数据库进行读取。我找到的大多数示例都将数据库嵌入用于独立测试。我不想这样做。我不确定配置是否会相似。以下是我拥有的最相关部分(部分已进行模糊处理):

@Configuration
@EnableBatchProcessing
public class BatchConfig {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    private static final String GET_DATA =
        "SELECT " +
        "stuffA, " +
        "stuffB, " +
        "FROM STUFF_TABLE " +
        "ORDER BY stuffA ASC";

    @Bean
    public ItemReader<StuffDto> itemReader(DataSource dataSource) {
        return new JdbcCursorItemReaderBuilder<StuffDto>()
            .name("cursorItemReader")
            .dataSource(dataSource)
            .sql(GET_DATA)
            .rowMapper(new BeanPropertyRowMapper<>(StuffDto.class))
            .build();
    }

    // ...
}

然后从应用程序启动它:

@SpringBootApplication
@EnableBatchProcessing
public class BatchApplication {

    public static void main(String[] args) {
        SpringApplication.run(BatchApplication.class, args);
    }
}

我没有配置数据源。我不确定,因为我的实际上只是与远程网络数据库的客户端连接,所以它是否会与我找到的各种示例的嵌入式设置有所不同。所以,我自然会遇到错误(如下所示)。我只是在寻找一个简单的配置,以实例化作为连接到远程数据库的数据源。

谢谢任何回应。

(错误信息省略)

英文:

I have a springbatch application where I will be connecting to a networked database for reading. Most of the examples I find have the database embedded for the purposes of testing stand-alone. I don't want to do that. I am not certain if the configuration would be similar or not. Here are the most germaine portions of what I have (with some obfiscation):

@Configuration
@EnableBatchProcessing
public class BatchConfig {


    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;


    private static final String GET_DATA =
        &quot;SELECT &quot; +
                &quot;stuffA, &quot; +
                &quot;stuffB, &quot; +
                &quot;FROM STUFF_TABLE &quot; +
                &quot;ORDER BY stuffA ASC&quot;;

    @Bean
    public ItemReader&lt;StuffDto&gt; itemReader(DataSource dataSource) {
        return new JdbcCursorItemReaderBuilder&lt;StuffDto&gt;()
            .name(&quot;cursorItemReader&quot;)
            .dataSource(dataSource)
            .sql(GET_DATA)
            .rowMapper(new BeanPropertyRowMapper&lt;&gt;(StuffDto.class))
            .build();
}

....

, and then starting it up from application:

@SpringBootApplication
@EnableBatchProcessing
public class BatchApplication {

    public static void main(String[] args) {
        SpringApplication.run(BatchApplication.class, args);
    }
}

I don't have the datasource configured. I am not sure if, since mine is essentially just a client connection to a remote, networked database, it would configured differently than if I have the embedded set up of the various examples I find. So naturally I get error (below). I am simply looking for a simple configuration to instantiate datasource that is a connection to my remote database.

Thanks for any response

.
.
.

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-08-21 13:18:09.773 ERROR 1220 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Parameter 0 of method itemReader in java.main.configuration.configuration.BatchConfig required a bean of type 'javax.sql.DataSource' that could not be found.

The following candidates were found but could not be injected:
- Bean method 'dataSource' in 'JndiDataSourceAutoConfiguration' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' in 'XADataSourceAutoConfiguration' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'

Action:

Consider revisiting the entries above or defining a bean of type 'javax.sql.DataSource' in your configuration.

答案1

得分: 0

一个 javax.sql.DataSource 是对远程/本地/嵌入式数据源的抽象。因此,这是一个配置问题,您可以将JDBC连接字符串设置为远程数据库主机。以下是远程MySQL数据源的示例:

@Bean
public DataSource dataSource() {
    MysqlDataSource datasource = new MysqlDataSource();
    // 配置值可以从属性/环境变量中注入
    datasource.setURL("jdbc:mysql://REMOTE_HOST:PORT/schema");
    datasource.setUser("username");
    datasource.setPassword("password");
    return datasource;
}

Spring Batch 使用 DataSource 对象,不知道(也不应该知道)它是本地的、远程的还是嵌入式的。

英文:

A javax.sql.DataSource is an abstraction over a remote/local/embedded data source. So it's a matter of configuration where you can set the JDBC connection string to a remote database host. Here is an example for a remote MySQL datasource:

@Bean
public DataSource dataSource() {
    MysqlDataSource datasource = new MysqlDataSource();
    // config values could be injected from properties/environment variables
    datasource.setURL(&quot;jdbc:mysql://REMOTE_HOST:PORT/schema&quot;);
    datasource.setUser(&quot;username&quot;);
    datasource.setPassword(&quot;passord&quot;);
    return datasource;
}

Spring Batch works with a DataSource object and does not (and should not) know if it's local, remote or embedded.

huangapple
  • 本文由 发表于 2020年8月22日 01:59:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63527873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定