如何在 Spring 中将 JdbcTemplate 转换为 Flux?
How to convert a JdbcTemplate to Flux in Spring?
我有一个 return 是 List<Item>
的现有服务。这些项目是由多个后续数据库调用、解析和聚合创建的(比本例中复杂得多)。
如何将以下示例转换为 Flux
,以便流式传输我的结果,而不必在内存中聚合之前的所有项目?
@RestController
public class BookingInfoServlet {
@Autowired
private JdbcTemplate jdbc;
@GetMapping(value = "/export", produces = APPLICATION_JSON_VALUE)
public List<Item> export(String productType) {
List<Item> list = new ArrayList<>();
for (int i = 0; i < jdbc.count(); i++) {
List<String> refIds = jdbc.queryForList("SELECT ref_id FROM products where type = ? LIMIT 1000 OFFSET = ?", String.class, productType, i);
for (String id : refIds) {
Map map = jdbc.queryForMap("SELECT <anything> ... where some_id = ?, id);
Item item = new Item();
item.setName(map.get("name"));
item.setCode(map.getCode("code"));
item.set...
list.add(item);
}
//TODO how to convert to Flux here and already send the chunks back into the stream?
}
return list; //TODO how to convert to Flux?
}
}
第一个问题:这里我首先将第一个查询的所有结果提取到内存中,然后在内存中迭代并形成我所有的 Item
s,然后 returning 整个列表。
因此我正在尝试 return Flux<Item>
。但是:我现在如何在使用 JdbcTemplate
时准确地 return 通量?
由于没有异步 mysql
java 驱动程序,我可能必须将数据库查找分成 1000 个块进行分页,然后准备 1000 个项目并将它们流式传输回客户端。然后获取下 1000 个项目。但是我怎样才能让他们直接进入流呢?
public Flux<Item> export(String productType) {
int pageSize = 1000;
int count = jdbc.count();
return Flux.range(0, count / pageSize) //page numbers
.flatMapIterable(pageNumber ->
jdbc.queryForList("SELECT ref_id FROM products where type = ? LIMIT ? OFFSET = ?",
String.class,
productType,
pageSize,
pageNumber * pageSize))
.map(id -> {
Map map = jdbc.queryForMap("SELECT <anything> ... where some_id = ?", id);
Item item = new Item();
//
//
return item;
});
}
我有一个 return 是 List<Item>
的现有服务。这些项目是由多个后续数据库调用、解析和聚合创建的(比本例中复杂得多)。
如何将以下示例转换为 Flux
,以便流式传输我的结果,而不必在内存中聚合之前的所有项目?
@RestController
public class BookingInfoServlet {
@Autowired
private JdbcTemplate jdbc;
@GetMapping(value = "/export", produces = APPLICATION_JSON_VALUE)
public List<Item> export(String productType) {
List<Item> list = new ArrayList<>();
for (int i = 0; i < jdbc.count(); i++) {
List<String> refIds = jdbc.queryForList("SELECT ref_id FROM products where type = ? LIMIT 1000 OFFSET = ?", String.class, productType, i);
for (String id : refIds) {
Map map = jdbc.queryForMap("SELECT <anything> ... where some_id = ?, id);
Item item = new Item();
item.setName(map.get("name"));
item.setCode(map.getCode("code"));
item.set...
list.add(item);
}
//TODO how to convert to Flux here and already send the chunks back into the stream?
}
return list; //TODO how to convert to Flux?
}
}
第一个问题:这里我首先将第一个查询的所有结果提取到内存中,然后在内存中迭代并形成我所有的 Item
s,然后 returning 整个列表。
因此我正在尝试 return Flux<Item>
。但是:我现在如何在使用 JdbcTemplate
时准确地 return 通量?
由于没有异步 mysql
java 驱动程序,我可能必须将数据库查找分成 1000 个块进行分页,然后准备 1000 个项目并将它们流式传输回客户端。然后获取下 1000 个项目。但是我怎样才能让他们直接进入流呢?
public Flux<Item> export(String productType) {
int pageSize = 1000;
int count = jdbc.count();
return Flux.range(0, count / pageSize) //page numbers
.flatMapIterable(pageNumber ->
jdbc.queryForList("SELECT ref_id FROM products where type = ? LIMIT ? OFFSET = ?",
String.class,
productType,
pageSize,
pageNumber * pageSize))
.map(id -> {
Map map = jdbc.queryForMap("SELECT <anything> ... where some_id = ?", id);
Item item = new Item();
//
//
return item;
});
}