HBase实战

本文详细介绍了HBase的逻辑视图、特点、相关术语、读写操作流程,以及常见命令。阐述了HBase在海量数据存储中的优势,如预分区、RowKey设计原则,并探讨了HBase的适用场景和优化策略。此外,通过MapReduce示例展示了HBase的读写操作,以及如何利用MapReduce对HBase数据进行分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.     HBase海量数据库(key-value)

HBase逻辑视图


行键+时间戳+列族=key

字节码=value

修改=新增;删除=标记(合并时删除)

HBase的特点


HBase相关术语:

元素由行健+列族+时间戳唯一确定

HLOG保存在HDFS中,因此自身具有一定的容错能力;一个RegionServer只有一个HLog

HBase中特殊的表-ROOT(只有一个Region),.META.

当采取写操作时

1)    Zookeeper->-ROOT表->.META.表->TableRegion

2)    写入MemStore中,当MemStore达到某个阈值时,通过flushcache写入StoreFile中,一个冲写产生一个StoreFile

3)    当StoreFile的数量达到某个阈值时,多个StoreFile合并成一个大的StoreFile

4)    当StoreFile的大小达到某个阈值时会分成两个Region

当采取读操作时会优先读取MemStore中的数据,若找不到则去StoreFile中找

同一个列簇的数据被保存在同一个文件夹下的多个文件中,一个Region至少包括一行数据



一张HBase表根据RowKey划分成多个Region,一个Region又包括多个Store(列簇),一个Store又包括多个StoreFile(不同版本数据)

常见HBase命令:

1)  status 查看HBase状态

2)  version 查看HBase版本

3)  create ‘member’,’member_id’,’address’,’info’创建一个’member’表,且具有’member_id’,’address’,’info’三个列族

4)  list 列举所有表

5)  desc ‘member’ 获得表详情

6)  disable ‘member’ 关闭表

7)  alter ‘member’,{NAME=>’member_id’,METHOD=>’delete’}删除表中的member_id列族

8)  enable ‘member’ 打开表

9)  exits ‘member’  表是否存在

10)   is_enabled‘member’ 表是否开启

11)   put ‘member’,’sxw’,’info:age’,’24’插入member表的sxw行的info:age列族value=24

12)   get‘member’,’sxw’,’info’ 得到member表的sxw行的info列族下的所有值

13)   get‘member’,’sxw’,{COLUMN=>’info:age’,TIMESTAMP=>XXXXXXX}得到确定的值

14)   delete‘member’,’sxw’,’info:age’删除member表的sxw行的info:age列族

15)   count‘member’ member表一共多少行

16)   truncate‘member’ 清空member表、

2.     HBase的适用场景

HBase的优势

去重工作只能通过应用程序完成,HBase本身并没有此功能

HBase其他功能:

1)  辅助表(辅助索引)

2)  符合行健(范围查找)

HBase优化策略:

1)

行健startKey->endKey范围的数据写入指定的分区中(即预分区中)

只要创建一个HBase表,最好创建预分区,所谓的预分区是指startkey->endkey之间创建numRegions个分区,当有数据插入时,会根据其rowkey,判断属于哪个预分区中,然后插入

使得近期数据更容易被访问,RowKey的设计特别重要,RowKey一般包括重要信息+(Long.MaxValue-timestamp)

RowKey设计原则:

1) 越小越好

2)  RowKey根据业务需求来,最好包含重要信息

3)  散列型(并发性能考虑)

取反:001 002 100 200

Hash

设置读缓存,MemStore则是写缓存

Minor Compact

 

 

默认合并的文件数量只能是3~10个storefile,且选中的storefile大小必须符合条件,优先合并old storefile,不会造成大量的资源占用,尽量不自动触发major compact,因为它会合并每个Store中的所有StoreFile,会造成很大的负载

当读写数据很大时,允许创建多个HTable分担写压力

既有批量写,又有对于row KEY的优化(将时间戳作为部分,可很快查询到最新数据)

 

 

BlockCache

每次Scanner查找50条数据

尽量使用HTablePool,线程安全

 

MapReduce并发读写HBase数据(对HBase中数据进行分析):

package MapReduceReadAndWriteHBase;

import org.apache.hadoop.hbase.client.Result;

importorg.apache.hadoop.hbase.io.ImmutableBytesWritable;

import org.apache.hadoop.hbase.mapreduce.TableMapper;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import java.io.IOException;

/**

 * Created byGeorge on 2017/5/24.

 */

public class MyMapper extendsTableMapper<Text,IntWritable> {//访问HBase表必须继承TableMapper

    publicstatic final byte[] CF = "cfi".getBytes();//列簇

    publicstatic final byte[] ATTR1 = "name".getBytes();//列名

    privatefinal IntWritable ONE = new IntWritable(1);

    private Texttext = new Text();

    public voidmap(ImmutableBytesWritable row, Result value, Context context) throws

           IOException, InterruptedException {//row为Hbase表中的rowkey,value为scan该行所得的result

       if(row.toString().equals("8283888483818")){

           String val = new String(value.getValue(CF, ATTR1));

           text.set(val);     // we can onlyemit Writables...

           context.write(text, ONE);//"caiqi"->1

        }

        else{

           context.write(new Text("ckey"),new IntWritable(1));

        }

    }

}

 

package MapReduceReadAndWriteHBase;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
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 java.io.IOException;
/**
 * Created by George on 2017/5/24.
 */
public  class MyTableReducer extends TableReducer<Text, IntWritable,
        ImmutableBytesWritable> {//同理
    public static final byte[] CF = "cfi".getBytes();
    public static final byte[] COUNT = "count".getBytes();
    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws
            IOException, InterruptedException {
        int i = 0;
        for (IntWritable val : values) {
            i += val.get();
        }
        Put put = new Put(Bytes.toBytes(key.toString()));//key为HBase表中的rowkey
        System.out.println("count is "+i);
        put.add(CF, COUNT, Bytes.toBytes(i));//参数分别为列簇,列名,value
        context.write(null, put);//写入HBase表中
    }
}

 

package MapReduceReadAndWriteHBase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import java.io.IOException;

/**
 * Created by George on 2017/5/24.
 */
public class MySummaryJob {
    public static void main(String[] args)throws Exception{
        Configuration config = HBaseConfiguration.create();
        Job job = new Job(config,"ExampleSummary");
        job.setJarByClass(MySummaryJob.class);     // class that contains mapper and reducer
        Scan scan = new Scan();
        scan.setCaching(500);        // 1 is the default in Scan, which will be bad for MapReduce jobs
        scan.setCacheBlocks(false);  // don't set to true for MR jobs
// set other scan attrs
        TableMapReduceUtil.initTableMapperJob(
                "test",//MapReduce所读的HBase表名
                scan,//扫描器
                MyMapper.class,//Map类
                Text.class,      //mapper output key
                IntWritable.class,  // mapper output value
                job);
// input table
// Scan instance to control CF and attribute selection
// mapper class
// mapper output key
        TableMapReduceUtil.initTableReducerJob(
                "test",        // output table
                MyTableReducer.class,    // reducer class
                job);
        job.setNumReduceTasks(1);   // at least one, adjust as required
        boolean b = job.waitForCompletion(true);
        if (!b) {
            throw new IOException("error with job!");
        }

    }
}

 

HBase表设计:

一、如何开始模式设计
当我们说到模式(schema),要考虑以下内容:
(1)这个表应该有多少个列族?
(2)列族使用什么数据?
(3)每个列族应该有多少列?
(4)列名应该是什么?(尽管列名不必在建表时定义,但是读写数据时是需要知道的。)
(5)单元存放什么数据?
(6)每个单元存储多少个时间版本?
(7)行键结构是什么?应该包括什么信息?

多对多表设计


一对多表设计

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值