hbase之前版本有些api已经deprecated了,下面是1.2的api,没有deprecated
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.apache.hadoop.hbase.util.Bytes;
public class HBaseUtils {
private static Configuration conf = HBaseConfiguration.create();
private static ExecutorService pool=Executors.newScheduledThreadPool(20);
private static Connection connection = null;
private static HBaseUtils instance=null;
private HBaseUtils(){
if(connection==null){
try {
connection=ConnectionFactory.createConnection(conf, pool);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static synchronized HBaseUtils getInstance(){
if(instance==null){
instance=new HBaseUtils();
}
return instance;
}
/**
* 创建表
* @throws IOException
*/
public void createTable(String tableName, String[] columns) throws IOException {
Admin admin = connection.getAdmin();
TableName name=TableName.valueOf(tableName);
if (admin.tableExists(name)) {
admin.disableTable(name);
admin.deleteTable(name);
} else {
HTableDescriptor desc =admin.getTableDescriptor(name);
for (String column : columns) {
desc.addFamily(new HColumnDescriptor(column));
}
admin.createTable(desc);
}
}
/**
* 插入一行记录
* @param tablename
* 表名
* @param row
* 行名称
* @param columnFamily
* 列族名
* @param columns
* (列族名:column)组合成列名
* @param values
* 行与列确定的值
*/
public void insertRecord(String tableName, String row, String columnFamily, String[] columns, String[] values) {
try {
TableName name=TableName.valueOf(tableName);
Table table = connection.getTable(name);
Put put = new Put(Bytes.toBytes(row));
for (int i = 0; i < columns.length; i++) {
put.addColumn(Bytes.toBytes(columnFamily),
Bytes.toBytes(String.valueOf(columns[i])),
Bytes.toBytes(values[i]));
table.put(put);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除一行记录
* @param tablename
* 表名
* @param rowkey
* 行名
* @throws IOException
*/
public void deleteRow(String tablename, String rowkey) throws IOException {
TableName name=TableName.valueOf(tablename);
Table table = connection.getTable(name);
List<Delete> list = new ArrayList<Delete>();
Delete d1 = new Delete(rowkey.getBytes());
list.add(d1);
table.delete(list);
}
/**
* 查找一行记录
* @param tablename
* 表名
* @param rowkey
* 行名
*/
public static void selectRow(String tablename, String rowKey)
throws IOException {
TableName name=TableName.valueOf(tablename);
Table table = connection.getTable(name);
Get g = new Get(rowKey.getBytes());
Result rs = table.get(g);
for (Cell cell : rs.rawCells()) {
System.out.print(new String(cell.getRowArray()) + " ");
System.out.print(new String(cell.getFamilyArray()) + ":");
System.out.print(new String(cell.getQualifierArray()) + " ");
System.out.print(cell.getTimestamp() + " ");
System.out.println(new String(cell.getValueArray()));
}
}
/**
* 查询表中所有行
* @param tablename
*/
public List scanAllRecord(String tablename) {
try {
List entityList=new ArrayList();
TableName name=TableName.valueOf(tablename);
Table table = connection.getTable(name);
Scan s = new Scan();
ResultScanner rs = table.getScanner(s);
for(Result result : rs){
for (Cell cell : result.rawCells()) {
HBaseBaseEntity entity=new HBaseBaseEntity();
entity.setRowKey(new String(CellUtil.cloneRow(cell)));
entity.setFamily(new String(CellUtil.cloneFamily(cell)));
entity.setQualifier(new String(CellUtil.cloneQualifier(cell)));
entity.setTimestamp(cell.getTimestamp());
entity.setValue(CellUtil.cloneValue(cell));
entityList.add(entity);
}
}
return entityList;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 删除表操作
* @param tablename
* @throws IOException
*/
public void deleteTable(String tablename) throws IOException {
try {
TableName name=TableName.valueOf(tablename);
if(admin.tableExists(name)){
admin.disableTable(name);
admin.deleteTable(name);
}
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
}
}
}