Hbase Java API使用
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* @author sy
* @Create 2020-03-21-21:54
*/
public class HbaseDemo {
HBaseAdmin admin;
HTable hTable;
String TN="phone";//表名
String RowKey="123456";
@Before
public void init() throws IOException {
Configuration configuration=new Configuration();
configuration.set("hbase.zookeeper.quorum","node02,node03,node04");//加入zookeeper信息
admin=new HBaseAdmin(configuration);
hTable=new HTable(configuration,TN.getBytes());
}
@Test
public void createtest() throws IOException {
//创建phone表
if (admin.tableExists(TN)){
admin.disableTable(TN);
admin.deleteTable(TN);
}
HTableDescriptor desc=new HTableDescriptor(TableName.valueOf(TN));
HColumnDescriptor cf=new HColumnDescriptor("cf".getBytes());
desc.addFamily(cf);
admin.createTable(desc);
}
//插入数据
@Test
public void insertDB() throws InterruptedIOException, RetriesExhaustedWithDetailsException {
Put put=new Put(RowKey.getBytes());
put.add("cf".getBytes(),"name".getBytes(),"xiaoming".getBytes());
put.add("cf".getBytes(),"age".getBytes(),"12".getBytes());
put.add("cf".getBytes(),"sex".getBytes(),"male".getBytes());
hTable.put(put);
}
//查看数据
@Test
public void getDB() throws IOException {
Get get=new Get(RowKey.getBytes());
Result rs=hTable.get(get);
get.addColumn("cf".getBytes(),"name".getBytes());
Cell cell = rs.getColumnLatestCell("cf".getBytes(),"name".getBytes());
Cell cell2 = rs.getColumnLatestCell("cf".getBytes(),"age".getBytes());
Cell cell3 = rs.getColumnLatestCell("cf".getBytes(),"sex".getBytes());
System.out.println(new String(CellUtil.cloneValue(cell)));
System.out.println(new String(CellUtil.cloneValue(cell2)));
System.out.println(new String(CellUtil.cloneValue(cell3)));
}
//扫描表
@Test
public void scan() throws Exception {
Scan scan=new Scan();
String phoneNn="18692892084";
String startRow=phoneNn+"_"+(Long.MAX_VALUE-(data.parse("20180301000000").getTime()));
String stoptRow=phoneNn+"_"+(Long.MAX_VALUE-(data.parse("20180201000000").getTime()));
scan.setStartRow(startRow.getBytes());
scan.setStopRow(stoptRow.getBytes());
ResultScanner resultScanner =hTable.getScanner(scan);
for(Result result:resultScanner){
System.out.println(new String(CellUtil.cloneValue(result.getColumnLatestCell("cf".getBytes(),"dum".getBytes()))));
}
}
SimpleDateFormat data=new SimpleDateFormat("yyyyMMddHHmmss");
//批量插入测试数据
@Test
public void insertDB2() throws ParseException, InterruptedIOException, RetriesExhaustedWithDetailsException {
List<Put> putList=new ArrayList<Put>();
for (int i = 0; i <10 ; i++) {
String PhoneNum=getPhoneNum("186");
for (int j = 0; j <100 ; j++) {
String dum=getPhoneNum("157");
String length=r.nextInt(99)+"";
String type=r.nextInt(2)+"";
String datastr=getDate("2018");
String rowkey=PhoneNum+"_"+(Long.MAX_VALUE-(data.parse(datastr).getTime()));
Put put=new Put(rowkey.getBytes());
put.add("cf".getBytes(),"dum".getBytes(),dum.getBytes());
put.add("cf".getBytes(),"length".getBytes(),length.getBytes());
put.add("cf".getBytes(),"type".getBytes(),type.getBytes());
put.add("cf".getBytes(),"datastr".getBytes(),datastr.getBytes());
putList.add(put);
}
}
hTable.put(putList);
}
@Test
public void scan2() throws Exception {
FilterList filterList=new FilterList(FilterList.Operator.MUST_PASS_ALL);
PrefixFilter prefixFilter=new PrefixFilter("18692892084".getBytes());
SingleColumnValueFilter filter=new SingleColumnValueFilter(
"cf".getBytes(),
"type".getBytes(),
CompareFilter.CompareOp.EQUAL,
"1".getBytes());
filterList.addFilter(prefixFilter);
filterList.addFilter(filter);
Scan scan=new Scan();
scan.setFilter(filterList);
String phoneNn="18692892084";
String startRow=phoneNn+"_"+(Long.MAX_VALUE-(data.parse("20180301000000").getTime()));
String stoptRow=phoneNn+"_"+(Long.MAX_VALUE-(data.parse("20180201000000").getTime()));
scan.setStartRow(startRow.getBytes());
scan.setStopRow(stoptRow.getBytes());
ResultScanner resultScanner =hTable.getScanner(scan);
for(Result result:resultScanner){
System.out.println(new String(CellUtil.cloneValue(result.getColumnLatestCell("cf".getBytes(),"dum".getBytes()))));
System.out.println(new String(CellUtil.cloneValue(result.getColumnLatestCell("cf".getBytes(),"type".getBytes()))));
}
}
private String getDate(String s) {
return s+String.format("%02d%02d%02d%02d%02d",new Object[]{r.nextInt(12)+1,r.nextInt(31)+1,r.nextInt(24),r.nextInt(60),r.nextInt(60)});
}
Random r=new Random();
private String getPhoneNum(String s) {
return s+String.format("%08d",r.nextInt(99999999));
}
@After
public void destory() throws Exception{
if(admin!=null) {
admin.close();
}
}
}