This extension contains an ItemReader
and ItemWriter
implementations for Neo4j.
The Neo4jItemReader
can be configured as follows:
Neo4jItemReader<User> reader = new Neo4jItemReaderBuilder<User>()
.neo4jTemplate(neo4jTemplate)
.name("userReader")
.statement(Cypher.match(userNode).returning(userNode))
.targetType(User.class)
.pageSize(50)
.build();
The Neo4jItemWriter
can be configured as follows:
Neo4jItemWriter<User> writer = new Neo4jItemWriterBuilder<User>()
.neo4jTemplate(neo4jTemplate)
.neo4jDriver(driver)
.neo4jMappingContext(mappingContext)
.build();
Additional to the already existing dependencies in a new Spring Boot application,
spring-boot-starter-data-neo4j
, spring-batch-neo4j
and the spring-boot-starter-batch
are needed
but spring-jdbc
and spring-boot-starter-jdbc
must be explicitly excluded.
The exclusions are mandatory to avoid any need for JDBC-based connections, like JDBC URI etc.
See the following build.gradle dependency definition for a minimal example.
dependencies {
implementation ('org.springframework.boot:spring-boot-starter-batch') {
exclude group: 'org.springframework', module: 'spring-jdbc'
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-jdbc'
}
// current development version 0.2.0-SNAPSHOT
implementation 'org.springframework.batch.extensions:spring-batch-neo4j'
implementation 'org.springframework.boot:spring-boot-starter-data-neo4j'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.batch:spring-batch-test'
}
An example of the usage can be seen in the following example, implementing the CommandLineRunner
interface.
@SpringBootApplication
public class TestSpringBatchApplication implements CommandLineRunner {
// those dependencies are created by Spring Boot's
// spring-data-neo4j autoconfiguration
@Autowired
private Driver driver;
@Autowired
private Neo4jMappingContext mappingContext;
@Autowired
private Neo4jTemplate neo4jTemplate;
public static void main(String[] args) {
SpringApplication.run(TestSpringBatchApplication.class, args);
}
@Override
public void run(String... args) {
// writing
Neo4jItemWriter<User> writer = new Neo4jItemWriterBuilder<User>()
.neo4jTemplate(neo4jTemplate)
.neo4jDriver(driver)
.neo4jMappingContext(mappingContext)
.build();
writer.write(Chunk.of(new User("id1", "ab"), new User("id2", "bb")));
// reading
org.neo4j.cypherdsl.core.Node userNode = Cypher.node("User");
Neo4jItemReader<User> reader = new Neo4jItemReaderBuilder<User>()
.neo4jTemplate(neo4jTemplate)
.name("userReader")
.statement(Cypher.match(userNode).returning(userNode))
.targetType(User.class)
.build();
List<User> allUsers = new ArrayList<>();
User user = null;
while ((user = reader.read()) != null) {
System.out.printf("Found user: %s%n", user.name);
allUsers.add(user);
}
// deleting
writer.setDelete(true);
writer.write(Chunk.of(allUsers.toArray(new User[]{})));
}
@Node("User")
public static class User {
@Id public final String id;
public final String name;
public User(String id, String name) {
this.id = id;
this.name = name;
}
}
}