HBase版本:1.2.6
1. HBaseUtil.java
import java.io.IOException;
import java.util.Date;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
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.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.HBaseAdmin;
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 com.aura.hbase.utils.HBasePrintUtil;
/**
* HBase基础的CRUD操作
*/
public class HbaseUtil {
private static final String ZOOKEEPER_LIST = "node01:2181,node02:2181,node03:2181";
private static Configuration conf = HBaseConfiguration.create();
private static Connection conn;
private static HBaseAdmin admin;
private static HTable table;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", ZOOKEEPER_LIST);
try {
conn = ConnectionFactory.createConnection(conf);
admin = (HBaseAdmin) conn.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static HTable getTable(String tableName) throws Exception {
return (HTable) conn.getTable(TableName.valueOf(tableName));
}
public static boolean isTableExist(String tableName) throws Exception {
return admin.tableExists(TableName.valueOf(tableName));
}
public static void disableTable(String tableName) throws Exception {
admin.disableTable(TableName.valueOf(tableName));
}
public static void enableTable(String tableName) throws Exception {
admin.enableTable(TableName.valueOf(tableName));
}
public static boolean isTableDisabled(String tableName) throws Exception {
return admin.isTableDisabled(TableName.valueOf(tableName));
}
public static boolean isTableEnabled(String tableName) throws Exception {
return admin.isTableEnabled(TableName.valueOf(tableName));
}
public static void getAllTables() throws Exception {
// 方法一
/*
TableName[] tables = admin.listTableNames();
for(TableName table : tables) {
System.out.println(table.getNameAsString());
}
*/
// 方法二
HTableDescriptor[] descs = admin.listTables();
for(HTableDescriptor desc : descs) {
System.out.println(desc.getNameAsString());
}
}
public static void createTable(String tableName, String[] family) throws Exception {
if(isTableExist(tableName)) {
dropTable(tableName);
}
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
if(family != null && family.length > 0) {
for(String cf :family) {
HColumnDescriptor hcd = new HColumnDescriptor(cf);
desc.addFamily(hcd);
}
}
admin.createTable(desc);
}
public static void descTable(String tableName) throws Exception {
table = getTable(tableName);
HTableDescriptor desc = table.getTableDescriptor();
HColumnDescriptor[] columnDescs = desc.getColumnFamilies();
HBasePrintUtil.printHColumnDescriptors(columnDescs);
}
public static void dropTable(String tableName) throws Exception {
if(isTableDisabled(tableName)) {
admin.deleteTable(TableName.valueOf(tableName));
}else {
disableTable(tableName);
admin.deleteTable(TableName.valueOf(tableName));
}
}
/*
* 从原表中获取的列不能修改,只能再创建一个新表
* 方法还有一些问题...
*/
public void modifyTable(String tableName, String[] addColumn, String[] removeColumn) throws Exception {
/*
table = getTable(tableName);
HTableDescriptor newDesc = new HTableDescriptor(TableName.valueOf(tableName));
HTableDescriptor oldDesc = table.getTableDescriptor();
HColumnDescriptor[] columnFamilies = oldDesc.getColumnFamilies();
for(String column : addColumn) {
newDesc.addFamily(new HColumnDescriptor(column));
}
for(String column : removeColumn) {
newDesc.removeFamily(Bytes.toBytes(column));
}
admin.modifyTable(tableName, newDesc);
*/
}
public static void putData(String tableName, String rowKey, String familyName, String columnName, String value)
throws Exception {
putData(tableName, rowKey, familyName, columnName, value, new Date().getTime());
}
public static void putData(String tableName, String rowKey, String familyName, String columnName, String value,
long timestamp) throws Exception {
table = getTable(tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), timestamp, Bytes.toBytes(value));
table.put(put);
}
public static void putData(String tableName, Put put) throws Exception {
table = getTable(tableName);
table.put(put);
}
public static void putData(String tableName, List<Put> putList) throws Exception {
table = getTable(tableName);
table.put(putList);
}
public static Result getResult(String tableName, String rowKey) throws Exception {
table = getTable(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
return table.get(get);
}
public static Result getResult(String tableName, String rowKey, String familyName) throws Exception {
table = getTable(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
get.addFamily(Bytes.toBytes(familyName));
return table.get(get);
}
public static Result getResult(String tableName, String rowKey, String familyName, String columnName) throws Exception {
table = getTable(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
return table.get(get);
}
public static Result getResultByVersion(String tableName, String rowKey, String familyName, String columnName,
int versions) throws Exception {
table = getTable(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
get.setMaxVersions(versions);
return table.get(get);
}
public static ResultScanner getResultScann(String tableName) throws Exception {
table = getTable(tableName);
Scan scan = new Scan();
return table.getScanner(scan);
}
public static void deleteColumn(String tableName, String rowKey) throws Exception {
table = getTable(tableName);
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
}
public static void deleteColumn(String tableName, String rowKey, String falilyName) throws Exception {
table = getTable(tableName);
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addFamily(Bytes.toBytes(falilyName));
table.delete(delete);
}
public static void deleteColumn(String tableName, String rowKey, String falilyName, String columnName) throws Exception {
table = getTable(tableName);
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(Bytes.toBytes(falilyName), Bytes.toBytes(columnName));
table.delete(delete);
}
}
2. HBasePrintUtil.java
package com.aura.hbase.utils;
import java.util.List;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.KeyValueUtil;
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(Cell cell : cells) {
printCell(cell);
}
}
public static void printCell(Cell cell) {
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell)) + "\t" +
Bytes.toString(CellUtil.cloneFamily(cell)) + "\t" +
Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t" +
cell.getTimestamp() + "\t" +
Bytes.toString(CellUtil.cloneValue(cell)));
}
public static void printHTableDescriptor(HTableDescriptor desc) {
System.out.println(
"Name:" + desc.getNameAsString() + "\n" +
"FlushPolicyClassName:" + desc.getFlushPolicyClassName() + "\n" +
"MaxFileSize:" + desc.getMaxFileSize() + "\n" +
"MemStoreFlushSize:" + desc.getMemStoreFlushSize() + "\n" +
"RegionReplication:" + desc.getRegionReplication() + "\n" +
"RegionSplitPolicyClassName:" + desc.getRegionSplitPolicyClassName() + "\n" +
"---------------------------------------------");
}
public static void printHColumnDescriptors(HColumnDescriptor[] columnDescs) {
for(HColumnDescriptor desc : columnDescs) {
printHColumnDescriptor(desc);
}
}
public static void printHColumnDescriptor(HColumnDescriptor desc) {
System.out.println(
"Name : " + desc.getNameAsString() + "\n" +
"BloomFilterType : " + desc.getBloomFilterType() + "\n" +
"MinVersions : " + desc.getMinVersions() + "\n" +
"MaxVersions : " + desc.getMaxVersions() + "\n" +
"InMemory : " + desc.isInMemory() + "\n" +
"DataBlockEncoding : " + desc.getDataBlockEncoding() + "\n" +
"TimeToLive : " + desc.getTimeToLive() + "\n" +
"Compression : " + desc.getCompression() + "\n" +
"BlockCacheEnabled : " + desc.isBlockCacheEnabled() + "\n" +
"Blocksize : " + desc.getBlocksize() + "\n" +
"---------------------------------------------");
}
}