#博学谷IT技术支持#
一、高级Shell操作
1. HBase的过滤器查询操作
格式:scan '表名',{FILTER=>"过滤器名称(比较运算符,比较表达式)"}
- 常见的过滤器
- rowkey过滤器
- RowFilter
- PrefixFilter
- 列族过滤器
- FamilyFilter
- 列名过滤器
- QualifierFilter
- 列值过滤器
- ValueFilter
- SingleColumnValueFilter
- SingleColumnValueExcludeFilter
- 其他过滤器
- PageFilter
- rowkey过滤器
- 比较器
- BinaryComparator
- BinaryPrefixComparator
- NullComparator
- SubstringComparator
相关示例
添加一条非rk前缀的数据
put 'test01','0001','f1:name','tianqi'
需求: 查询rowkey中以rk开头的数据
scan 'test01',{FILTER=>"PrefixFilter('rk')" }
添加一条非rk前缀的数据
put 'test01','0001','f1:name','tianqi'
需求: 查询rowkey中以rk开头的数据
scan 'test01',{FILTER=>"PrefixFilter('rk')" }
需求: 查询在name列对应值中包含字母 z 展示出来
scan 'test01',{FILTER=>"ValueFilter(=,'substring:z')" } 将值中含z的值获取出来
查询 name的值包含z的数据, 带有Exclude 表示是否去除查询列
scan 'test01',{FILTER=>"SingleColumnValueFilter('f1','name',=,'substring:z')" }
scan 'test01',{FILTER=>"SingleColumnValueExcludeFilter('f1','name',=,'substring:z')" }
2. whoami
显示HBase当前使用用户
3. exists
判断表是否存在
4. is_enabled和is_disabled
判断表是否启用和是否禁用
5. alter
修改表和列族信息
alter 'test01','delete'=>'f2'
二、Java API操作
2.1 添加一条数据
@Test
public void addDataTest02() throws Exception {
//1. 根据Hbase的连接工厂创建HBase的连接对象
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,node3:2181");
Connection hbaseConn = ConnectionFactory.createConnection(conf);
//2. 根据连接对象, 获取相关的管理对象: admin table
Table table = hbaseConn.getTable(TableName.valueOf("WATER_BILL"));
//3. 执行相关的操作: 添加数据
Put put = new Put("4944191".getBytes());
put.addColumn("C1".getBytes(),"NAME".getBytes(),"登卫红".getBytes());
put.addColumn("C1".getBytes(),"ADDRESS".getBytes(),"贵州省铜仁市德江县7单元267室".getBytes());
put.addColumn("C1".getBytes(),"SEX".getBytes(),"男".getBytes());
table.put(put);
// 4. 处理结果集: 非查询没有结果集
//5. 释放资源
table.close();
hbaseConn.close();
}
2.2 查询一条数据
@Test
public void getRowTest03() throws Exception{
// 3 执行相关的操作: 根据rowkey查询数据
Get get = new Get("4944191".getBytes());
Result result = table.get(get);
//4. 处理结果集
// 4.1 获取对应行所有的单元格信息
List<Cell> cellList = result.listCells();
// 4.2 遍历每一个单元格
for (Cell cell : cellList) {
// 4.3 从单元格中获取数据 -- rowkey
byte[] rowkeyBytes = CellUtil.cloneRow(cell);
String rowkey = new String(rowkeyBytes);
// 4.4 从单元格中获取数据 -- 列族
byte[] cloneFamily = CellUtil.cloneFamily(cell);
String family = new String(cloneFamily);
// 4.5 从单元格中获取数据 -- 列名
byte[] qualifierBtyes = CellUtil.cloneQualifier(cell);
String qualifier = new String(qualifierBtyes);
// 4.6 从单元格中获取数据 -- 列值
byte[] valueBytes = CellUtil.cloneValue(cell);
String value = new String(valueBytes);
// 4.7 合并数据
System.out.println("rowkey:"+rowkey +";列族为:"+family +";列名为:"+qualifier +";列值为:" + value);
}
}
2.3 基于scan的扫描
// 需求六: 查询 2020年 6月份的所有用户的用户量 : 日期: RECORD_DATE 用水量: NUM_USAGE
@Test
@SuppressWarnings("ALL")
public void scanTest06() throws Exception{
// 3- 执行相关的操作: 查询数据
// SQL语言: select 姓名,日期, 用水量 from xxx where 日期 between '2020-06-01' and '2020-06-30';
Scan scan = new Scan();
scan.setLimit(10);
// 3.1 设置过滤器
SingleColumnValueFilter filter1 = new SingleColumnValueFilter(
"C1".getBytes(),
"RECORD_DATE".getBytes(),
CompareOperator.GREATER_OR_EQUAL,
new BinaryComparator("2020-06-01".getBytes())
);
SingleColumnValueFilter filter2 = new SingleColumnValueFilter(
"C1".getBytes(),
"RECORD_DATE".getBytes(),
CompareOperator.LESS_OR_EQUAL,
new BinaryComparator("2020-06-30".getBytes())
);
FilterList filterList = new FilterList();
filterList.addFilter(filter1);
filterList.addFilter(filter2);
scan.setFilter(filterList);
//3.2 执行scan操作
ResultScanner results = table.getScanner(scan);
//4. 处理结果集
for (Result result : results) {
// 4.1 获取对应行所有的单元格信息
List<Cell> cellList = result.listCells();
// 4.2 遍历每一个单元格
for (Cell cell : cellList) {
// 从单元格中获取数据 -- 列名
byte[] qualifierBtyes = CellUtil.cloneQualifier(cell);
String qualifier = Bytes.toString(qualifierBtyes);
if("NAME".equalsIgnoreCase(qualifier) || "NUM_USAGE".equalsIgnoreCase(qualifier)|| "RECORD_DATE".equalsIgnoreCase(qualifier)){
// 从单元格中获取数据 -- rowkey
byte[] rowkeyBytes = CellUtil.cloneRow(cell);
String rowkey = Bytes.toString(rowkeyBytes);
// 从单元格中获取数据 -- 列族
byte[] cloneFamily = CellUtil.cloneFamily(cell);
String family = Bytes.toString(cloneFamily);
//从单元格中获取数据 -- 列值
byte[] valueBytes = CellUtil.cloneValue(cell);
Object value ;
if("NUM_USAGE".equalsIgnoreCase(qualifier)){
value = Bytes.toDouble(valueBytes);
}else {
value = Bytes.toString(valueBytes);
}
// 4.7 合并数据
System.out.println("rowkey:"+rowkey +";列族为:"+family +";列名为:"+qualifier +";列值为:" + value);
}
}
System.out.println("------------");
}
}