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() + " ------------------");
}*/
}
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));//删除表
}
}
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() + " ------------------");
}*/
}
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));//删除表
}
}