join hbaseマルチスレッドを使用して無効な問題を挿入
1520 ワード
joinなしでマルチスレッドを実行できないコードは、@Test public void multInsert()throws Exception{final Connection conn=HbaseUtils.conn();final String tableName="ns 1:t 2";Thread t 1=new Thread("thread 1"){public void run(){String inset=inset(0,300,tableName,conn);System.out.println(inset)};Thread t2 =new Thread(“thread2”){ public void run() { inset(300,600,tableName,conn); } }; Thread t3 = new Thread(“thread3”) { public void run() { inset(600, 1000, tableName, conn); } }; t1.start(); t2.start(); t3.start(); t1.join(); t2.join(); t3.join(); }
//
private String inset(int start,int end,String tableName,Connection conn ) {
try {
TableName tname = TableName.valueOf(tableName);
HTable table = (HTable) conn.getTable(tname);
DecimalFormat df = new DecimalFormat("0000");
table.setAutoFlush(false);
for (int j = start; j < end; j++) {
byte[] rowkey = Bytes.toBytes("row" + df.format(j));
Put put = new Put(rowkey);
put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("id"), Bytes.toBytes(end - 1));
put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes("name" + j));
table.put(put);
if (j % 2000 == 0) {
table.flushCommits();
}
}
table.flushCommits();
}catch(Exception e){
log.error(" :{}",e);
}
return " :"+(end-start);
}