Jooq Java代码处理超过20万条记录的表

huangapple go评论133阅读模式
英文:

Jooq Java Code to handle more than 200k records from a table

问题

我的翻译如下:

我的数据库中的表有超过200,000条记录,我不想一次性从resultSet中获取所有记录进行处理。我正在尝试编写一个服务中的函数,调用另一个持久化类的函数,只获取1000条记录进行处理。
一旦处理完这1000条记录,它应该获取接下来的1000条记录。我在我的Java代码中使用了JOOQ数据库库。我只是分享了我脑海中的一个示例代码框架。

  1. class RecordService {
  2. RecordPersistence recordPersistence = new RecordPersistence();
  3. public void processRecords() {
  4. List<Record> records = recordPersistence.fetchRecords();
  5. // 处理记录的代码
  6. }
  7. }
  8. class RecordPersistence {
  9. public List<Record> fetchRecords(){
  10. Cursor<Record> records = dsl.select...fetchLazy();
  11. while (records.hasNext()) {
  12. records.fetchNext(1000);
  13. }
  14. return records;
  15. }
  16. }

如何从fetchRecords()函数中仅返回少量记录?我应该将其编写为异步函数吗?这是处理结果的正确方式吗?还是有更好的处理方式来处理我的情况?

英文:

The table in my database has more than 200k records and I don't want to fetch all of them at once in the resultSet to process. I am trying to write a function in a service that calls another persistence class function to bring only 1000 records to process.
Once it processes the 1000 records, it should fetch the following thousand records. I am using the JOOQ database library in my Java code. I am just sharing a sample skeleton code that I had in my mind.

  1. class RecordService {
  2. RecordPersistence recordPersistence = new RecordPersistence();
  3. public void processRecords() {
  4. List&lt;Record&gt; records = recordPersistence.fetchRecords();
  5. // Code to process them
  6. }
  7. }
  8. class RecordPersistence {
  9. public List&lt;Record&gt; fetchRecords(){
  10. Cursor&lt;Record&gt; records = dsl.select...fetchLazy();
  11. while (records.hasNext()) {
  12. records.fetchNext(1000);
  13. }
  14. return records
  15. }
  16. }

How to return only a few records from the fetchRecords() function? Should I write this is an async function? Is this the correct way to process result? or is there a better way to handle my case?

答案1

得分: 0

如评论中所提到的,问题在于你将处理逻辑放在了获取逻辑之外。只需将其移动到内部,例如:

  1. class RecordService {
  2. RecordPersistence recordPersistence = new RecordPersistence();
  3. public void processRecords() {
  4. recordPersistence.fetchRecords(
  5. (List<Record> records) -> {
  6. // 处理记录的代码
  7. }
  8. );
  9. }
  10. }
  11. class RecordPersistence {
  12. public List<Record> fetchRecords(Consumer<? super List<Record>> consumer) {
  13. try (Cursor<Record> cursor = dsl.select...fetchLazy()) {
  14. while (cursor.hasNext()) {
  15. consumer.accept(cursor.fetchNext(1000));
  16. }
  17. }
  18. }
  19. }

对于这种类型的流式处理,你不需要进行任何范式转换(例如异步、响应式等)。只需从外部迭代切换到内部迭代即可。

英文:

As mentioned in the comments, the problem is that you're keeping the processing logic outside of the fetch logic. Just move it inside, e.g. as follows:

  1. class RecordService {
  2. RecordPersistence recordPersistence = new RecordPersistence();
  3. public void processRecords() {
  4. recordPersistence.fetchRecords(
  5. (List&lt;Record&gt; records) -&gt; {
  6. // Code to process them
  7. }
  8. );
  9. }
  10. }
  11. class RecordPersistence {
  12. public List&lt;Record&gt; fetchRecords(Consumer&lt;? super List&lt;Record&gt;&gt; consumer) {
  13. try (Cursor&lt;Record&gt; cursor = dsl.select...fetchLazy()) {
  14. while (cursor.hasNext()) {
  15. consumer.accept(cursor.fetchNext(1000));
  16. }
  17. }
  18. }
  19. }

You don't need any paradigm shift (e.g. async, reactive, etc.) for this type of streaming to work. Just switch from external to internal iteration

huangapple
  • 本文由 发表于 2023年8月9日 09:19:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定