Hbase是一个非关系型,面向列族的数据库,其插入和查询的性能远高于一般关系型数据库,下面主要来看一下一些常用的sql.
一、ddl操作
- 创建一个表
create ‘table1’, ‘tab1_id’, ‘tab1_add’, ‘tab1_info’ - 列出所有的表
list - 获得表的描述
describe ‘UBA.VISITOR’ - 删除一个列族
disable alter enable
disable ‘table1’
alter ‘table1’, {NAME=>‘tab1_add’, METHOD=>‘delete’}
enable ‘table1’
5. 查看表是否存在
exists ‘table2’
6. 判断表是否为‘enable’
is_enabled ‘table1’
判断表是否为‘disable’
is_disabled ‘table1’
7. 删除一个表
disable ‘table1’
drop ‘table1’
二、dml操作
-
插入几条记录
put ‘member’, ‘scutshuxue’, ‘info:age’, ‘24’
put ‘member’, ‘scutshuxue’, ‘info:birthday’, ‘1987-06-17’
put ‘member’, ‘scutshuxue’, ‘info:company’, ‘alibaba’
put ‘member’, ‘scutshuxue’, ‘address:contry’, ‘china’
put ‘member’, ‘scutshuxue’, ‘address:province’, ‘zhejiang’
put ‘member’, ‘scutshuxue’, ‘address:city’, ‘hangzhou’ -
全表扫描
scan “UBA.VISITOR” -
获得数据 get
3.1 获得一行的所有数据
get ‘UBA.VISITOR’,‘00devNo12’
获得student表rowkey为001的5个版本数据
get ‘student’,‘001’,{COLUMN=>‘info:name’,VERSIONS=>5}
3.2 获得某行,某列族的几条数据
scan ‘U_FLOWNO_BUSINESS_INFO’,{LIMIT=>10},{COLUMN=>‘info’}
3.3按条件查询(不走rowkey)获得某行,某列族,某列的所有数据
(1)查询LAST_UPDATE_TIME这个字段在一个时间范围之内的数据,
import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter
import org.apache.hadoop.hbase.filter.SubstringComparator
import org.apache.hadoop.hbase.util.Bytes
scan ‘U_FLOWNO_BUSINESS_INFO’, { COLUMNS => ‘INFO:LAST_UPDATE_TIME’, FILTER => SingleColumnValueFilter.new(Bytes.toBytes(‘INFO’), Bytes.toBytes(‘LAST_UPDATE_TIME’),CompareFilter::CompareOp.valueOf(‘GREATER_OR_EQUAL’),Bytes.toBytes(“1470044957000”))}
(2)查询以特定符号开发或结尾的列
scan ‘CUST_GROUP_DETAIL’,{COLUMNS=>‘info’,FILTER=>"(SingleColumnValueFilter(‘info’,‘CUST_GROUP_ID’,=,‘regexstring:.*2015122215551461389799587’,true,true) AND SingleColumnValueFilter(‘info’,‘SN’,=,‘regexstring:.*A1D41B4650C5BE7F’,true,true))"} -
更新一条记录 put(把scutshuxue年龄改为99)
put ‘member’, ‘scutshuxue’, ‘info:age’, 99 -
删除
delete、 deleteall
5.1 (删除行’scutshuxue’, 列族为‘info’ 中age的值
delete ‘member’, ‘scutshuxue’, ‘info:age’
5.2 删除整行
deleteall ‘member’, ‘scutshuxue’
6. 查询表中有多少行
count ‘member’
7. 给‘xiaoming’这个id增加’info:age’字段,并使用counter实现递增
incr ‘member’, ‘xiaoming’, ‘info:age’
8. 将整个表清空
truncate ‘member’