我从两个数据库表中获取值,在重复删除之后,基于它们从第三个表中获取值。我的示例代码是:
db.select("SELECT id from table1 ")
.getAs(String.class)
.collectInto(new HashSet<String>(), HashSet::add)
.zipWith(db.select("SELECT id from table2 ")
.getAs(String.class)
.collectInto(new HashSet<String>(), HashSet::add),new BiFunction<Set,Set, Single<HashSet<String>>>() {
@Override
public Single<HashSet<String>> apply(Set t1, Set t2) throws Exception {
t1.addAll(t2);
return db.select("select useful_data from table3 where id IN ("+AsQuatedString(t1)+").getAs(String.class).collectInto(new HashSet<String>(), HashSet::add);
}
})/*Point A*/.subscribe(a -> a.subscribe(b -> System.out.println(b)));所以问题是在A点,我得到的Single<Single<HashSet>>不能作为rest客户端的响应。
如何将其转换为Single<HashSet>。
使用这进行DB连接
发布于 2019-02-02 21:54:27
您的zip应该返回一个带有合并集的Single<HashSet>,然后您可以将第二个数据库请求放在flatmap中。
db.select("SELECT id from table1 ")
.getAs(String.class)
.collectInto(new HashSet<String>(), HashSet::add)
.zipWith(db.select("SELECT id from table2 ")
.getAs(String.class)
.collectInto(new HashSet<String>(), HashSet::add), (t1, t2) -> {
t1.addAll(t2);
return t1;
})
.flatMap(concatSet -> {
// concatSet is a HashMap<String>
return db.select("select useful_data from table3 where id IN ("+AsQuatedString(concatSet)+")
.getAs(String.class)
.collectInto(new HashSet<String>(), HashSet::add);
})
.subscribe(b -> { // b is a HashMap<String>, result of your second db request.
System.out.println(b);
});https://stackoverflow.com/questions/54497075
复制相似问题