Spring被动:链接存储库结果

Spring被动:链接存储库结果,spring,project-reactor,reactive,Spring,Project Reactor,Reactive,我如何使用弹簧式反应器来实现这一点 我可以使用blockFirst()在otherRepo中搜索,但它会打破反应链 我也尝试过使用handle()来控制流,但当我找到一个项目时,我无法中断流 有什么想法吗? 谢谢如果您有这样的repo,那么对于repo1的每个记录,如果您需要从repo2中查找记录,您可能可以使用spring数据JPQL连接表&使用您的自定义方法,因为您当前的方法可能会影响性能 因为你似乎只对第一张唱片感兴趣,只是给你一个想法,我们可以实现这样的目标 Repository rep

我如何使用弹簧式反应器来实现这一点

我可以使用blockFirst()在otherRepo中搜索,但它会打破反应链

我也尝试过使用handle()来控制流,但当我找到一个项目时,我无法中断流

有什么想法吗?
谢谢

如果您有这样的repo,那么对于repo1的每个记录,如果您需要从repo2中查找记录,您可能可以使用spring数据JPQL连接表&使用您的自定义方法,因为您当前的方法可能会影响性能

因为你似乎只对第一张唱片感兴趣,只是给你一个想法,我们可以实现这样的目标

Repository repo
Repository otherRepo

foreach entity : repo.FindAll() {
    entityFind = otherRepo.FindById(entity.Prop)
    if (entityFind != null) {
        return entityFind 
    }
}

来自
vins
的答案是假设存储库是非反应式的,因此这里的存储库是完全反应式的:

return Flux.fromIterable(repo.findAll()) //assuming it returns a list
           .map(entity -> otherRepo.findById(entity.property)) // for each entity we query the other repo
           .filter(Objects::nonNull) // replace it with Optional::isPresent if it is optional
           .next();   //converts the flux to mono with the first record
return repo.findAll()//假设为反应式存储库,返回通量
.flatMap(entity->otherRepo.findById(entity.property))//如果找不到id,findById将返回一个空的Mono,这基本上被flatMap忽略
.next()//第一个记录变成单声道,通量被取消

请注意,如您所述,这可能会导致向Cassandra发出不必要的请求(然后被
next()
取消)。这是由于
flatMap
允许多个并发请求(默认为256个)。您可以减少
flatMap
并行性(通过提供第二个参数
int
),或者使用
concatMap
连续执行
findById
查询。

我使用Cassandra。此解决方案的问题是,如果repo1中有n个元素,并且在repo2中的第一个元素中找到了n个元素,那么将在repo2中执行不必要的搜索。谢谢你的回答
return repo.findAll() //assuming reactive repository, which returns Flux<Entity>
    .flatMap(entity -> otherRepo.findById(entity.property)) //findById returns an empty Mono if id not found, which basically gets ignored by flatMap
    .next(); //first record is turned into a Mono, and the Flux is cancelled