/**
* hbase 根据rowkey 条件过滤
*
* @param hbaseFimilay hbase 列族
* @param hbaseColumn hbase 列
* @param userList 用户列表
* @throws Exception 异常类
*/
public Map<String, HashSet<String>> scanWithFilterByRowKey(String hbaseFimilay, String hbaseColumn, List<String> userList, String taskId) {
Map<String, HashSet<String>> date2Users = new HashMap<>();
try {
for (int i = 0; i < userList.size(); i++) {
Scan scan = new Scan();
FilterList filterList = new FilterList();
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(userList.get(i).getBytes()));
filterList.addFilter(filter);
scan.setFilter(filterList);
scan.addColumn(hbaseFimilay.getBytes(), hbaseColumn.getBytes());
ResultScanner scanner = table.getScanner(scan);
HashSet<String> dateCollection = new HashSet<>();
for (Result result : scanner) {
final byte[] value = result.getValue(hbaseFimilay.getBytes(), hbaseColumn.getBytes());
dateCollection.add(new String(value));
}
date2Users.put(userList.get(i), dateCollection);
}
table.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
return date2Users;
}
/**
* 模糊查rowkeys + 删除rowKey数据
*
* @param hbaseFimilay hbase family
* @param hbaseColumn hbase column
* @param userList 用户列表
* @throws Exception 异常类
*/
public void scanFuzzyMatchingByRowKey(String hbaseFimilay, String hbaseColumn, List<String> userList) throws Exception {
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf(tablename));
// 模糊查rowkey集合
HashSet<byte[]> o = new HashSet<>();
for (int i = 0; i < userList.size(); i++) {
Scan scan = new Scan();
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("*_" + userList.get(i) + "_*"));
scan.setFilter(rowFilter);
scan.addColumn(hbaseFimilay.getBytes(), hbaseColumn.getBytes());
ResultScanner scanner = table.getScanner(scan);
while (scanner.iterator().hasNext()) {
byte[] row = scanner.next().getRow();
o.add(row);
}
}
// 删除集合rowkey行
Iterator<byte[]> iterator = o.iterator();
while (iterator.hasNext()) {
Delete delete = new Delete(iterator.next());
table.delete(delete);
}
table.close();
connection.close();
}
/**
* 删除表中指定的rowkey列,并非所有的列
*
* @param hbaseTableName hbase 表名
* @param hbaseFamilyName hbase family
* @param columnList hbase column
* @param rowKey hbase rowkey
* @throws Exception 异常类
*/
public void deleteColumnsByRowkey(String hbaseTableName, String hbaseFamilyName, List<String> columnList, String rowKey) throws Exception {
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf(hbaseTableName));
for (String s : columnList) {
Delete delete = new Delete(rowKey.getBytes());
table.delete(delete);
}
table.close();
connection.close();
}
Hbase查询Java实现
于 2022-01-04 08:56:59 首次发布