1 import java.io.IOException;
2 import java.util.ArrayList;
3 import java.util.List;
4 import java.util.Random;
5
6 import org.apache.hadoop.conf.Configuration;
7 import org.apache.hadoop.hbase.HBaseConfiguration;
8 import org.apache.hadoop.hbase.HColumnDescriptor;
9 import org.apache.hadoop.hbase.HTableDescriptor;
10 import org.apache.hadoop.hbase.KeyValue;
11 import org.apache.hadoop.hbase.MasterNotRunningException;
12 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
13 import org.apache.hadoop.hbase.client.Delete;
14 import org.apache.hadoop.hbase.client.Get;
15 import org.apache.hadoop.hbase.client.HBaseAdmin;
16 import org.apache.hadoop.hbase.client.HTable;
17 import org.apache.hadoop.hbase.client.Result;
18 import org.apache.hadoop.hbase.client.ResultScanner;
19 import org.apache.hadoop.hbase.client.Scan;
20 import org.apache.hadoop.hbase.client.Put;
21 import org.apache.hadoop.hbase.util.Bytes;
22
23 /*
24 * -------优化案例说明------------
25 * 1.优化参数1:Autoflush 默认关闭,需要手动开启
26 * 2.优化参数2:put list size 支持单条与批量
27 * 3.优化参数3:JVM heap size 默认值是平台而不同,需要手动设置
28 * 4.优化参数4:Write Buffer Size 默认值2M
29 * 5.优化参数5:Write Ahead Log 默认开启,需要手动关闭
30 * */
31
32 public class TestInsert {
33 static Configuration hbaseConfig = null;
34
35 public static void main(String[] args) throws Exception {
36 Configuration HBASE_CONFIG = new Configuration();
37 HBASE_CONFIG.set("hbase.master", "192.168.230.133:60000");
38 HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.230.133");
39 HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
40 hbaseConfig = HBaseConfiguration.create(HBASE_CONFIG);
41 //关闭wal,autoflush,writebuffer = 24M
42 insert(false,false,1024*1024*24);
43 //开启AutoFlush,writebuffer = 0
44 insert(false,true,0);
45 //默认值,全部开启
46 insert(true,true,0);
47 }
48
49 private static void insert(boolean wal,boolean autoFlush,long writeBuffer)
50 throws IOException {
51 String tableName="etltest";
52 HBaseAdmin hAdmin = new HBaseAdmin(hbaseConfig);
53 if (hAdmin.tableExists(tableName)) {
54 hAdmin.disableTable(tableName);
55 hAdmin.deleteTable(tableName);
56 }
57
58 HTableDescriptor t = new HTableDescriptor(tableName);
59 t.addFamily(new HColumnDescriptor("f1"));
60 t.addFamily(new HColumnDescriptor("f2"));
61 t.addFamily(new HColumnDescriptor("f3"));
62 t.addFamily(new HColumnDescriptor("f4"));
63 hAdmin.createTable(t);
64 System.out.println("table created");
65
66 HTable table = new HTable(hbaseConfig, tableName);
67 table.setAutoFlush(autoFlush);
68 if(writeBuffer!=0){
69 table.setWriteBufferSize(writeBuffer);
70 }
71 List<Put> lp = new ArrayList<Put>();
72 long all = System.currentTimeMillis();
73
74 System.out.println("start time = "+all);
75 int count = 20000;
76 byte[] buffer = new byte[128];
77 Random r = new Random();
78 for (int i = 1; i <= count; ++i) {
79 Put p = new Put(String.format("row d",i).getBytes());
80 r.nextBytes(buffer);
81 p.add("f1".getBytes(), null, buffer);
82 p.add("f2".getBytes(), null, buffer);
83 p.add("f3".getBytes(), null, buffer);
84 p.add("f4".getBytes(), null, buffer);
85 p.setWriteToWAL(wal);
86 lp.add(p);
87 if(i%1000 == 0){
88 table.put(lp);
89 lp.clear();
90 }
91 }
92
93 System.out.println("WAL="+wal+",autoFlush="+autoFlush+",buffer="+writeBuffer+",count="+count);
94 long end = System.currentTimeMillis();
95 System.out.println("total need time = "+ (end - all)*1.0/1000+"s");
96
97
98 System.out.println("insert complete"+",costs:"+(System.currentTimeMillis()-all)*1.0/1000+"ms");
99 }
100 }