Hbase的shell命令
1. help 帮助命令
help '命令名字'
2. hbase中数据库的概念
namespace
2.1 显示所有的数据库
list_namespace
默认 default
hbase
2.2 显示当前数据库中所有的表
list_namespace_tables 'hbase'
2.3 创建一个数据库
create_namespace 'baizhi150'
2.4 描述数据库
describe_namespace 'baizhi150'
2.5 修改数据库
alter_namespace
2.6 删除数据库
drop_namespace 'baizhi150'
3. 创建hbase中的表【重点】
3.1 基本的建表方式 【默认default库中】
create 't1','cf1'
3.2 创建多个列簇【默认default库中】
create 't1','cf1','cf2'
3.3 指定表所属的数据库
create 'baizhi150:t1','cf1'
3.4 详细描述列簇的相关属性
create 'baizhi150:t2',{NAME=>'cf1',VERSIONS=>2},{NAME=>'cf2'}
4. 描述表
describe 'baizhi150:t1'
5. 修改表
alter 'baizhi150:t2',{NAME=>'cf2',VERSIONS=>2}
6. 删除表
disable 'baizhi150:t2'
drop 'baizhi150:t2'
7. 失效 生效表相关命令
enable disable
enable_all disable_all
is_enable is_disable
8. 判断表是否存在
exists
9. 表的查找命令
list 'ns:t.*'
10. 插入数据
put 't1',’rowkey‘,'family:qualify','value'
put 'ns:t1',’rowkey‘,'family:qualify','value'
11. 删除数据
delete 'ns:t1' ,'rowkey','family:qualify','timestamp'
12. 全表扫描
scan '表名'
scan 'ns1:tb1', {STARTROW => '20170521_10001',STOPROW => '20170521_10003'}
scan 'baizhi150:user',{STARTROW=>'001',STOPROW=>'004'}
不包括stoprow的值
13. 某条数据的查询
get 'baizhi150:user','001'
get 'baizhi150:user','001','base:name'
Hbase的集群的搭建
-
时间同步集群
hadoop41.baizhiedu.com 作为时间同步服务器 主节点 1. yum install ntp 三台机器 2. service ntpd start 三台机器 chkconfig ntpd on 3. 服务器节点 主节点 ntpdate -u 202.112.10.36 vi /etc/ntp.conf restrict 192.168.111.0 mask 255.255.255.0 nomodify notrap # 中国这边最活跃的时间服务器 : http://www.pool.ntp.org/zone/cn server 210.72.145.44 perfer # 中国国家受时中心 server 202.112.10.36 # 1.cn.pool.ntp.org server 59.124.196.83 # 0.asia.pool.ntp.org # 允许上层时间服务器主动修改本机时间 restrict 210.72.145.44 nomodify notrap noquery restrict 202.112.10.36 nomodify notrap noquery restrict 59.124.196.83 nomodify notrap noquery # 外部时间服务器不可用时,以本地时间作为时间服务 server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 service ntpd restart 4. client端 vi /etc/ntp.conf server 192.168.111.41 #这里指的是ntp服务的ip 192.168.206.130 restrict 192.168.111.41 nomodify notrap noquery server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 5. 三台机器 service ntpd restart 6. 从节点同步主节点时间 ntpdate -u 192.168.184.16 adjust time server 192.168.19.10 offset -0.017552 sec 5. date命令查看处理结果
-
Hadoop集群
HDFS集群 Yarn集群
-
Zookeeper集群
1. 解压缩 2. 创建数据文件夹 zookeeper_home/data 3. 修改配置文件 conf/zoo.cfg dataDir server.0 4. 在data文件夹中创建 myid文件 0 1 2 5. 启动服务 bin/zkServer.sh start
-
Hbase集群
1. 准备:hbase_home data/tmp logs目录中的内容清空 hdfs 上面 hbase目录清空 2. 修改hbase_home/conf/hbase-env.sh export JAVA_HOME=/usr/java/jdk1.7.0_71 export HBASE_MANAGES_ZK=false 3. hbase-site.xml <property > <name>hbase.tmp.dir</name> <value>/opt/install/hbase-0.98.6-hadoop2/data/tmp</value> </property> <property > <name>hbase.rootdir</name> <value>hdfs://hadoop6.baizhiedu.com:8020/hbase</value> </property> <property > <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>hadoop.baizhiedu.com,hadoop1.baizhiedu.com,hadoop2.baizhiedu.com</value> </property> 4. regionservers hadoop.baizhiedu.com hadoop1.baizhiedu.com hadoop2.baizhiedu.com 5. 替换jar hadoop jar 6. scp 分发 6. 启动hbase(所有安装hbase的机器都执行) bin/hbase-daemon.sh start master bin/hbase-daemon.sh start regionserver
HBase的Java API
# java访问HBase的核心API Configruation HBase相关的配置 Htable HBase中的表 Put 插入数据 Get 查询数据 Scan 扫描数据 BytesUtil 字节处理
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>0.98.6-hadoop2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>0.98.6-hadoop2</version> </dependency>
对象 | 作用 | 基本用法 |
---|---|---|
Configruation | Hbase相关的配置文件 | conf.set(“key”,“value”); |
HTable | Hbase中的表 | 表相关的操作都是HTable完成 |
Put | HBase中插入数据 | Put put = new Put(rowkey) put.add HTable.put(put) |
Delete | HBase中的删除操作 | Delete delete = new Delete(rowkey) HTable.delete(delete) |
Get | HBase查询单条数据 | Get get = net Get(rowkey) HTable.get(get) —> Result |
Result | 单行数据 | Result – Cells — Cell — cloneFamily cloneQualify cloneValue |
Scan | 表的扫描 | ResultScanner —> Result |
Hbase中的过滤器
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oETiSX4C-1572861985167)(大数据课程day12.assets\1559640136082.png)]
-
行键相关的过滤器
1.比较行键值的大小 Filter filter1 = new RowFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("0003"))); scan.setFilter(filter1); 2. 比较行键按照特定的规则设计 Filter filter1 = new PrefixFilter(Bytes.toBytes("000")); scan.setFilter(filter1);
-
列簇相关的筛选
1. 只要base列簇相关的数据 Filter filter1 = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("base"))); scan.setFilter(filter1);
-
限定符相关筛选
Filter filter1 = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("age"))); scan.setFilter(filter1);
-
值的筛选
Filter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("1") ); scan.setFilter(filter);
-
列簇中的数据进行筛选
SingleColumnValueFilter filter = new SingleColumnValueFilter( Bytes.toBytes("base"), Bytes.toBytes("sex"), CompareFilter.CompareOp.EQUAL, new SubstringComparator("male")); filter.setFilterIfMissing(true); //filter.setFilterIfMissing(false); //符合要求的数据 password=123456 //column 中不包含password限定符 查询出来 scan.setFilter(filter); 同时要排除password 限定符 SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter( Bytes.toBytes("base"), Bytes.toBytes("password"), CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("66666")); filter.setFilterIfMissing(true); //filter.setFilterIfMissing(false); //符合要求的数据 password=123456 //column 中不包含password限定符 查询出来 scan.setFilter(filter);
-
FilterList
设置多个过滤器 同时生效 FilterList filterList = new FilterList(); Filter filter1 = new PrefixFilter(Bytes.toBytes("000")); SingleColumnValueFilter filter2 = new SingleColumnValueFilter( Bytes.toBytes("base"), Bytes.toBytes("password"), CompareFilter.CompareOp.EQUAL, new SubstringComparator("123456")); filter2.setFilterIfMissing(true); filterList.addFilter(filter1); filterList.addFilter(filter2); scan.setFilter(filterList);