HbaseApi.java
package com.carssun.hbase.api;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.junit.Before;
import org.junit.Test;
import com.carssun.printUtil.HBasePrintUtil;
public class HbaseApi {
private static final String ZK_CONNECT_KEY="hbase.zookeeper.quorum";
private static final String ZK_CONNECT_VALUE="hadoop02:2199,hadoop03:2199,hadoop04:2199,hadoop05:2199";
private static Table hTable=null;
public static void main(String[] args) {
}
private static Connection conn=null;
private static Admin admin=null;
@Before
public void init() throws Exception {
Configuration conf=HBaseConfiguration.create();
conf.set(ZK_CONNECT_KEY, ZK_CONNECT_VALUE);
//獲取連接
conn=ConnectionFactory.createConnection(conf);
admin=conn.getAdmin();
TableName tn=TableName.valueOf("Hbase1");//需要進行增刪操作的表
hTable=conn.getTable(tn);
}
@Test
public void createTable() throws Exception {
TableName tn=TableName.valueOf("Hbase1");
HTableDescriptor htd=new HTableDescriptor(tn);
String familyName="base_info";
HColumnDescriptor family=new HColumnDescriptor(familyName);
htd.addFamily(family);
admin.createTable(htd);
//判斷是否創建成功
if(admin.tableExists(tn)){
System.out.println("創建陳功");
}
else{
System.out.println("創建失敗");
}
}
//列舉表的名字和簇名
@Test
public void listTable() throws Exception {
HTableDescriptor[] listTables = admin.listTables();
for(HTableDescriptor htd:listTables){
HColumnDescriptor[] families = htd.getColumnFamilies();
for(HColumnDescriptor hc:families){
System.out.println(htd.getTableName()+"\t"+new String(hc.getName()));
}
}
}
//刪除表需要先把表diasable
@Test
public void deleteTable() throws Exception {
TableName tb=TableName.valueOf("Hbase1");
//判斷是否是disable狀態
boolean tableEnabled = admin.isTableEnabled(tb);
//
if(tableEnabled){
admin.disableTable(tb);
}
admin.deleteTable(tb);
if(admin.tableExists(tb)){
System.out.println("刪除失敗");
}
else{
System.out.println("刪除成功");
}
}
/**插入數據 表示往某張表中插入數據
* 需要一個表對象
* @throws Exception
*
* */
//批量添加
@Test
public void putData() throws Exception{
Put put=new Put("rk03".getBytes());
byte[] family="base_info".getBytes();//簇
byte[] qualifier="qualifier".getBytes();
byte[] value="yy".getBytes();
put.addColumn(family, qualifier, value);
Put put1=new Put("rk02".getBytes());
byte[] family1="base_info".getBytes();//簇
byte[] qualifier1="xxx".getBytes();
byte[] value1="yy".getBytes();
put1.addColumn(family1, qualifier1, value1);
Put put2=new Put("rk01".getBytes());
byte[] family2="base_info".getBytes();//簇
byte[] qualifier2="jajaja".getBytes();
byte[] value2="asdsa".getBytes();
put2.addColumn(family2, qualifier2, value2);
Put put3=new Put("rk01".getBytes());
byte[] family3="base_info".getBytes();//簇
byte[] qualifier3="jajaja44".getBytes();
byte[] value3="asdsa".getBytes();
put3.addColumn(family3, qualifier3, value3);
Put put4=new Put("rk01".getBytes());
byte[] family4="base_info".getBytes();//簇
byte[] qualifier4="jajaja1".getBytes();
byte[] value4="asdsa".getBytes();
put4.addColumn(family4, qualifier4, value4);
Put put5=new Put("rk01".getBytes());
byte[] family5="base_info".getBytes();//簇
byte[] qualifier5="jajaja2".getBytes();
byte[] value5="asdsa".getBytes();
put5.addColumn(family5, qualifier5, value5);
List<Put> puts=new ArrayList<>();
puts.add(put);
puts.add(put1);
puts.add(put2);
puts.add(put3);
puts.add(put4);
puts.add(put5);
puts.add(put1);
hTable.put(puts);
}
@Test
public void delete() throws Exception{
Delete delete=new Delete("rk02".getBytes());//整個rowkey刪掉
delete.addColumn("base_info".getBytes(),"xxx".getBytes());
hTable.delete(delete);
}
@Test
public void getData() throws Exception{
Get get=new Get("rk03".getBytes());
Result result = hTable.get(get);
List<Cell> listCells = result.listCells();
//下面是hbaseAPI的原生方法
/*for(Cell c:listCells){
String family=Bytes.toString(c.getFamily());
String qualify=Bytes.toString(c.getQualifier());
String value=Bytes.toString(c.getValue());
long timestamp=c.getTimestamp();
System.out.println(family+' '+qualify+" "+value+" "+timestamp);
}*/
//使用老師封裝好的方法打印
HBasePrintUtil.printResult(result);
}
@Test
public void getResultScanner() throws Exception{
Scan scan=new Scan();
//scan.addColumn("base_info".getBytes(), "xxx".getBytes());
//scan.addColumn("base_info".getBytes(), "jajaja".getBytes());
byte[] startRow="rk01".getBytes();
scan.setStartRow(startRow);
byte[] stopRow="rk03".getBytes();
scan.setStopRow(stopRow);
ResultScanner scanner=hTable.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
}
接下來是封裝好的打印數據的方法,無論是resultScanner還是Result還是Cell最後都轉換成Cell對象輸出
HbasePrintUtil.hava
package com.carssun.printUtil;
import java.util.List;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.util.Bytes;
public class HBasePrintUtil {
public static void printResultScanner(ResultScanner resultScann) {
for (Result result : resultScann) {
printResult(result);
}
}
public static void printResult(Result result) {
List<Cell> cells = result.listCells();
for (int i = 0; i < cells.size(); i++) {
Cell cell = cells.get(i);
printCell(cell);
}
}
public static void printCell(Cell cell) {
System.out.println(Bytes.toString(cell.getRow()) + "\t" + Bytes.toString(cell.getFamily()) + "\t" + Bytes.toString(cell.getQualifier())
+ "\t" + Bytes.toString(cell.getValue()) + "\t" + cell.getTimestamp());
}
public static void printKeyValye(KeyValue kv) {
System.out.println(Bytes.toString(kv.getRow()) + "\t" + Bytes.toString(kv.getFamily()) + "\t" + Bytes.toString(kv.getQualifier()) + "\t"
+ Bytes.toString(kv.getValue()) + "\t" + kv.getTimestamp());
}
}