1.Hbase原理
写快读慢的一个数据库,但是读的速度也比mysql快。
首先假设我们有这么一条的命令:put 'user','123','info1:name','zhangsan', client会向hregionserver发送写请求。这条命令会原封不动的存到HLog里面,write ahead log,为什么会有HLog的存在,他只能从底部追加,而不可以去修改,为了数据的持久化和恢复。然后将数据写到内存(memstore)当中,然后反馈client写成功。当memstore数据达到阈值(默认是64M新版128M),将数据刷到StoreFile,StoreFile也是一个文件,再将这个文件写道HFile中,HFile也是一个文件,这个文件是存储在hdfs里面的,进行了序列化,然后将这个文件通过hdfs的api弄到datanode集群里面,将内存中的数据删除,同时删除Hlog中的历史数据。但是本台机器(hregionserver)是有刚刚上出那块数据的管理权限的,也就是说读这块数据还得找到这个hregionserver。
hregionserver数据有拆和合的一个过程:
合是去掉冗余的数据,让块变得小一点,而且数据还是准确的,如果数据还归你管,那么数据会撑成很大很大,读起来就慢了,拆成两个机器管读起来就快了。这个活是Hmaster干的,所以在整个集群运行当中不涉及表管理不涉及到合并数据,不涉及增删改查,Hmaster挂了没关系。
数据的拆分和合并不是一个时时刻刻地过程,他是由数块的大小来触发的,并且他不会占用hregionserver的IO和网络请求。所以他不影响数据的读取性能,而且还清理了垃圾数据。
定位数据也就来回三次,总共六次:
1.写流程
- client向hregionserver发送写请求。
- hregionserver将数据写到hlog(write ahead log)。为了数据的持久化和恢复。
- hregionserver将数据写到内存(memstore)
- 反馈client写成功。
2.数据flush过程
- 当memstore数据达到阈值(默认是64M),将数据刷到硬盘,将内存中的数据删除,同时删除Hlog中的历史数据。
- 并将数据存储到hdfs中。
- 在hlog中做标记点。
3.数据合并过程
- 当数据块达到4块,hmaster将数据块加载到本地,进行合并
- 当合并的数据超过256M,进行拆分,将拆分后的region分配给不同的hregionserver管理
- 当hregionser宕机后,将hregionserver上的hlog拆分,然后分配给不同的hregionserver加载,修改.META.
- 注意:hlog会同步到hdfs
4.hbase的读流程
- 通过zookeeper和-ROOT- .META.表定位hregionserver。
- 数据从内存和硬盘合并后返回给client
- 数据块会缓存
5.hmaster的职责
1、管理用户对Table的增、删、改、查操作;
2、记录region在哪台Hregion server上
3、在Region Split后,负责新Region的分配;
4、新机器加入时,管理HRegion Server的负载均衡,调整Region分布
5、在HRegion Server宕机后,负责失效HRegion Server 上的Regions迁移。
6.hregionserver的职责
HRegion Server主要负责响应用户I/O请求,向HDFS文件系统中读写数据,是HBASE中最核心的模块。
HRegion Server管理了很多table的分区,也就是region。
7.client职责
Client
HBASE Client使用HBASE的RPC机制与HMaster和RegionServer进行通信
管理类操作:Client与HMaster进行RPC;
数据读写类操作:Client与HRegionServer进行RPC。
2.MapReduce操作Hbase
package com.wx.hbase1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HbaseMR {
/*
这是一个运行MR的模板程序,主要是将一些文本输入hbase,然后读取hbase的信息,进行wordcount计算,最后再写入hbase
*/
/**
* 创建hbase配置
*/
static Configuration config = null;
static {
config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum","zookeeper1"); //设置zookeeper地址
config.set("hbase.zookeeper.property.clientPort", "2181"); //zookeeper端口
}
/**
* 表信息
*/
public static final String tableName = "word";//表名1 存放文本信息的表
public static final String colf = "content";//列族
public static final String col = "info";//列
public static final String tableName2 = "stat";//表名2。存放计算后的信息的表
/**
* 初始化表结构,及其数据
*/
public static void initTB() {
HTable table=null; //表类
HBaseAdmin admin=null; //管理类
try {
admin = new HBaseAdmin(config);//创建表管理
/*删除表,事先判断如果这个表存在就删除*/
if (admin.tableExists(tableName)||admin.tableExists(tableName2)) {
System.out.println("table is already exists!");
admin.disableTable(tableName); //删除的语法是要先置为不可用
admin.deleteTable(tableName);
admin.disableTable(tableName2);
admin.deleteTable(tableName2);
}
/*创建表 创建表的描述类*/
HTableDescriptor desc = new HTableDescriptor(tableName);
//创建列族的描述类
HColumnDescriptor family = new HColumnDescriptor(colf);
desc.addFamily(family);
admin.createTable(desc);
//创建表二的描述类
HTableDescriptor desc2 = new HTableDescriptor(tableName2);
//创建表二的列族的描述类
HColumnDescriptor family2 = new HColumnDescriptor(colf);
desc2.addFamily(family2);
admin.createTable(desc2);
/*插入数据*/
table = new HTable(config,tableName);
table.setAutoFlush(false);
table.setWriteBufferSize(500);
List<Put> lp = new ArrayList<Put>();
Put p1 = new Put(Bytes.toBytes("1"));
p1.add(colf.getBytes(), col.getBytes(), ("The Apache Hadoop software library is a framework").getBytes());
lp.add(p1);
Put p2 = new Put(Bytes.toBytes("2"));
p2.add(colf.getBytes(),col.getBytes(),("The common utilities that support the other Hadoop modules").getBytes());
lp.add(p2);
Put p3 = new Put(Bytes.toBytes("3"));
p3.add(colf.getBytes(), col.getBytes(),("Hadoop by reading the documentation").getBytes());
lp.add(p3);
Put p4 = new Put(Bytes.toBytes("4"));
p4.add(colf.getBytes(), col.getBytes(),("Hadoop from the release page").getBytes());
lp.add(p4);
Put p5 = new Put(Bytes.toBytes("5"));
p5.add(colf.getBytes(), col.getBytes(),("Hadoop on the mailing list").getBytes());
lp.add(p5);
table.put(lp);
table.flushCommits();
lp.clear();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(table!=null){
table.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* MyMapper 继承 TableMapper
* TableMapper<Text,IntWritable>
* Text:输出的key类型,
* IntWritable:输出的value类型
*/
public static class MyMapper extends TableMapper<Text, IntWritable> {
private static IntWritable one = new IntWritable(1);
private static Text word = new Text();
@Override
//输入的类型为:key:rowKey; value:一行数据的结果集Result
protected void map(ImmutableBytesWritable key, Result value,
Context context) throws IOException, InterruptedException {
//获取一行数据中的colf:col
String words = Bytes.toString(value.getValue(Bytes.toBytes(colf), Bytes.toBytes(col)));// 表里面只有一个列族,所以我就直接获取每一行的值
//按空格分割
String itr[] = words.toString().split(" ");
//循环输出word和1
for (int i = 0; i < itr.length; i++) {
word.set(itr[i]);
context.write(word, one);
}
}
}
/**
* MyReducer 继承 TableReducer
* TableReducer<Text,IntWritable>
* Text:输入的key类型,
* IntWritable:输入的value类型,
* ImmutableBytesWritable:输出类型,表示rowkey的类型
*/
public static class MyReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
//对mapper的数据求和
int sum = 0;
for (IntWritable val : values) {//叠加
sum += val.get();
}
// 创建put,设置rowkey为单词
Put put = new Put(Bytes.toBytes(key.toString()));
// 封装数据
put.add(Bytes.toBytes(colf), Bytes.toBytes(col),Bytes.toBytes(String.valueOf(sum)));
//写到hbase,需要指定rowkey、put
context.write(new ImmutableBytesWritable(Bytes.toBytes(key.toString())),put);
}
}
public static void main(String[] args) throws IOException,ClassNotFoundException, InterruptedException {
//初始化表
initTB();//初始化表
//创建job
Job job = Job.getInstance(config,"HbaseMR");//job
job.setJarByClass(HbaseMR.class);//主类
//创建scan
Scan scan = new Scan();
//可以指定查询某一列
scan.addColumn(Bytes.toBytes(colf), Bytes.toBytes(col));
//创建查询hbase的mapper,设置表名、scan、mapper类、mapper的输出key、mapper的输出value
TableMapReduceUtil.initTableMapperJob(tableName, scan, MyMapper.class,Text.class, IntWritable.class, job);
//创建写入hbase的reducer,指定表名、reducer类、job
TableMapReduceUtil.initTableReducerJob(tableName2, MyReducer.class, job);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}