Java batch data processing

Simulate batch processing data

If the amount of data is too large and causes timeout and other problems, the data can be processed in batches

public class BatchUtil {
    public static void listBatchUtil(List<Long> lists) {
        System.out.println(lists);
        // 定义批处理的数据数量(即批处理条件)
        int num = 3;
        // 判断集合数量,如果小于等于定义的数量(即未达到批处理条件),直接进行处理
        if (lists.size() <= num) {
            System.out.println(lists.size());
            System.out.println(lists.toString().substring(1, lists.toString().lastIndexOf("]")));
            return;
        }
        // 如果大于定义的数量,按定义数量进行批处理
        int times = lists.size()/num + 1;
        System.out.println("一共要进行"+times+"次批处理");
        // 遍历分批处理次数,并进行批处理
        for (int i = 0; i < times; i++) {
            // 定义要进行批处理的临时集合
            Set<Long> tempList = new HashSet<>();
            // 将要批处理数据放入临时集合中
            for (int j = i*num; j < lists.size(); j++) {
                System.out.println("i*num"+i*num);
                tempList.add(lists.get(j));
                if (tempList.size() == num) {
                    break;
                }
            }
            System.out.println("进行删除"+tempList);
            // 进行批处理
            System.out.println("======================进行第"+(i+1)+"次批处理=======================");
            //System.out.println(tempList.size());
            System.out.println(tempList.toString().substring(1, tempList.toString().lastIndexOf("]")));
            System.out.println("=========================================================");
        }
    }

    public static void main(String[] args) {
        Set<Long> lists = new HashSet<>();
        for (int i = 1; i <= 26; i++) {
            lists.add(Long.valueOf(i));
        }
        List<Long> list1 = new ArrayList<Long>(lists);
        listBatchUtil(list1);
    }
}
public class BatchUtil2 {
    public static void listBatchUtil(List lists) {
        System.out.println(lists);
        System.out.println(lists.size());
        int temp = 1;
        for (int i = 0; i < lists.size(); i += 10) {
            System.out.println("======================进行第" + temp + "次批处理=======================");
            if (lists.size() - i > 10) {
                System.out.println("位置1"+lists.subList(i, i + 10).toString());
            } else {
                if (lists.size() > i) {
                    System.out.println("位置2"+lists.subList(i, lists.size()).toString());
                }
            }
            temp += 1;
        }
    }

    public static void main(String[] args) {
        List lists = new ArrayList<>();
        for (int i = 1; i <= 26; i++) {
            lists.add(i);
        }
        listBatchUtil(lists);
    }

Guess you like

Origin blog.csdn.net/m0_46580493/article/details/127775161