在使用HBase Put API的时候,有几个会影响性能的因素。
1.Put List Size
HBase的Put支持单条插入,也支持批量插入。
2. AutoFlush
AutoFlush指的是在每次调用HBase的Put操作,是否提交到HBase Server。 默认是true,每次会提交。如果此时是单条插入,就会有更多的IO,从而降低性能
3.Write Buffer Size
Write Buffer Size在AutoFlush为false的时候起作用,默认是2MB,也就是当插入数据超过2MB,就会自动提交到Server
4.WAL
WAL是Write Ahead Log的缩写,指的是HBase在插入操作前是否写Log。默认是打开,关掉会提高性能,但是如果系统出现故障(负责插入的Region Server挂掉),数据可能会丢失。
下面是一个简单的测试:
table: 4个family 每行插入的数据4KB,每次提交1000行
WAL=false,autoFlush=false,buffer=25165824 insert complete,costs:0.4453ms/row
WAL=false,autoFlush=true,buffer=0 insert complete,costs:0.6ms/row
WAL=true,autoFlush=true,buffer=0 insert complete,costs:1.8797ms/row
可以看出在WAL关闭,并且设置比较合适的buffer size,可以将性能提高到4到5倍.
测试代码:
public class TestInsert {
static HBaseConfiguration hbaseConfig;
public static void main(String[] args) throws Exception {
Configuration HBASE_CONFIG = new Configuration();
HBASE_CONFIG.set("hbase.zookeeper.quorum", "gms5,gms6");
HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
hbaseConfig = new HBaseConfiguration(HBASE_CONFIG);
insert(false,false,1024*1024*24);
insert(false,true,0);
insert(true,true,0);
}
private static void insert(boolean wal,boolean autoFlush,long writeBuffer)
throws IOException {
String tableName="etltest";
HBaseAdmin hAdmin = new HBaseAdmin(hbaseConfig);
if (hAdmin.tableExists(tableName)) {
hAdmin.disableTable(tableName);
hAdmin.deleteTable(tableName);
HTableDescriptor t = new HTableDescriptor(tableName);
t.addFamily(new HColumnDescriptor("f1"));
t.addFamily(new HColumnDescriptor("f2"));
t.addFamily(new HColumnDescriptor("f3"));
t.addFamily(new HColumnDescriptor("f4"));
hAdmin.createTable(t);
System.out.println("table created");
}
HTable table = new HTable(hbaseConfig, tableName);
table.setAutoFlush(autoFlush);
if(writeBuffer!=0){
table.setWriteBufferSize(writeBuffer);
}
List<Put> lp = new ArrayList<Put>();
long all = System.currentTimeMillis();
int count = 10000;
byte[] buffer = new byte[1024];
Random r = new Random();
for (int i = 1; i <= count; ++i) {
Put p = new Put(String.format("row d",i).getBytes());
r.nextBytes(buffer);
p.add("f1".getBytes(), null, buffer);
p.add("f2".getBytes(), null, buffer);
p.add("f3".getBytes(), null, buffer);
p.add("f4".getBytes(), null, buffer);
p.setWriteToWAL(wal);
lp.add(p);
if(100==0){
table.put(lp);
lp.clear();
}
}
System.out.println("WAL="+wal+",autoFlush="+autoFlush+",buffer="+writeBuffer);
System.out.println("insert complete"+",costs:"+(System.currentTimeMillis()-all)*1.0/count+"ms");
}
}