package com.bpf.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
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.util.Bytes;
import org.apache.hadoop.io.IOUtils;
/*
* CRUD
* */
@SuppressWarnings("deprecation")
public class HBaseOperation {
public static HTable getHtableByTablename(String tableName) throws Exception {
Configuration conf = HBaseConfiguration.create();
//get table instance
HTable table = new HTable(conf, tableName);
return table;
}
public static void getData() throws Exception {
String tableName = "user";
HTable table = getHtableByTablename(tableName);
//create Get with rowkey
Get get = new Get(Bytes.toBytes("1001"));
//add column
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
//get data
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(
Bytes.toString(CellUtil.cloneFamily(cell)) + ":" +
Bytes.toString(CellUtil.cloneQualifier(cell)) + ":" +
Bytes.toString(CellUtil.cloneValue(cell))
);
}
table.close();
}
public static void putData() throws Exception {
String tableName = "user";
HTable table = getHtableByTablename(tableName);
// put data
Put put = new Put(Bytes.toBytes("1003"));
//add columns
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("calvin"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(25));
put.add(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("UK"));
table.put(put);
table.close();
}
public static void deleteData() throws Exception {
String tableName = "user";
HTable table = getHtableByTablename(tableName);
Delete delete = new Delete(Bytes.toBytes("1003"));
//刪除某列
//列有多版本,deleteColumn刪除最新的版本 deleteColumns刪除所有版本
//delete.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("address"));
//刪除整行(一個列族一個列族刪)
delete.deleteFamily(Bytes.toBytes("info"));
table.delete(delete);
table.close();
}
public static void scanData() {
String tableName = "user";
HTable table = null;
ResultScanner scanner = null;
try {
table = getHtableByTablename(tableName);
Scan scan = new Scan();
//range
scan.setStartRow(Bytes.toBytes("1001"));
scan.setStopRow(Bytes.toBytes("1002"));
//也可以在實例化時構造函數中指定
//Scan scan2 = new Scan(startRow, stopRow);
//添加scan的條件
//scan.addFamily(family);
//scan.addColumn(family, qualifier)
//filter---->PageFilter分頁,PrefixFilter前綴
//scan.setFilter(filter)
//scan.setCacheBlocks(cacheBlocks);
//scan.setCaching(caching);
scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
System.out.println(result);
System.out.println("----------------------------");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
IOUtils.closeStream(scanner);
IOUtils.closeStream(table);
}
}
public static void main(String[] args) {
}
}