(七)多线程写入数据

1.

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
public class Treadmore2 {
    static Configuration conf = HBaseConfiguration.create();
    @SuppressWarnings("deprecation")
    public static HTablePool pool = new HTablePool(conf, 1000);
    public static String tableName = "threadmore";
    @SuppressWarnings("deprecation")
    public static void InsertProcess(int startdata, int enddata) throws IOException {
        long start = System.currentTimeMillis();        
        HTableInterface table = pool.getTable(tableName);
        table.setAutoFlush(false);
        table.setWriteBufferSize(24 * 1024 * 1024);
        // 构造测试数据
        List<Put> list = new ArrayList<Put>();
        int count = 100;
        for (int i = startdata; i < enddata; i++) {
            Put put = new Put(String.format("row %d", i).getBytes());
            put.add("cf".getBytes(), null, Bytes.toBytes("" + i));
            put.setWriteToWAL(false);
            list.add(put);
            if (i % 10000 != 0) {
                table.put(list);
                list.clear();
                table.flushCommits();
            }
        }
        long stop = System.currentTimeMillis();
        System.out.println(
                "线程:" + Thread.currentThread().getId() + "插入数据:" + count + "共耗时:" + (stop - start) * 1.0 / 1000 + "s");
    }
    public static void ThreadInsert() throws InterruptedException {
        System.out.println("---------开始MultThreadInsert测试----------");
        long start = System.currentTimeMillis();
        int threadNumber = 10;
        Thread[] threads = new Thread[threadNumber];
        int startnum = 1;
        int endnum = 0;
        for (int i = 0; i < threads.length; i++) {
            startnum = endnum ;
            endnum = (i + 1) * 100;
            threads[i] = new ImportThread(startnum, endnum);
            threads[i].start();
            
        }
        for (int j = 0; j < threads.length; j++) {
            (threads[j]).join();
        }
        long stop = System.currentTimeMillis();
        System.out.println("MultThreadInsert:" + threadNumber * 100 + "共耗时:" + (stop - start) * 1.0 / 1000 + "s");
        System.out.println("---------结束MultThreadInsert测试----------");
    }
    public static class ImportThread extends Thread {
        int startdata, enddata;
        public ImportThread(int startdata, int enddata) {
            this.startdata = startdata;
            this.enddata = enddata;
        }
        public void run() {
            try {
                InsertProcess(startdata, enddata);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                System.gc();
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        ThreadInsert();
    }
}

posted @ 2017-12-11 00:00  appointint  阅读(244)  评论(0编辑  收藏  举报