Skip to content

Commit b643162

Browse files
aravind-r-ranganathanjzheaux
authored andcommitted
Spring Data Cassandra Reactive
Issue: BAEL-1870
1 parent ac37b4f commit b643162

8 files changed

Lines changed: 254 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>spring-data-cassandra-reactive</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>spring-data-cassandra-reactive</name>
13+
<description>Spring Data Cassandra reactive</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>2.1.0.RELEASE</version>
19+
<relativePath/>
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8
25+
</project.reporting.outputEncoding>
26+
<java.version>1.8</java.version>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.data</groupId>
32+
<artifactId>spring-data-cassandra</artifactId>
33+
<version>2.1.2.RELEASE</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>io.projectreactor</groupId>
37+
<artifactId>reactor-core</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-web</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.projectlombok</groupId>
45+
<artifactId>lombok</artifactId>
46+
<optional>true</optional>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-test</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.projectreactor</groupId>
55+
<artifactId>reactor-test</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-maven-plugin</artifactId>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
69+
70+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.cassandra.reactive;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringDataCassandraReactiveApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringDataCassandraReactiveApplication.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.cassandra.reactive.controller;
2+
3+
import com.baeldung.cassandra.reactive.model.Employee;
4+
import com.baeldung.cassandra.reactive.service.EmployeeService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
import reactor.core.publisher.Flux;
11+
import reactor.core.publisher.Mono;
12+
13+
import javax.annotation.PostConstruct;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
@RestController
18+
@RequestMapping("employee")
19+
public class EmployeeController {
20+
21+
@Autowired
22+
EmployeeService employeeService;
23+
24+
@PostConstruct
25+
public void saveEmployees() {
26+
List<Employee> employees = new ArrayList<>();
27+
employees.add(new Employee(123, "John Doe", "Delaware", "[email protected]", 31));
28+
employees.add(new Employee(324, "Adam Smith", "North Carolina", "[email protected]", 43));
29+
employees.add(new Employee(355, "Kevin Dunner", "Virginia", "[email protected]", 24));
30+
employees.add(new Employee(643, "Mike Lauren", "New York", "[email protected]", 41));
31+
employeeService.initializeEmployees(employees);
32+
}
33+
34+
@GetMapping("/list")
35+
public Flux<Employee> getAllEmployees() {
36+
Flux<Employee> employees = employeeService.getAllEmployees();
37+
return employees;
38+
}
39+
40+
@GetMapping("/{id}")
41+
public Mono<Employee> getEmployeeById(@PathVariable int id) {
42+
return employeeService.getEmployeeById(id);
43+
}
44+
45+
@GetMapping("/filterByAge/{age}")
46+
public Flux<Employee> getEmployeesFilterByAge(@PathVariable int age) {
47+
return employeeService.getEmployeesFilterByAge(age);
48+
}
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.cassandra.reactive.model;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
7+
import org.springframework.data.cassandra.core.mapping.Table;
8+
9+
@Table
10+
@Data
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
public class Employee {
14+
@PrimaryKey
15+
private int id;
16+
private String name;
17+
private String address;
18+
private String email;
19+
private int age;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.cassandra.reactive.repository;
2+
3+
import com.baeldung.cassandra.reactive.model.Employee;
4+
import org.springframework.data.cassandra.repository.AllowFiltering;
5+
import org.springframework.data.cassandra.repository.ReactiveCassandraRepository;
6+
import reactor.core.publisher.Flux;
7+
8+
public interface EmployeeRepository extends ReactiveCassandraRepository<Employee, Integer> {
9+
@AllowFiltering
10+
Flux<Employee> findByAgeGreaterThan(int age);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.cassandra.reactive.service;
2+
3+
import com.baeldung.cassandra.reactive.model.Employee;
4+
import com.baeldung.cassandra.reactive.repository.EmployeeRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.Mono;
9+
10+
import java.util.List;
11+
12+
@Service
13+
public class EmployeeService {
14+
15+
@Autowired
16+
EmployeeRepository employeeRepository;
17+
18+
public void initializeEmployees(List<Employee> employees) {
19+
Flux<Employee> savedEmployees = employeeRepository.saveAll(employees);
20+
savedEmployees.subscribe();
21+
}
22+
23+
public Flux<Employee> getAllEmployees() {
24+
Flux<Employee> employees = employeeRepository.findAll();
25+
return employees;
26+
}
27+
28+
public Flux<Employee> getEmployeesFilterByAge(int age) {
29+
return employeeRepository.findByAgeGreaterThan(age);
30+
}
31+
32+
public Mono<Employee> getEmployeeById(int id) {
33+
return employeeRepository.findById(id);
34+
}
35+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.data.cassandra.keyspace-name=practice
2+
spring.data.cassandra.port=9042
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.cassandra.reactive;
2+
3+
import com.baeldung.cassandra.reactive.model.Employee;
4+
import com.baeldung.cassandra.reactive.repository.EmployeeRepository;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
import reactor.core.publisher.Flux;
12+
import reactor.core.publisher.Mono;
13+
import reactor.test.StepVerifier;
14+
15+
16+
@RunWith(SpringRunner.class)
17+
@SpringBootTest
18+
public class ReactiveEmployeeRepositoryIntegrationTest {
19+
20+
@Autowired
21+
EmployeeRepository repository;
22+
23+
@Before
24+
public void setUp() {
25+
26+
Flux<Employee> deleteAndInsert = repository.deleteAll() //
27+
.thenMany(repository.saveAll(Flux.just(
28+
new Employee(111, "John Doe", "Delaware", "[email protected]", 31),
29+
new Employee(222, "Adam Smith", "North Carolina", "[email protected]", 43),
30+
new Employee(333, "Kevin Dunner", "Virginia", "[email protected]", 24),
31+
new Employee(444, "Mike Lauren", "New York", "[email protected]", 41))));
32+
33+
StepVerifier.create(deleteAndInsert).expectNextCount(4).verifyComplete();
34+
}
35+
36+
@Test
37+
public void givenRecordsAreInserted_whenDbIsQueried_thenShouldIncludeNewRecords() {
38+
39+
Mono<Long> saveAndCount = repository.count()
40+
.doOnNext(System.out::println)
41+
.thenMany(repository.saveAll(Flux.just(new Employee(325, "Kim Jones", "Florida", "[email protected]", 42),
42+
new Employee(654, "Tom Moody", "New Hampshire", "[email protected]", 44))))
43+
.last()
44+
.flatMap(v -> repository.count())
45+
.doOnNext(System.out::println);
46+
47+
StepVerifier.create(saveAndCount).expectNext(6L).verifyComplete();
48+
}
49+
50+
@Test
51+
public void givenAgeForFilter_whenDbIsQueried_thenShouldReturnFilteredRecords() {
52+
StepVerifier.create(repository.findByAgeGreaterThan(35)).expectNextCount(2).verifyComplete();
53+
}
54+
55+
}

0 commit comments

Comments
 (0)