Spring Batch Couchbase JPA
In this time, I explain about Spring Batch + Couchbase + JPA
- Spring Boot 2.2.2
- Use yaml file for Couchbase setting
Prepare couchbase bucket
Create couchbase bucket using console
And prepare user same name as bucket (Security tab)
Bucket name : newot
User name : newot
Password : xxxxxx


build.gradle
buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.2.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group 'com.daiji110.sample.couchbase' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() maven { url "https://plugins.gradle.org/m2/" } } dependencies { compile("org.springframework.boot:spring-boot-starter-batch") compile("org.hsqldb:hsqldb") compile 'org.springframework.boot:spring-boot-starter-data-couchbase' compileOnly 'org.projectlombok:lombok:1.18.12' annotationProcessor 'org.projectlombok:lombok:1.18.12' testCompile group: 'junit', name: 'junit', version: '4.12' }
spring-boot-starter-batch, org.springframework.boot:spring-boot-starter-data-couchbase are key for Spring Batch and Couchbase
application.yml
Let’s prepare Couchbase configuration using property file
spring: couchbase: bootstrap-hosts: localhost bucket: name: newot password: xxxxxx data: couchbase: auto-index: true
Simple. auto-index is to create Couchbase Index when creating entity
BatchConfig
@Configuration @EnableBatchProcessing @EnableAutoConfiguration public class BatchConfig { Logger logger = LoggerFactory.getLogger(BatchConfig.class); @Autowired private JobBuilderFactory jobBuilderFactory; @Autowired private StepBuilderFactory stepBuilderFactory; @Bean public Step step1(SimpleTask simpleTask) { return stepBuilderFactory.get("step1") .tasklet(simpleTask) .build(); } @Bean public Job job(Step step1) throws Exception { return jobBuilderFactory.get("job1") .incrementer(new RunIdIncrementer()) .start(step1) .build(); } }
SuperPoint.java
This is model class to use as Document
@EqualsAndHashCode @AllArgsConstructor @NoArgsConstructor @Builder @Data public class SuperPoint { @Id @GeneratedValue(strategy = UNIQUE) private String id; private String member; private Integer point; }
Repository
To manupulate data, repository is simple for JPA. (This is same as MySQL, Mongodb etc…)
@N1qlPrimaryIndexed @ViewIndexed(designDoc = "superpoint") public interface SuperPointRepository extends CouchbasePagingAndSortingRepository<SuperPoint, String> { List<SuperPoint> findByMember(String member); @Query("SELECT COUNT(*) as count FROM #{#n1ql.bucket} WHERE #{#n1ql.filter}") long countSuperPoint(); }
I prepare 2 customs findBy<Columnname> and Count
SimpleTask.java
This is Actual Taklet
Get Repository and try to insert data and retrieve data and use custom queries.
@Component public class SimpleTask implements Tasklet { @Autowired private SuperPointRepository superPointRepository; @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { // create SuperPoint superPoint1 = SuperPoint.builder() .point(1) .member("123456").build(); superPointRepository.save(superPoint1); // Custom find List<SuperPoint> list = superPointRepository.findByMember("123456"); System.out.println(list.size()); // Custom count long cnt = superPointRepository.countSuperPoint(); System.out.println(cnt); } }
コメント