Java Batch processing data

Source: Internet
Author: User

Requirements: A total of 1000 data, the first batch insert 100, the second batch Insert 101 to 200, insert data in turn;

The way of implementation here choose two commonly used methods, are using the list operation;

The first way to realize the idea is as follows:

<1> the list for the original storage data is recordlist, and the number of batch processing is obtained;

<2> Create a list for list, and after the loop, put the recordlist of the first MaxValue data into the list;

<3> Call the batch processing method, call Recordlist's RemoveAll method to remove the data from the list from the recordlist;

<4> call the List.clear method to erase the data from the list itself;

/*** BULK Insert for example: A total of 1000 data, the first batch insert 100, the second batch Insert 101 to 200, insert data in turn; *@paramRecordlist *@parammaxValue Number of batches processed, such as 1000 *@return     */    Private voidBatchaddrecords (list<string> recordlist,intmaxValue) {List<String> list =NewArraylist<string>(); intSize =recordlist.size (); intTotal = size/MaxValue; if(size% MaxValue! = 0) { Total+ = 1; }                 for(inti = 0; I < total; i++) {            if(i = = Total-1) {MaxValue= Size-(I *maxValue); }             for(intj = 0; J < MaxValue; J + +) {List.add (Recordlist.get (j)); }            //Methods for batch processingprint (list);            Recordlist.removeall (list);        List.clear (); }    }

The second way to achieve this is similar:

By calling Recordlist's Sublist method, the captured content is saved to the list, then the batch processing method is called, and finally the clear method of the list is called to clear the data in list and recordlist.

Note that the Sublist method of the list is a bit special, and it operates on the same piece of memory that the list and the original list refer to, so be aware of it.

Private voidBatchAddRecords2 (list<string> recordlist,intmaxValue) {List<String> list =NewArraylist<string>(); intSize =recordlist.size (); intTotal = size/MaxValue; if(size% MaxValue! = 0) { Total+ = 1; }         for(inti = 0; I < total; i++) {            if(i = = Total-1) {MaxValue= Size-(I *maxValue); } list= Recordlist.sublist (0, MaxValue); //Methods for batch processingprint (list);        List.clear (); }    }

Java Batch processing data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.