Jooq Java代码来处理超过20万条来自表格的记录。

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

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

问题

fetchRecords() 函数中如何只返回一部分记录?我应该将其编写为异步函数吗?这样处理结果是否正确?或者是否有更好的处理方法?

  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. }
英文:

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-2.html
匿名

发表评论

匿名网友

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

确定