HBase10.1基本操作(java代碼)

來源:互聯網
上載者:User

標籤: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代碼)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.