@@ -7,25 +7,105 @@ This extension contains an `ItemReader` and `ItemWriter` implementations for [Ne
7
7
The ` Neo4jItemReader ` can be configured as follows:
8
8
9
9
``` java
10
- SessionFactory sessionFactory = ...
11
- Neo4jItemReader<String > itemReader = new Neo4jItemReaderBuilder<String > ()
12
- .sessionFactory(sessionFactory)
13
- .name(" itemReader" )
14
- .targetType(String . class)
15
- .startStatement(" n=node(*)" )
16
- .orderByStatement(" n.age" )
17
- .matchStatement(" n -- m" )
18
- .whereStatement(" has(n.name)" )
19
- .returnStatement(" m" )
10
+ Neo4jItemReader<User > reader = new Neo4jItemReaderBuilder<User > ()
11
+ .neo4jTemplate(neo4jTemplate)
12
+ .name(" userReader" )
13
+ .statement(Cypher . match(userNode). returning(userNode))
14
+ .targetType(User . class)
20
15
.pageSize(50 )
21
16
.build();
22
17
```
23
18
24
19
The ` Neo4jItemWriter ` can be configured as follows:
25
20
26
21
``` java
27
- SessionFactory sessionFactory = ...
28
- Neo4jItemWriter<String > writer = new Neo4jItemWriterBuilder<String > ()
29
- .sessionFactory(sessionFactory)
22
+ Neo4jItemWriter<User > writer = new Neo4jItemWriterBuilder<User > ()
23
+ .neo4jTemplate(neo4jTemplate)
24
+ .neo4jDriver(driver)
25
+ .neo4jMappingContext(mappingContext)
30
26
.build();
27
+ ```
28
+
29
+ ## Minimal Spring Boot example
30
+
31
+ Additional to the already existing dependencies in a new Spring Boot application,
32
+ ` spring-boot-starter-data-neo4j ` , ` spring-batch-neo4j ` and the ` spring-boot-starter-batch ` are needed
33
+ but ` spring-jdbc ` and ` spring-boot-starter-jdbc ` must be explicitly excluded.
34
+ The exclusions are mandatory to avoid any need for JDBC-based connections, like JDBC URI etc.
35
+
36
+ See the following _ build.gradle_ dependency definition for a minimal example.
37
+
38
+ ``` groovy
39
+ dependencies {
40
+ implementation ('org.springframework.boot:spring-boot-starter-batch') {
41
+ exclude group: 'org.springframework', module: 'spring-jdbc'
42
+ exclude group: 'org.springframework.boot', module: 'spring-boot-starter-jdbc'
43
+ }
44
+ // current development version 0.2.0-SNAPSHOT
45
+ implementation 'org.springframework.batch.extensions:spring-batch-neo4j'
46
+ implementation 'org.springframework.boot:spring-boot-starter-data-neo4j'
47
+ testImplementation 'org.springframework.boot:spring-boot-starter-test'
48
+ testImplementation 'org.springframework.batch:spring-batch-test'
49
+ }
50
+ ```
51
+
52
+ An example of the usage can be seen in the following example, implementing the ` CommandLineRunner ` interface.
53
+
54
+ ``` java
55
+ @SpringBootApplication
56
+ public class TestSpringBatchApplication implements CommandLineRunner {
57
+ // those dependencies are created by Spring Boot's
58
+ // spring-data-neo4j autoconfiguration
59
+ @Autowired
60
+ private Driver driver;
61
+ @Autowired
62
+ private Neo4jMappingContext mappingContext;
63
+ @Autowired
64
+ private Neo4jTemplate neo4jTemplate;
65
+
66
+ public static void main (String [] args ) {
67
+ SpringApplication . run(TestSpringBatchApplication . class, args);
68
+ }
69
+
70
+ @Override
71
+ public void run (String ... args ) {
72
+ // writing
73
+ Neo4jItemWriter<User > writer = new Neo4jItemWriterBuilder<User > ()
74
+ .neo4jTemplate(neo4jTemplate)
75
+ .neo4jDriver(driver)
76
+ .neo4jMappingContext(mappingContext)
77
+ .build();
78
+ writer. write(Chunk . of(new User (" id1" , " ab" ), new User (" id2" , " bb" )));
79
+
80
+ // reading
81
+ org.neo4j.cypherdsl.core. Node userNode = Cypher . node(" User" );
82
+ Neo4jItemReader<User > reader = new Neo4jItemReaderBuilder<User > ()
83
+ .neo4jTemplate(neo4jTemplate)
84
+ .name(" userReader" )
85
+ .statement(Cypher . match(userNode). returning(userNode))
86
+ .targetType(User . class)
87
+ .build();
88
+ List<User > allUsers = new ArrayList<> ();
89
+ User user = null ;
90
+ while ((user = reader. read()) != null ) {
91
+ System . out. printf(" Found user: %s%n" , user. name);
92
+ allUsers. add(user);
93
+ }
94
+
95
+ // deleting
96
+ writer. setDelete(true );
97
+ writer. write(Chunk . of(allUsers. toArray(new User []{})));
98
+ }
99
+
100
+ @Node (" User" )
101
+ public static class User {
102
+ @Id public final String id;
103
+ public final String name;
104
+
105
+ public User (String id , String name ) {
106
+ this . id = id;
107
+ this . name = name;
108
+ }
109
+ }
110
+ }
31
111
```
0 commit comments