importorg.apache.hadoop.hbase.Cell;importorg.apache.hadoop.hbase.CellUtil;importorg.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;importorg.apache.hadoop.hbase.util.Bytes;importjava.io.IOException;/*** Created by VULCAN on 2020/3/21*/
public classDataUtil {//返回指定表名的Table对象
public static Table getTableByName(Connection conn,String nsName,String tableName) throwsIOException {
TableName tn=TableUtil.checkTableName(tableName, nsName);if (tn == null){return null;
}returnconn.getTable(tn);
}//put put 表名 rowkey 列族名:列名 值 [时间戳] put是向某一行的某列添加一个value
public static void put(Connection conn,String nsName,String tableName,String rowkey,String cfName,String cqName,String value) throwsIOException {
Table table=getTableByName(conn, nsName, tableName);if (table==null){return;
}//使用Table进行put操作//put对象需要基于rowkey构建
Put put = newPut(Bytes.toBytes(rowkey));//向put对象中封装参数
put.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(cqName),Bytes.toBytes(value));//.addColumn().addColumn()
table.put(put);
table.close();
}//get 查询一行的内容 get 表名 rowkey [列名]
public static Result get(Connection conn,String nsName,String tableName,String rowkey) throwsIOException {//获取表对象
Table table =getTableByName(conn, nsName, tableName);if (table==null){return null;
}//构建get对象
Get get = newGet(Bytes.toBytes(rowkey));//向get中,封装查询的参数//只查询某个列//get.addColumn()//只查某个列族//get.addFamily()//只查某个版本//get.setTimeStamp()//设置查询的最大版本数量
///get.setMaxVersions()
Result result=table.get(get);
table.close();returnresult;
}//遍历单行Result中的数据
public static voidparseResult(Result result){if (result !=null){//遍历这一行内容所有的Cell
Cell[] cells =result.rawCells();for(Cell cell : cells) {//利用Cell Util的clonexxx来获取相应的值
System.out.println("Row:"+Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("Family:"+Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("Qualifier:"+Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("Value:"+Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("-----------------------------------------");
}
}
}//scan
public static void scan(Connection conn,String nsName,String tableName) throwsIOException {//获取表对象
Table table =getTableByName(conn, nsName, tableName);if (table==null){return;
}//scan代表一个扫描器
Scan scan = newScan();//在scan中设置要扫描的参数//只扫某些列//scan.addColumn()//只扫某个列族//scan.addFamily()//指定起始行和终止行//scan.setStartRow()//scan.setStopRow()//专家设置 ,{RAW=>TRUE,VERSIONS=>10}//scan.setMaxVersions(10);//scan.setRaw(true);
ResultScanner scanner=table.getScanner(scan);for(Result result : scanner) {
parseResult(result);
}
table.close();
}//delete delete 表名 rowkey [列名] [ts]
public static void delete(Connection conn, String nsName, String tableName, String rowkey) throwsIOException {//获取表对象
Table table =getTableByName(conn, nsName, tableName);if (table==null){return;
}//删除一整行 为当前行的每个列族都添加一条 列族名: ,type=DeleteFamily的记录
Delete delete = newDelete(Bytes.toBytes(rowkey));//删除某个列族 为当前行的指定列族都添加一条 列族名: ,type=DeleteFamily的记录//delete.addFamily(Bytes.toBytes("cf1"));//删除某个列 为指定列添加一条 列族名:列名,type=Delete的记录 删除当前列最新版本的cell,如果//有历史版本,历史版本是可见的//delete.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes("name"));//删除指定列的所有版本 为指定的列添加一条 列族名:列名, type=DeleteColumn的记录
delete.addColumns(Bytes.toBytes("cf1"),Bytes.toBytes("name"));
table.delete(delete);
table.close();
}
}