Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

将batch提交改成每200行提交一次,每提交一次sleep一次,用com.google.common.collect.Lists#partition 实现 public…

  1. User

    将batch提交改成每200行提交一次,每提交一次sleep一次,用com.google.common.collect.Lists#partition 实现

    public void run() {
    	Object[][] args = new Object[preIds.size()][2];
    	int i = 0;
    	for (Long pre_action_id : preIds) {
    		args[i][0] = pre_action_id;
    		args[i++][1] = sub_id;
    	}
    	try {
    		runner.batch("insert into " + target_table + "(pre_id,sub_id) values(?,?)", args);
    	} catch (Exception e) {
    		e.printStackTrace();
    		System.err.println("fail " + sub_id + " " + preIds.toString());
    	}
    }
    
  2. Assistant

    为了解决这个问题,我们需要将数据分区为每200行,然后在每次批量提交后添加一个 sleep 方法。在 Java 中,我们可以使用 Guava 库的 Lists.partition 方法实现列表分区。

    下面是如何实现这个逻辑:

    1. 将 args 列表分割成每200条记录一个分区。
    2. 对于每个分区,执行批量插入操作。
    3. 在每次批量插入后调用 Thread.sleep() 方法。

    以下是示例代码:

    import com.google.common.collect.Lists;
    
    import java.sql.SQLException;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    public class BatchSubmitter {
        private final List<Long> preIds;
        private final long sub_id;
        private final String target_table;
        private final YourDatabaseRunner runner;
    
        public BatchSubmitter(List<Long> preIds, long sub_id, String target_table, YourDatabaseRunner runner) {
            this.preIds = preIds;
            this.sub_id = sub_id;
            this.target_table = target_table;
            this.runner = runner;
        }
    
        public void run() {
            Object[][] args = new Object[preIds.size()][2];
            int i = 0;
            for (Long pre_action_id : preIds) {
                args[i][0] = pre_action_id;
                args[i++][1] = sub_id;
            }
    
            try {
                // 每200行进行分区
                List<Object[][]> partitions = Lists.partition(List.of(args), 200);
                for (Object[][] partition : partitions) {
                    runner.batch("insert into " + target_table + "(pre_id,sub_id) values(?,?)", partition);
                    // 批量提交后等待一段时间
                    TimeUnit.MILLISECONDS.sleep(1000);  // 例如,等待1秒
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.err.println("SQL Exception for sub_id " + sub_id + " " + preIds);
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
                System.err.println("Thread was interrupted during sleep");
            }
        }
    }
    

    请注意:

    • YourDatabaseRunner 是一个占位符,代表在你的实际环境中负责数据库操作的类。
    • TimeUnit.MILLISECONDS.sleep(1000); 表示等待1秒钟,可以根据需要更改等待时间。
    • Lists.partition() 方法用于分割集合。
    • 处理了可能出现的异常,如 SQLException 和 InterruptedException。

    确保项目中已引入 Guava 库,以便使用 Lists.partition() 方法。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.