单列值过滤器(SingleColumnValueFilter)
用法一:单列值信息过滤器( SingleColumnValueFilter)
过滤出具体行记录
/**
* 通过具体列(col)的查询表相关记录
* @param tableName 表
* @param family 列族名
* @param column 列名/Quilifier
* @param col 列数据(根据需要的类型更改 int ,String long 等等)
*/
public static void selectRowByCol(String tableName, String family, String column, long col) {
Table table = null;
try {
table = initHbase().getTable(TableName.valueOf(tableName));
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
//hbase 存的是字节数组
SingleColumnValueFilter filter =
new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(column), CompareOperator.EQUAL, Bytes.toBytes(col));
filterList.addFilter(filter);
Scan scan = new Scan();
scan.setFilter(filterList);
scan.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));//一定要有
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
//r中有一行相关信息,需要什么可以取什么
for (Cell cell : r.rawCells()) {
System.out.println("Rowkey : " + Bytes.toString(r.getRow()) + "" +
" Familiy:Quilifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value : " + Bytes.toString(CellUtil.cloneValue(cell)) +
" Time : " + cell.getTimestamp());
}
}
table.close();//不要忘记释放资源
} catch (IOException e) {
e.printStackTrace();
}
}
用法二:单列值信息过滤器( SingleColumnValueFilter)进行区间过滤,
区间值可以是int,long,String等,是String 时一定要是数字串
/**
* 通过具体列信息区间(所给数据范围)查询相关行信息
* @param tableName 表
* @param family 列族名
* @param column 列名
* @param begin 区间开始
* @param end 区间结束
*/
public static void selectRowByZone(String tableName, String family, String column, int begin, int end) {
//类型转换
byte[] b_family = Bytes.toBytes(family);
byte[] b_column = Bytes.toBytes(column);
byte[] b_begin = Bytes.toBytes(begin);
byte[] b_end = Bytes.toBytes(end);
Table table = null;
try {
table = initHbase().getTable(TableName.valueOf(tableName));
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
//组成区间过滤
SingleColumnValueFilter filter1 =
new SingleColumnValueFilter(b_family, b_column, CompareOperator.GREATER_OR_EQUAL, b_begin);
SingleColumnValueFilter filter2 =
new SingleColumnValueFilter(b_family, b_column, CompareOperator.LESS_OR_EQUAL, b_end);
filterList.addFilter(filter1);
filterList.addFilter(filter2);
Scan scan = new Scan();
scan.setFilter(filterList);
scan.addColumn(b_family,b_column);//追加列族和列
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
//获取行键,也可以获取其他
String s = Bytes.toString(r.getRow());
//进行删除操作(根据自己的需求)
//Delete delete = new Delete(Bytes.toBytes(s));
//table.delete(delete);
}
table.close();//一定要释放资源
} catch (IOException e) {
e.printStackTrace();
}
}
个人用法:
根据列数据获取指定行记录,并返回行记录,生成文件存放本地
/**
* 根据列数据获取指定行记录,并返回行记录
* @param tableName 表
* @param family 列族名
* @param column 列名/Quilifier
* @param col 列数据
* @return infos 行信息
*/
public static List selectRowByClo(String tableName, String family, String column, long col) {
Table table = null;
List infos = null;//mac相关行信息
try {
infos = new ArrayList<>();
table = initHbase().getTable(TableName.valueOf(tableName));
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
SingleColumnValueFilter filter =
new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(column), CompareOperator.EQUAL, Bytes.toBytes(col));
filterList.addFilter(filter);
Scan scan = new Scan();
scan.setFilter(filterList);
scan.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));//确保查询对
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
for (Cell cell : r.rawCells()) {
String info = "Rowkey : " + Bytes.toString(r.getRow()) + "" +
" Familiy:Quilifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value : " + Bytes.toString(CellUtil.cloneValue(cell)) +
" Time : " + cell.getTimestamp()+"\n";
infos.add(info);
}
}
table.close();
} catch (IOException e) {
e.printStackTrace();
}
return infos;
}
/**
* 信息打包成文件
* @param info
*/
public static void packageInfo(List infos){
//1. 文件夹的路径 文件名
String directory = "D:\\info";
String filename = System.currentTimeMillis()+".txt";
//2. 创建文件夹对象 创建文件对象
File file = new File(directory);
//如果文件夹不存在 创建一个空的文件夹
if (!file.exists()) {
file.mkdirs();
}
File file2 = new File(directory, filename);
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//3.写入数据
FileOutputStream fos = null;
Pattern p= Pattern.compile("[\\[,\\] ]");
Matcher matcher = p.matcher(String.valueOf(infos));
String in = matcher.replaceAll("");
try {
fos = new FileOutputStream(directory + "\\" + filename);
//开始写
byte[] bytes = in.getBytes();
fos.write(bytes);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}