Java Code Examples for org.springframework.batch.item.database.JdbcBatchItemWriter

The following examples show how to use org.springframework.batch.item.database.JdbcBatchItemWriter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: building-microservices   Source File: BatchConfiguration.java    License: Apache License 2.0 7 votes vote down vote up
@Bean
Job personEtl(JobBuilderFactory jobBuilderFactory,
        StepBuilderFactory stepBuilderFactory,
        FlatFileItemReader<Person> reader,
        JdbcBatchItemWriter<Person> writer
) {

    Step step = stepBuilderFactory.get("file-to-database")
            .<Person, Person>chunk(5)
            .reader(reader)
            .writer(writer)
            .build();

    return jobBuilderFactory.get("etl")
            .start(step)
            .build();
}
 
Example 2
Source Project: SpringAll   Source File: DatabaseItemWriterDemo.java    License: MIT License 6 votes vote down vote up
private ItemWriter<TestData> dataSourceItemWriter() {
    // ItemWriter的实现类之一,mysql数据库数据写入使用JdbcBatchItemWriter,
    // 其他实现:MongoItemWriter,Neo4jItemWriter等
    JdbcBatchItemWriter<TestData> writer = new JdbcBatchItemWriter<>();
    writer.setDataSource(dataSource); // 设置数据源

    String sql = "insert into TEST(id,field1,field2,field3) values (:id,:field1,:field2,:field3)";
    writer.setSql(sql); // 设置插入sql脚本

    // 映射TestData对象属性到占位符中的属性
    BeanPropertyItemSqlParameterSourceProvider<TestData> provider = new BeanPropertyItemSqlParameterSourceProvider<>();
    writer.setItemSqlParameterSourceProvider(provider);

    writer.afterPropertiesSet(); // 设置一些额外属性
    return writer;
}
 
Example 3
@Bean
public JdbcBatchItemWriter<Voltage> writer(final DataSource dataSource) {
    return new JdbcBatchItemWriterBuilder<Voltage>()
            .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
            .sql("INSERT INTO voltage (volt, time) VALUES (:volt, :time)")
            .dataSource(dataSource)
            .build();
}
 
Example 4
@Bean
public Step step1(JdbcBatchItemWriter<Voltage> writer) {
    return stepBuilderFactory.get("step1")
            .<Voltage, Voltage> chunk(10)
            .reader(reader())
            .processor(processor())
            .writer(writer)
            .build();
}
 
Example 5
Source Project: spring-cloud   Source File: PeopleWriter.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
@Qualifier("jdbcBatchItemWriter")
public JdbcBatchItemWriter<People> jdbcBatchItemWriter() {
    JdbcBatchItemWriter<People> writer = new JdbcBatchItemWriter<>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    writer.setSql("insert into people (person_id, first_name, last_name) VALUES (uuid(), :firstName, :lastName)");
    writer.setDataSource(w_dataSource);
    return writer;
}
 
Example 6
@Bean(name = DATA_WRITER)
public JdbcBatchItemWriter<JavaChampion> writer(DataSource dataSource) throws Exception {
    JdbcBatchItemWriter<JavaChampion> jdbcBatchItemWriter = new JdbcBatchItemWriter<JavaChampion>();
    jdbcBatchItemWriter.setAssertUpdates(true);
    jdbcBatchItemWriter.setDataSource(dataSource);
    jdbcBatchItemWriter.setSql(
            " INSERT INTO java_champion( first_name, last_name, country, year )" +
                    " VALUES ( :firstName , :lastName, :country, :year ) ");
    jdbcBatchItemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    return jdbcBatchItemWriter;
}
 
Example 7
@Bean
public JdbcBatchItemWriter<PayrollTo> writer(DataSource dataSource) {
    return new JdbcBatchItemWriterBuilder<PayrollTo>()
            .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
            .sql("INSERT INTO PAYROLL (PERSON_IDENTIFICATION, CURRENCY, TX_AMMOUNT, ACCOUNT_TYPE, ACCOUNT_ID, TX_DESCRIPTION, FIRST_LAST_NAME) VALUES (:identification,:currency,:ammount,:accountType, :accountNumber, :description, :firstLastName)")
            .dataSource(dataSource)
            .build();
}
 
Example 8
@Bean
public Step step1(JdbcBatchItemWriter<PayrollTo> writer) {
    return stepBuilderFactory.get("step1")
            .<PayrollTo, PayrollTo> chunk(10)
            .reader(reader())
            .processor(processor())
            .writer(writer)
            .build();
}
 
Example 9
Source Project: CogStack-Pipeline   Source File: JobConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
@StepScope
@Qualifier("simpleJdbcItemWriter")
@Profile("jdbc_out")
public ItemWriter<Document> simpleJdbcItemWriter(
        @Qualifier("targetDataSource") DataSource jdbcDocumentTarget) {
    JdbcBatchItemWriter<Document> writer = new JdbcBatchItemWriter<>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    writer.setSql(env.getRequiredProperty("target.Sql"));
    writer.setDataSource(jdbcDocumentTarget);
    return writer;
}
 
Example 10
Source Project: CogStack-Pipeline   Source File: JobConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
@StepScope
@Qualifier("mapJdbcItemWriter")
@Profile("jdbc_out_map")
public ItemWriter<Document> mapJdbcItemWriter(
        @Qualifier("targetDataSource") DataSource jdbcDocumentTarget) {
    JdbcBatchItemWriter<Document> writer = new JdbcBatchItemWriter<>();
    writer.setItemSqlParameterSourceProvider(new MapItemSqlParameterSourceProvider<Document>());
    writer.setSql(env.getRequiredProperty("target.Sql"));
    writer.setDataSource(jdbcDocumentTarget);
    return writer;
}
 
Example 11
Source Project: messaging   Source File: BatchConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
JdbcBatchItemWriter<Contact> jdbcWriter(DataSource dataSource) {
 return new JdbcBatchItemWriterBuilder<Contact>()
  .dataSource(dataSource)
  .beanMapped()
  .sql(
   "insert into CONTACT( full_name, email, valid_email ) values ( :fullName, :email, :validEmail )")
  .build();
}
 
Example 12
Source Project: building-microservices   Source File: BatchConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
JdbcBatchItemWriter<Person> jdbcBatchItemWriter(DataSource h2) {
    JdbcBatchItemWriter<Person> w = new JdbcBatchItemWriter<>();
    w.setDataSource(h2);
    w.setSql("insert into PEOPLE( first, last, email) values ( :first, :last, :email )");
    w.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    return w;
}
 
Example 13
@Bean
public JdbcBatchItemWriter<BatchTaskAddress> addressWriter(final DataSource dataSource) {
    final JdbcBatchItemWriter<BatchTaskAddress> writer = new JdbcBatchItemWriter<>();
    writer.setDataSource(dataSource);
    writer.setSql(UPDATE_ADDRESS_STATEMENT);
    writer.setItemSqlParameterSourceProvider(item -> {
        final MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource();
        sqlParameterSource.addValue("processing_state", item.getProcessingState());
        sqlParameterSource.addValue("batch_task_id", item.getBatchTaskId());
        return sqlParameterSource;
    });
    writer.afterPropertiesSet();
    return writer;
}
 
Example 14
@Bean
public JdbcBatchItemWriter<Long> addressBatchTaskDeletionWriter(final DataSource dataSource) {
    final JdbcBatchItemWriter<Long> writer = new JdbcBatchItemWriter<>();
    writer.setDataSource(dataSource);
    writer.setSql(DELETE_BATCH_TASK_ADDRESS_STATEMENT);
    writer.setItemSqlParameterSourceProvider(batchTaskId -> {
        final MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();
        mapSqlParameterSource.addValue("batch_task_id", batchTaskId);
        return mapSqlParameterSource;
    });
    return writer;
}
 
Example 15
@Bean
public JdbcBatchItemWriter<Item> jdbcBatchItemWriter() {
	JdbcBatchItemWriter<Item> itemWriter = new JdbcBatchItemWriter<Item>();
	itemWriter.setSql(
			"INSERT INTO ITEM (ID, DESCRIPTION, FIRST_ACTION, SECOND_ACTION) VALUES (:id,:description,:firstAction,:secondAction)");
	itemWriter.setDataSource(dataSource);
	itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Item>());
	return itemWriter;
}
 
Example 16
@Bean
public JdbcBatchItemWriter<Item> jdbcBatchItemWriter() {
	JdbcBatchItemWriter<Item> itemWriter = new JdbcBatchItemWriter<Item>();
	itemWriter.setSql("INSERT INTO ITEM (ID, DESCRIPTION) VALUES (:id,:description)");
	itemWriter.setDataSource(dataSource);
	itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Item>());
	return itemWriter;
}
 
Example 17
@Bean
public JdbcBatchItemWriter<Item> jdbcBatchItemWriter() {
	JdbcBatchItemWriter<Item> itemWriter = new JdbcBatchItemWriter<Item>();
	itemWriter.setSql("INSERT INTO ITEM (ID, DESCRIPTION) VALUES (:id,:description)");
	itemWriter.setDataSource(dataSource);
	itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Item>());
	return itemWriter;
}
 
Example 18
@Bean
public JdbcBatchItemWriter<Item> jdbcBatchItemWriter() {
	JdbcBatchItemWriter<Item> itemWriter = new JdbcBatchItemWriter<Item>();
	itemWriter.setSql("INSERT INTO ITEM (ID, DESCRIPTION) VALUES (:id,:description)");
	itemWriter.setDataSource(dataSource);
	itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Item>());
	return itemWriter;
}