Search code examples
spring-webfluxproject-reactorspring-data-cassandra

Mono Slice with data transformation in spring reactive cassandra


After reading the new spring data casandra documentation (here), it says that there is now Mono<Slice<T>> support for reactive cassandra. Which is great, because in my team, we wanted to implement some sort of pagination on our reactive response. So we are happy to move from Flux<T> to Mono<Slice<T>> but there is this issue, we do transformations on the data of our flux, with flux.map, but that doesn't seem possible with Slice without blocking.

For example, we have this functionallity:

Flux<Location> resp = repository.searchLocations(searchFields).map(this::transformLocation);

Where transformLocation is a function that receives the database object and returns a more user friendly object with more user friendly data. How will you achieve that with Mono<Slice<Location>>?

From what I have seen of Slice, you can get the data with getContent, but that returns a list, will that not be like blocking?


Solution

  • You can use the list, got from the getContent() method, to create a flux as you were doing before.

    Mono<Slice<Location>> sliceMono; //this is just another mono on which you can operate
    
    Flux<location> intermediateResp = sliceMono.flatMap(slice -> Flux.fromIterable(slice.getContent()));
    
    //you can now transform this intermediateResp flux as you were doing before, can't you?
    

    Won't this serve your purpose or do you require something else?

    (The code has been written without the help of IDE, so use it to understand the approach)