您的位置:首页 > 大数据

大数据开发基础下笔记

2016-08-03 21:28 197 查看
HBase原理

1.      认识HBase

2.      Hbase架构

3.      HBase读写流程

 

 

1.      认识HBase

HBase是一个高可靠性、面向列、可伸缩的分布式存储系统

可以随机访问,弥补了HDFS不能随机访问数据,随机读取数据的缺陷

存储和检索数据的平台

不介意数据类型,允许动态的,灵活的数据类型

(1)      HBase架构:master-slaver



Zookeeper存储的是root表,HMaster的地址

RegionServer把自己注册到在zookeeper中,提供HBase regionserver状态信息,是否在线

HMaster可以随时感知各个regionserver的健康状况

Zookeeper可以避免HMaster的单点故障

在HBase中Hmaster可以有多个,通过zookeeper的master选举机制,保证有一个master提供服务

 

 

HMaster管理用户对表的增删改查的操作,管理regionserver的负载均衡,调整region的分布,在region split后负责新的region分布,在regionserver停机后,负责失效的regionserver的region迁移。这些都可以手动使用命令控制

 

Regionserver里包含多个region

Store 存储表的column family的部分数据

Storefile,Hfile存储在Hadoop之下的二进制文件,storefile是对hfile的轻量级封装

Memstore

Regionserver是HBase中最核心的模块,主要负责相应用户的I/O请求,像HDFS文件系统中读写数据。

Regionserver管理一系列的region对象,region由多个store组成,每个store对应一个columnfamily。

(2)      HBase的逻辑模型



尽量设计为一个列簇

(3)HBase的读写流程



HBase相较于传统关系型数据库

优势:速度,并发量可以突破传统关系数据库的瓶颈,扩展性也好,不用加硬件就可以使访问性能成线性增长

缺点:不支持表与表的关联,但是其协处理器可以实现部分的join过程

 

写过程:HBase首先根据zookeeper找到root表,进而找到meta表(存储region在regionserver上的位置),client访问regionserver相应的region,如果集群开启预选设置机制的话,首先会将数据写入memstore,同样会去写预选式日志(memstore达到一个阈值的时候会刷写到磁盘,如果宕机,开启了预选设置机制,可以恢复memstore的数据)。当集群的memstore达到lowerlimit时,会找根节点较大的memstore刷写;如果达到了upperlimit,它会强制把memstore刷写到磁盘。

Compaction的过程:合并文件,清除过期文件

 

Split切分

 

 

读过程

找到root表,进而找到meta表,找到region首先在memstore里读,如果没有找到数据,就会去blockcache中找,如果还是没找到就去磁盘里去找,找到了放到browcache里方便下次去读。

 

//增加和更新

//HBase的增删改查的java代码

Configurationconf = HBaseConfiguration.create();

conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);

conf.set(“hbase.zookeeper.property.clientport”,”2181”);

HConnection conn= HConnectionManager.createConnection(conf);

HTableInterfacetable = conn.getTable(“创建的表名”);

 

Put put = newPut(Bytes.toBytes(“row1”));

Put.add(Bytes.toBytes(“列名”),Bytes.toBytes(“a”),Bytes.toBytes(“abc”));//更新可以直接用

//put实现abc->abcd

table.put(put);

table.close();

conn.close();

 

 

读取数据

Configurationconf = HBaseConfiguration.create();

conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);

conf.set(“hbase.zookeeper.property.clientport”,”2181”);

HConnection conn= HConnectionManager.createConnection(conf);

HTableInterfacetable = conn.getTable(“创建的表名”);

 

Get get = newGet(Bytes.toBytes(“row1”));

get.addColumn(Bytes.toBytes(“d”),Bytes.toBytes(“b”));

Result  rr = table.get(get);

if(rr.getValue(Bytes.toBytes(“d”),Bytes.toBytes(“c”))!=null){

String value = new String(rr.getValue(Bytes.toBytes(“d”), Bytes.toBytes(“c”)));

System.out.println(value);

}

List<cell>cells = rr.listCells();

for(Cell cell :cells){

String rowkey = new String(CellUtil.cloneRow(cell));

String family = new String(CellUtil.cloneFamily(cell));

String column = new String(CellUtil.cloneQualifier(cell));

String value = new String(CellUtil.cloneValue(cell));

System.out.println(“rowkey:”+rowkey+”family:”+family+”column:”+colume+”value:”+value);

 

}

String value =new String(rr.getValue(Bytes.toBytes(“d”), Bytes.toBytes(“b”)));

System.out.println(value);

table.close();

conn.close();

 

 

查询scan

Configurationconf = HBaseConfiguration.create();

conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);

conf.set(“hbase.zookeeper.property.clientport”,”2181”);

HConnection conn= HConnectionManager.createConnection(conf);

HTableInterfacetable = conn.getTable(“创建的表名”);

 

Scan scan = newScan();

ResultScan ss = table.getScanner(scan);

for(Result r : ss){

           List<cell> cells = rr.listCells();

for(Cell cell : cells){

                    Stringrowkey = new String(CellUtil.cloneRow(cell));

String family = new String(CellUtil.cloneFamily(cell));

String column = new String(CellUtil.cloneQualifier(cell));

String value = new String(CellUtil.cloneValue(cell));

System.out.println(“rowkey:”+rowkey+”family:”+family+”column:”+colume+”value:”+value);

}

 

}

table.close();

conn.close();

 

删除

Configurationconf = HBaseConfiguration.create();

conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);

conf.set(“hbase.zookeeper.property.clientport”,”2181”);

HConnection conn= HConnectionManager.createConnection(conf);

HTableInterfacetable = conn.getTable(“创建的表名”);

 

Delete delete =new Delete(Bytes.toBytes(“row1”));

table.delete(delete);

 

Delete delete1 =new Delete(Bytes.toBytes(“row2”));

delete1.deleteColumns(Bytes.toBytes(“d”),Bytes.toBytes(“b”));

table.delete(delete1);

 

table.close();

conn.close();

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  大数据 hbase