Hbase常用操作记录
创建表
语法:create <table>, {NAME => <family>, VERSIONS => <VERSIONS>}
- 例如:创建表t1,有两个family name:f1,f2,且版本数均为2
create ‘table‘,{NAME => ‘family1‘, VERSIONS => 2,TTL=>‘100000‘},{NAME => ‘family2‘, VERSIONS => 2,TTL=>‘100000‘}
查看表结构
describe <table>
修改表结构
disable ‘table‘
alter ‘table‘,{NAME=>‘info‘,TTL=>‘100000‘}
enable ‘table‘
删除表
disable ‘table‘
drop ‘table‘
统计行数 hbase org.apache.hadoop.hbase.mapreduce.RowCounter ‘tablename‘
添加数据 put ‘table‘,‘rowkey‘,‘family:column‘,‘value‘
查询表中的数据行数 count ‘table‘, {INTERVAL => 100, CACHE => 500}
删除数据
删除行中的某个列值 delete ‘table‘,‘rowkey‘,‘family:column‘
删除行 deleteall ‘table‘,‘rowkey‘
删除表中的所有数据 truncate ‘table‘
rowkey中包含某关键字
- BinaryComparator-使用Bytes.compareTo()比较
- BinaryPrefixComparator-和BinaryComparator差不多,从前面开始比较
- NullComparator-Does not compare against an actual value but whether a given one is null, or not null.
- BitComparator-Performs a bitwise comparison, providing a BitwiseOp class with AND, OR, and XOR operators.
- RegexStringComparator-正则表达式
- SubstringComparator-把数据当成字符串,用contains()来判断
import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.需要使用的过滤器
提取rowkey以01结尾数据
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(".*01$"));
提取rowkey以包含201407的数据
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator("201407"));
提取rowkey以123开头的数据
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator("123".getBytes())))
import org.apache.hadoop.hbase.filter.RowFilter
scan ‘table‘,FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf(‘EQUAL‘),SubstringComparator.new(‘KEY‘))
以某关键字开头
scan ‘table‘, FILTER => "PrefixFilter (‘关键字‘)"
根据column family value查询 包含某一关键字的数据
scan ‘table‘, FILTER=>"ColumnPrefixFilter(‘family:column‘) AND ( ValueFilter(=,‘substring:关键字1‘) OR ValueFilter(=,‘substring:关键字2‘) )"
查询值等于某一关键字的数据
scan ‘table‘, FILTER=>"ValueFilter(=,‘binary:关键字‘)"
原文地址:https://www.cnblogs.com/fresh00air/p/fresh00air_record_2019-04-24_hbase_01.html