Add sample app to show MongoDB support

This commit is contained in:
Mahmoud Ben Hassine
2020-01-08 16:15:55 +01:00
parent fef02a6e30
commit 20061007c0
7 changed files with 273 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.sample.mongodb;
import java.util.HashMap;
import java.util.Map;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.data.MongoItemReader;
import org.springframework.batch.item.data.MongoItemWriter;
import org.springframework.batch.item.data.builder.MongoItemReaderBuilder;
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
/**
* This sample job will copy data from collection "person_in" into collection "person_out"
* using {@link MongoItemReader} and {@link MongoItemWriter}.
*
* @author Mahmoud Ben Hassine
*/
@EnableBatchProcessing
public class JobConfiguration {
private JobBuilderFactory jobBuilderFactory;
private StepBuilderFactory stepBuilderFactory;
public JobConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
this.jobBuilderFactory = jobBuilderFactory;
this.stepBuilderFactory = stepBuilderFactory;
}
@Bean
public MongoItemReader<Person> mongoItemReader(MongoTemplate mongoTemplate) {
Map<String, Sort.Direction> sortOptions = new HashMap<>();
sortOptions.put("name", Sort.Direction.DESC);
return new MongoItemReaderBuilder<Person>()
.name("personItemReader")
.collection("person_in")
.targetType(Person.class)
.template(mongoTemplate)
.jsonQuery("{}")
.sorts(sortOptions)
.build();
}
@Bean
public MongoItemWriter<Person> mongoItemWriter(MongoTemplate mongoTemplate) {
return new MongoItemWriterBuilder<Person>()
.template(mongoTemplate)
.collection("person_out")
.build();
}
@Bean
public Step step(MongoItemReader<Person> mongoItemReader, MongoItemWriter<Person> mongoItemWriter) {
return this.stepBuilderFactory.get("step")
.<Person, Person>chunk(2)
.reader(mongoItemReader)
.writer(mongoItemWriter)
.build();
}
@Bean
public Job job(Step step) {
return this.jobBuilderFactory.get("job")
.start(step)
.build();
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.sample.mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.mongodb.core.MongoTemplate;
@Configuration
@PropertySource("classpath:/mongodb-sample.properties")
public class MongoDBConfiguration {
@Value("${mongodb.host}")
private String mongodbHost;
@Value("${mongodb.port}")
private int mongodbPort;
@Value("${mongodb.database}")
private String mongodbDatabase;
@Bean
public MongoTemplate mongoTemplate() {
String connectionString = "mongodb://" +
this.mongodbHost + ":" + this.mongodbPort + "/" + this.mongodbDatabase;
MongoClient mongoClient = MongoClients.create(connectionString);
return new MongoTemplate(mongoClient, "test");
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.sample.mongodb;
import java.util.Arrays;
import java.util.List;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
/**
* Ensure a MongoDB instance is running on "localhost:27017",
* otherwise modify mongodb-sample.properties file as needed.
*
* If you use docker, you can run a mongo db server with:
* "docker run --name mongodb --rm -d -p 27017:27017 mongo"
*
* @author Mahmoud Ben Hassine
*/
public class MongoDBSampleApp {
public static void main(String[] args) throws Exception {
Class<?>[] configurationClasses = {JobConfiguration.class, MongoDBConfiguration.class};
ApplicationContext context = new AnnotationConfigApplicationContext(configurationClasses);
MongoTemplate mongoTemplate = context.getBean(MongoTemplate.class);
// clear collections and insert some documents in "person_in"
MongoCollection<Document> personsIn = mongoTemplate.getCollection("person_in");
MongoCollection<Document> personsOut = mongoTemplate.getCollection("person_out");
personsIn.deleteMany(new Document());
personsOut.deleteMany(new Document());
personsIn.insertMany(Arrays.asList(
new Document("name", "foo1"),
new Document("name", "foo2"),
new Document("name", "foo3"),
new Document("name", "foo4"))
);
// run the job
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
// check results
List<Person> persons = mongoTemplate.findAll(Person.class, "person_out");
System.out.println("Checking persons in person_out collection");
for (Person person : persons) {
System.out.println(person);
}
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.sample.mongodb;
public class Person {
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}

View File

@@ -0,0 +1,3 @@
mongodb.host=127.0.0.1
mongodb.port=27017
mongodb.database=test