立即注册 登录
About云-梭伦科技 返回首页

langke93的个人空间 https://www.aboutyun.com/?1415 [收藏] [复制] [分享] [RSS]

日志

HBase1.0.1基本操作(java代码)

已有 823 次阅读2015-8-16 20:09 | java

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));//删除表
}
}

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

关闭

推荐上一条 /2 下一条