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

The following examples show how to use org.springframework.batch.item.database.JdbcBatchItemWriter. 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 File: BatchConfiguration.java    From building-microservices with 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 File: DatabaseItemWriterDemo.java    From SpringAll with 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
Source File: BatchConfiguration.java    From messaging with 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 #4
Source File: FlatFileToDbSkipJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@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 #5
Source File: FlatFileToDbSkipProcessorNonTransactionalJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@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 #6
Source File: FlatFileToDbSkipReaderTransactionalJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@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 #7
Source File: FlatFileToDbNoSkipJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@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 #8
Source File: AddressPrinterJobConfiguration.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
@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 #9
Source File: AddressMigrationJobConfiguration.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
@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 #10
Source File: BatchConfiguration.java    From building-microservices with 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 #11
Source File: BatchConfiguration.java    From batch-processing-large-datasets-spring with MIT License 5 votes vote down vote up
@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 #12
Source File: JobConfiguration.java    From CogStack-Pipeline with 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 #13
Source File: JobConfiguration.java    From CogStack-Pipeline with 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 #14
Source File: BatchConfig.java    From Software-Architecture-with-Spring-5.0 with MIT License 5 votes vote down vote up
@Bean
public Step step1(JdbcBatchItemWriter<PayrollTo> writer) {
    return stepBuilderFactory.get("step1")
            .<PayrollTo, PayrollTo> chunk(10)
            .reader(reader())
            .processor(processor())
            .writer(writer)
            .build();
}
 
Example #15
Source File: BatchConfig.java    From Software-Architecture-with-Spring-5.0 with MIT License 5 votes vote down vote up
@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 #16
Source File: BatchConfiguration.java    From Software-Architecture-with-Spring-5.0 with MIT License 5 votes vote down vote up
@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 #17
Source File: PeopleWriter.java    From spring-cloud with 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 #18
Source File: BatchConfiguration.java    From batch-processing-large-datasets-spring with MIT License 5 votes vote down vote up
@Bean
public Step step1(JdbcBatchItemWriter<Voltage> writer) {
    return stepBuilderFactory.get("step1")
            .<Voltage, Voltage> chunk(10)
            .reader(reader())
            .processor(processor())
            .writer(writer)
            .build();
}