大数据笔记(十三)——常见的NoSQL数据库之HBase数据库(A)
一.HBase的表結(jié)構(gòu)和體系結(jié)構(gòu)
1.HBase的表結(jié)構(gòu)
把所有的數(shù)據(jù)存到一張表中。通過犧牲表空間,換取良好的性能。
HBase的列以列族的形式存在。每一個(gè)列族包括若干列
2.HBase的體系結(jié)構(gòu)
主從結(jié)構(gòu):
主節(jié)點(diǎn):HBase
從節(jié)點(diǎn):RegionServer 包含多個(gè)Region,一個(gè)列族就是一個(gè)Region
HBase在ZK中保存數(shù)據(jù)
(*)配置信息、HBase集群結(jié)構(gòu)信息
(*)表的元信息
(*)實(shí)現(xiàn)HBase的HA:high avaibility 高可用性
二.搭建HBase的本地模式和偽分布模式
1.解壓:
tar -zxvf hbase-1.3.1-bin.tar.gz -C ~/training/2.設(shè)置環(huán)境變量: vi ~/.bash_profile
HBASE_HOME=/root/training/hbase-1.3.1 export HBASE_HOMEPATH=$HBASE_HOME/bin:$PATH export PATH使文件生效:source ~/.bash_profile
本地模式??不需要HDFS、直接把數(shù)據(jù)存在操作系統(tǒng)
hbase-env.sh?
export JAVA_HOME=/root/training/jdk1.8.0_144hbase-site.xml
<property><name>hbase.rootdir</name><value>file:///root/training/hbase-1.3.1/data</value> </property>偽分布模式
hbase-env.sh?添加下面這一行,使用自帶的Zookeeper
export HBASE_MANAGES_ZK=truehbase-site.xml 把本地模式的property刪除,添加下列配置
<property><name>hbase.rootdir</name><value>hdfs://192.168.153.11:9000/hbase</value> </property><property><name>hbase.cluster.distributed</name><value>true</value> </property><property><!--Zookeeper的地址--><name>hbase.zookeeper.quorum</name><value>192.168.153.11</value> </property><property><!--數(shù)據(jù)冗余度--><name>dfs.replication</name><value>1</value> </property>
regionservers
192.168.153.11可以在web上查看
?
?
三.搭建HBase的全分布模式和HA
在putty中設(shè)置bigdata12 bigdata13 bigdata14 時(shí)間同步:date -s 2018-03-10
主節(jié)點(diǎn):hbase-site.xml
<property><name>hbase.rootdir</name><value>hdfs://192.168.153.12:9000/hbase</value> </property><property><name>hbase.cluster.distributed</name><value>true</value> </property><property><name>hbase.zookeeper.quorum</name><value>192.168.153.12</value> </property><property><name>dfs.replication</name><value>2</value> </property> <property><!--解決時(shí)間不同步的問題:允許的時(shí)間誤差最大值--><name>hbase.master.maxclockskew</name><value>180000</value> </property>regionservers
192.168.154.13 192.168.153.14拷貝到13和14上:
scp -r hbase-1.3.1/ root@bigdata13:/root/training scp -r hbase-1.3.1/ root@bigdata14:/root/training?
四.HBase在Zookeeper中保存的數(shù)據(jù)和HA的實(shí)現(xiàn)
HA的實(shí)現(xiàn):
不需要額外配置,只用在其中一個(gè)從節(jié)點(diǎn)上單點(diǎn)啟動(dòng)Hmaster
bigdata13:hbase-daemon.sh start master
五.操作HBase
1.Web Console網(wǎng)頁:端口:16010?
? ? ? ? 2.命令行
開啟hbase: start-hbase.sh
? ? ? 開啟hbase shell
?
?
?建表:
hbase(main):001:0> create 'students','info','grade' //創(chuàng)建表 0 row(s) in 1.7020 seconds=> Hbase::Table - students hbase(main):002:0> desc 'students' //查看表結(jié)構(gòu) Table students is ENABLED students COLUMN FAMILIES DESCRIPTION {NAME => 'grade', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODIN G => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATI ON_SCOPE => '0'} {NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING=> 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATIO N_SCOPE => '0'} 2 row(s) in 0.2540 secondshbase(main):003:0> describe 'students' Table students is ENABLED students COLUMN FAMILIES DESCRIPTION {NAME => 'grade', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODIN G => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATI ON_SCOPE => '0'} {NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING=> 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATIO N_SCOPE => '0'} 2 row(s) in 0.0240 seconds
desc和describe的區(qū)別:
desc是SQL*PLUS語句
describe是SQL語句
分析students表的結(jié)構(gòu)
查看有哪些表:list
?
插入數(shù)據(jù):put
put 'students','stu001','info:name','Tom' put 'students','stu001','info:age','24' put 'students','stu001','grade:math','85' put 'students','stu002','info:name','Mary' put 'students','stu002','info:age','28'查詢數(shù)據(jù):
scan?相當(dāng)于:select * from students
?
get? ?相當(dāng)于? select * from students where rowkey=??
?
清空表中的數(shù)據(jù)
delete?DML(可以回滾)
truncate?DDL(不可以回滾)
補(bǔ)充:DDL:數(shù)據(jù)定義語言,如 create/alter/drop/truncate/comment/grant等
DML:數(shù)據(jù)操作語言,如select/delete/insert/update/explain plan等
DCL:數(shù)據(jù)控制語言,如commit/roollback
2、delete會(huì)產(chǎn)生碎片;truncate不會(huì)
3、delete不會(huì)釋放空間;truncate會(huì)
4、delete可以閃回(flashback),truncate不可以閃回
?truncate 'students' -----> 本質(zhì): 先刪除表,再重建
日志:
Truncating 'students' table (it may take a while): - Disabling table... - Truncating table... 0 row(s) in 4.0840 seconds3.JAVA API
修改etc文件:C:\Windows\System32\drivers\etc
添加一行:192.168.153.11 bigdata11
TestHBase.java
package demo;import java.io.IOException;import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Test;import io.netty.util.internal.SystemPropertyUtil;/*** 1.需要一個(gè)jar包: hamcrest-core-1.3.jar* 2.修改windows host文件* C:\Windows\System32\drivers\etc\hosts* 192.168.153.11 bigdata11* @author YOGA**/ public class TestHBase {@Testpublic void testCreateTable() throws Exception{//配置ZK的地址信息Configuration conf = new Configuration();//hbase-site.xml文件里conf.set("hbase.zookeeper.quorum", "192.168.153.11");//得到HBsase客戶端HBaseAdmin client = new HBaseAdmin(conf);//創(chuàng)建表的描述符HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("mytable"));//添加列族htd.addFamily(new HColumnDescriptor("info"));htd.addFamily(new HColumnDescriptor("grade"));//建表 client.createTable(htd);client.close();}@Testpublic void testPut() throws Exception{//配置ZK的地址信息Configuration conf = new Configuration();conf.set("hbase.zookeeper.quorum", "192.168.153.11");//得到HTable客戶端HTable client = new HTable(conf, "mytable");//構(gòu)造一個(gè)Put對(duì)象,參數(shù):rowKeyPut put = new Put(Bytes.toBytes("id001"));//put.addColumn(family, //列族// qualifier, //列// value) ?//列對(duì)應(yīng)的值put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Tom"));client.put(put);//client.put(List<Put>); client.close();}@Testpublic void testGet() throws Exception{//配置ZK的地址信息Configuration conf = new Configuration();conf.set("hbase.zookeeper.quorum", "192.168.153.11");//得到HTable客戶端HTable client = new HTable(conf, "mytable");//構(gòu)造一個(gè)Get對(duì)象Get get = new Get(Bytes.toBytes("id001"));//查詢Result result = client.get(get);//取出數(shù)據(jù)String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));System.out.println(name);client.close();}@Testpublic void testScan() throws Exception{//配置ZK的地址信息Configuration conf = new Configuration();conf.set("hbase.zookeeper.quorum", "192.168.153.11");//得到HTable客戶端HTable client = new HTable(conf, "mytable");//定義一個(gè)掃描器Scan scan = new Scan();//scan.setFilter(filter); 定義一個(gè)過濾器//通過掃描器查詢數(shù)據(jù)ResultScanner rScanner = client.getScanner(scan);for (Result result : rScanner) {String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));System.out.println(name);}} }執(zhí)行以上test,結(jié)果(最后一個(gè))
?
?
?
?
?
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lingluo2017/p/8541387.html
總結(jié)
以上是生活随笔為你收集整理的大数据笔记(十三)——常见的NoSQL数据库之HBase数据库(A)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: container-with-most-
- 下一篇: 解决连接mysql报错1130