標籤:hbase1.0 java hbase
public class HQuery {
private static ConnHBase connHbase=new ConnHBase();
/***************建表****************************/
public void creatTable(String TBname,String...colFamily) throws Exception {
TableName tableName = TableName.valueOf(TBname);// 獲得表名稱
/*表描述器*/
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
for(String cols:colFamily){
tableDesc.addFamily(new HColumnDescriptor(cols));// 添加列族
}
/*建立管理員*/
Admin admin = connHbase.getConnect().getAdmin();
/*建立一個表*/
admin.createTable(tableDesc);
}
/***************插入和更新資料****************************/
public void createCell(String tableName,String colFamily,String rowKey,String column,String value) throws IOException {
Table table = connHbase.getConnect().getTable(TableName.valueOf(tableName));//表執行個體
HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies();//擷取表中全部的列族
/*插入器*/
Put put = new Put(Bytes.toBytes(rowKey));// 設定行號,RowKey
/*遍曆列族,找到匹配的列族*/
for (int i = 0; i < columnFamilies.length; i++) {
String familyName = columnFamilies[i].getNameAsString(); // 擷取列族名
// 如果是指定列族
if (familyName.equals(colFamily)) {
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(column), Bytes.toBytes(value));// 寫入
}
}
table.put(put); // 運行寫入
}
/************查詢單元格資料***********/
public List<Cell> getRow(String tableName,String rowKey) throws IOException {
Table table = connHbase.getConnect().getTable(TableName.valueOf(tableName));//表執行個體
Get get = new Get(Bytes.toBytes(rowKey));//查詢指定行
Result result = table.get(get);//執行查詢
List<Cell> listCells = result.listCells();//指定行、全部列族的全部列
/*遍曆單元格*/
/* for (Cell cell : listCells) {
System.out.println("列 族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列 名:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("列 值:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("時間戳記:" + cell.getTimestamp());
list.add(cell)
}*/
return listCells;
}
/***********全表掃描************/
public List<Cell> scanTable(String tableName) throws IOException {
List<Cell> cells=null;
Table table = connHbase.getConnect().getTable(TableName.valueOf(tableName));//表執行個體
ResultScanner resultScanner = table.getScanner(new Scan()); //針對全表的查詢器
java.util.Iterator<Result> results = resultScanner.iterator();// 結果迭代器
while(results.hasNext()) {
Result result = results.next();
cells = result.listCells();
/* for(Cell cell : cells) {
System.out.println("列 族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列 名:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("列 值:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("時間戳記:" + cell.getTimestamp() + "\n------------------");
}*/
}
Scan scan =new Scan();
resultScanner.close();// 關閉資源
return cells;
}
/*********刪除單元格*********/
public void deleteCell(String colFamily ,String column ,String rowKey ,String tableName) throws IOException {
Table table = connHbase.getConnect().getTable(TableName.valueOf(tableName));//表執行個體
Delete del = new Delete(Bytes.toBytes(rowKey));// 操作指定行鍵的刪除器
del.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(column));// 指定列族的列
table.delete(del);// 執行刪除
}
/*********刪除指定行***************/
public void deleteRow(String tableName,String rowKey) throws IOException {
Table table = connHbase.getConnect().getTable(TableName.valueOf(tableName));//表執行個體
Delete deleterow = new Delete(Bytes.toBytes(rowKey));
table.delete(deleterow);
}
/***********刪除表**************/
public void deleteTable(String tableName ) throws IOException {
Admin admin = connHbase.getConnect().getAdmin();
admin.disableTable(TableName.valueOf(tableName)); // 關閉表
admin.deleteTable(TableName.valueOf(tableName));//刪除表
}
}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
HBase10.1基本操作(java代碼)