package com.bi.net;
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.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
/**
* Created by DengNi on 2016/6/18.
*/
public class HbaseJdbc {
public static Configuration conf = null;
public static TableName tab = TableName.valueOf("HbaseInWord".getBytes());
public static TableName hbaseInWord = TableName.valueOf("HbaseInWord");
public static Random ra = new Random();
/**
* 1 create table 需要 HbaseAdmin 类
* 2 数据的insert (put) ,update ,select by get or scan 都是通过HTable 类操作
*/
@Before
public void init() {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "bigdataspark:2181,bigdatacloud:2181,bigdatahadoop:2181");
}
@Test
public void createNewTableOnHbase() throws IOException {
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
if (hBaseAdmin.tableExists(tab)) {
hBaseAdmin.disableTable(tab);
hBaseAdmin.deleteTable(tab);
}
HTableDescriptor tableDesc = new HTableDescriptor(tab);
HColumnDescriptor colDesc = new HColumnDescriptor("cfOne");
colDesc.setBlockCacheEnabled(true);
colDesc.setBlocksize(1280);
colDesc.setMaxVersions(16);
tableDesc.addFamily(colDesc);
hBaseAdmin.createTable(tableDesc);
// base(main):015:0> list cmd check if the table is been create on HBase
}
@Test
public void insertIntoHbaseInWord() throws IOException {
HTable table = new HTable(conf, hbaseInWord);
List list = new ArrayList<Put>();
for (int i = 999; i >= 0; i--) {
Put putValue = new Put(String.valueOf(i).getBytes());
putValue.add("cfOne".getBytes(), "name".getBytes(), "zl".getBytes());
putValue.add("cfOne".getBytes(), "age".getBytes(), String.valueOf(i % 102).getBytes());
list.add(putValue);
}
table.put(list);
}
@Test
public void searchFromHbaseInWordByGet() throws IOException {
HTable table = new HTable(conf, hbaseInWord);
Get get = new Get("999".getBytes());
Result result = table.get(get);
Cell cell = result.getColumnLatestCell("cfOne".getBytes(), "name".getBytes());
byte[] bytes = CellUtil.cloneValue(cell);
System.out.println(new String(bytes, "UTF-8"));
}
@Test
public void searchByScan() throws IOException {
HTable table = new HTable(conf, hbaseInWord);
Scan scan = new Scan("900".getBytes(), "999".getBytes());
ResultScanner result = table.getScanner(scan);
Iterator<Result> iterator = result.iterator();
while (iterator.hasNext()) {
Result result1 = iterator.next();
Cell cell = result1.getColumnLatestCell("cfOne".getBytes(), "name".getBytes());
byte[] value = CellUtil.cloneValue(cell);
byte[] row = CellUtil.cloneRow(cell);
System.out.println(new String(row, "UTF-8") + " " + new String(value, "UTF-8"));
}
}
@Test
public void deleteOneRow() throws IOException {
HTable table = new HTable(conf, hbaseInWord);
Delete delete = new Delete("988".getBytes());
table.delete(delete);
}
/**
* A more complicated example
*/
@Test
public void insertPut() throws IOException {
HTable table1 = new HTable(conf, hbaseInWord);
List<Put> list = new ArrayList<Put>();
for (int i = 0; i < 10000; i++) {
Put put = new Put(generateRowKey("1531187321").getBytes());
put.add("cfOne".getBytes(),"addr".getBytes(),"Shanghai".getBytes());
put.add("cfOne".getBytes(),"type".getBytes(),String.valueOf(ra.nextInt(2)).getBytes());
put.add("cfOne".getBytes(),"phone".getBytes(),generatePhone("153").getBytes());
put.add("cfOne".getBytes(),"time".getBytes(),String.valueOf(ra.nextInt(500)).getBytes());
list.add(put);
}
table1.put(list);
}
private String generateRowKey(String s){
StringBuffer sb = new StringBuffer(s);
sb.append("_2016").append(String.format("%02d",ra.nextInt(12)+1))
.append(String.format("%02d",ra.nextInt(30)+1))
.append(String.format("%02d",ra.nextInt(24)))
.append(String.format("%02d",ra.nextInt(60)))
.append(String.format("%02d",ra.nextInt(60)));
return sb.toString();
}
private String generatePhone(String p){
StringBuffer sb = new StringBuffer(p);
sb.append(String.format("%08d",ra.nextInt(999999999)));
return sb.toString();
}
@Test
public void searchByScanInterval() throws IOException {
HTable table = new HTable(conf,hbaseInWord);
Scan scan = new Scan("1531187321_20160200000000".getBytes(),"1531187321_20160301000000".getBytes());
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while(iterator.hasNext()){
Result result = iterator.next();
Cell column = result.getColumnLatestCell("cfOne".getBytes(),"phone".getBytes());
byte[] row = CellUtil.cloneRow(column);
byte[] value = CellUtil.cloneValue(column);
System.out.println(new String(row) + " "+ new String(value));
}
}
@Test
public void findByContition() throws IOException {
HTable table = new HTable(conf,hbaseInWord);
Scan scan = new Scan();
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
PrefixFilter prefixFilter = new PrefixFilter("1531187321_201610".getBytes());
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("cfOne".getBytes(), "type".getBytes(), CompareFilter.CompareOp.EQUAL, "1".getBytes());
filterList.addFilter(prefixFilter);
filterList.addFilter(singleColumnValueFilter);
scan.setFilter(filterList);
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while(iterator.hasNext()){
Result result = iterator.next();
Cell columnLatestCell = result.getColumnLatestCell("cfOne".getBytes(),"phone".getBytes());
byte[] va = CellUtil.cloneValue(columnLatestCell);
byte[] row = CellUtil.cloneRow(columnLatestCell);
System.out.println(new String(row) +" "+new String(va));
}
}
}