大数据全系列 教程
1869个小节阅读:467.6k
目录
408考研
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
/**scan扫描表数据
* @param tableName:表的名称
* @param startRow:起始的rowkey 包含
* @param stopRow:结束的rowkey 不包含
*/
public static void scanTableData(String tableName,String startRow,String stopRow) throws IOException {
//1.参数的校验省略
//2.获取table对象
Table table = connection.getTable(TableName.valueOf(tableName));
//3.创建Scan对象
Scan scan = new Scan();
//4.设置查询的范围
//包含startRow对应的数据
scan.setStartRow(Bytes.toBytes(startRow));
//不包含stopRow
scan.setStopRow(Bytes.toBytes(stopRow));
//5.执行查询操作
ResultScanner resultScanner = table.getScanner(scan);
//6.解析resultScanner
for(Result result:resultScanner){
//7.解析result对象
for(Cell cell:result.rawCells()){
System.out.println(
"rowkey:"+Bytes.toString(CellUtil.cloneRow(cell))+
",family:"+Bytes.toString(CellUtil.cloneFamily(cell))+
",qualifier:"+Bytes.toString(CellUtil.cloneQualifier(cell))+
",value:"+Bytes.toString(CellUtil.cloneValue(cell))
);
}
}
//8.关闭资源
table.close();
connection.close();
}
public static void scanTableData2(String tableName,String startRow,String stopRow) throws IOException {
//1.参数的校验省略
//2.获取table对象
Table table = connection.getTable(TableName.valueOf(tableName));
//3.创建Scan对象
Scan scan = new Scan();
//4.设置查询的范围
//包含startRow对应的数据
scan.withStartRow(Bytes.toBytes(startRow));
//默认是不包含stopRow
//scan.withStopRow(Bytes.toBytes(stopRow));
//第二个参数true包含stopRow,false不包含stopRow
scan.withStopRow(Bytes.toBytes(stopRow),true);
//5.执行查询操作
ResultScanner resultScanner = table.getScanner(scan);
//6.解析resultScanner
for(Result result:resultScanner){
//7.解析result对象
for(Cell cell:result.rawCells()){
System.out.println(
"rowkey:"+Bytes.toString(CellUtil.cloneRow(cell))+
",family:"+Bytes.toString(CellUtil.cloneFamily(cell))+
",qualifier:"+Bytes.toString(CellUtil.cloneQualifier(cell))+
",value:"+Bytes.toString(CellUtil.cloneValue(cell))
);
}
}
//8.关闭资源
table.close();
connection.close();
}
//main方法中的测试:
//scan测试
//以下三个添加数据的操作要分别执行
//putCellData("psn","cf","name",Bytes.toBytes("rk002"),Bytes.toBytes("diaosi"));
//putCellData("psn","cf","name",Bytes.toBytes("rk003"),Bytes.toBytes("baifumei"));
//putCellData("psn","cf","name",Bytes.toBytes("rk004"),Bytes.toBytes("aituocuo"));
//scanTableData("psn","rk001","rk003");
//重载后的scan测试
scanTableData2("psn","rk001","rk003");