一、案例分析

常见避免数据热点问题的处理方式有:加盐、哈希、反转等方法结合预分区使用。

由于目前原数据第一字段为时间戳形式,第二字段为电话号码,直接存储容易引起热点问题,通过加随机列、组合时间戳、字段反转的方式来设计Rowkey,来实现既能高效查询又能避免热点问题。

二、代码部分

 package beifeng.hadoop.hbase;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.HBaseAdmin;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.io.Text; /**
* 遵循rowkey的设计原则
* 1.rowkey不能过长
* 2.唯一性,加随机列 md5
* 3.注意避免产生数据热点
* 4.满足更多的查询场景
* @author Administrator
*
*/
public class LoadData extends Configured implements Tool { /**
* 综合考虑 使用时间和手机 做组合key,能更好的满足应用场景
* @author Administrator
*
*/
public static class LoadDataMapper extends Mapper<LongWritable, Text, LongWritable, Text> {
//专门处理时间戳 =》标准时间格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHsss");
private Text mapOutputValue = new Text();
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, LongWritable, Text>.Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] splited = line.split("\t"); //将切分的第一个字段转成标准时间
String formatDate = sdf.format(new Date(Long.parseLong(splited[0].trim())));
//将手机号码反转
String phoneNumber = splited[1].toString();
String reversePhoneNumber = new StringBuffer(phoneNumber).reverse().toString(); String rowKeyString = reversePhoneNumber +"|"+formatDate;
//反转手机号+“|”+时间 +正行内容拼接
mapOutputValue.set(rowKeyString+"\t"+ line);
context.write(key, mapOutputValue);
}
} public static class LoadDataReuducer extends TableReducer<LongWritable, Text, NullWritable>{ //设置HBase的列簇
private static final String COLUMN_FAMAILY = "info";
@Override
protected void reduce(LongWritable key, Iterable<Text> values,
Reducer<LongWritable, Text, NullWritable, Mutation>.Context context)
throws IOException, InterruptedException {
for (Text value:values) {
String[] splited = value.toString().split("\t");
String rowKey = splited[0];
// System.err.println(rowKey);
Put put = new Put(rowKey.getBytes());
//put.addColumn(COLUMN_FAMAILY.getBytes(),"row".getBytes(),value.getBytes());
put.add(COLUMN_FAMAILY.getBytes(), "reportTime".getBytes(), splited[1].getBytes());
put.add(COLUMN_FAMAILY.getBytes(), "apmac".getBytes(), splited[3].getBytes());
put.add(COLUMN_FAMAILY.getBytes(), "acmac".getBytes(), splited[4].getBytes());
put.add(COLUMN_FAMAILY.getBytes(), "host".getBytes(), splited[5].getBytes());
put.add(COLUMN_FAMAILY.getBytes(), "siteType".getBytes(), splited[6].getBytes());
put.add(COLUMN_FAMAILY.getBytes(), "upPackNum".getBytes(), splited[7].getBytes());
put.add(COLUMN_FAMAILY.getBytes(), "downPackNum".getBytes(), splited[8].getBytes());
put.add(COLUMN_FAMAILY.getBytes(), "unPayLoad".getBytes(), splited[9].getBytes());
put.add(COLUMN_FAMAILY.getBytes(), "downPayLoad".getBytes(), splited[10].getBytes());
put.add(COLUMN_FAMAILY.getBytes(),"httpStatus".getBytes(),splited[11].getBytes());
context.write(NullWritable.get(), put); }
}
} public static void createTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "beifeng01"); HBaseAdmin admin = new HBaseAdmin(conf); TableName tName = TableName.valueOf(tableName); HTableDescriptor htd = new HTableDescriptor(tName);
HColumnDescriptor hcd = new HColumnDescriptor("info");
htd.addFamily(hcd); if(admin.tableExists(tName)) {
System.out.println(tableName+"is exist,trying to recrate the table");
admin.disableTable(tName);
admin.deleteTable(tName);
}
admin.createTable(htd);
System.out.println("create new table"+ " " + tableName); } public int run(String[] args) throws Exception { Configuration conf = this.getConf();
conf.set("hbase.zookeeper.quorum", "beifeng01");
conf.set(TableOutputFormat.OUTPUT_TABLE, "phoneLog"); createTable("phoneLog"); Job job = Job.getInstance(conf, this.getClass().getSimpleName());
job.setJarByClass(this.getClass());
job.setNumReduceTasks(1); // map class
job.setMapperClass(LoadDataMapper.class);
job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(Text.class); // reduce class
job.setReducerClass(LoadDataReuducer.class);
job.setOutputFormatClass(TableOutputFormat.class); Path inPath = new Path(args[0]);
FileInputFormat.addInputPath(job, inPath); boolean isSucced = job.waitForCompletion(true); return isSucced ? 0 : 1;
} public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create(); //指定HDFS数据地址
args = new String[] {"hdfs://hbase/data/input/HTTP_20130313143750.data"};
int status = ToolRunner.run(
conf,
new LoadData(),
args); System.exit(status);
}
}

运行完程序后scan 查看效果

hbase(main):004:0> scan 'phoneLog', {LIMIT => 2}
ROW COLUMN+CELL
01787706731|2013031314048 column=info:acmac, timestamp=1544022103345, value=120.196.100.82
01787706731|2013031314048 column=info:apmac, timestamp=1544022103345, value=00-FD-07-A4-7B-08:CMCC
01787706731|2013031314048 column=info:downPackNum, timestamp=1544022103345, value=2
01787706731|2013031314048 column=info:downPayLoad, timestamp=1544022103345, value=120
01787706731|2013031314048 column=info:host, timestamp=1544022103345, value=
01787706731|2013031314048 column=info:httpStatus, timestamp=1544022103345, value=200
01787706731|2013031314048 column=info:reportTime, timestamp=1544022103345, value=1363157988072
01787706731|2013031314048 column=info:siteType, timestamp=1544022103345, value=
01787706731|2013031314048 column=info:unPayLoad, timestamp=1544022103345, value=120
01787706731|2013031314048 column=info:upPackNum, timestamp=1544022103345, value=2
10007032831|2013031314045 column=info:acmac, timestamp=1544022103345, value=120.196.100.99
10007032831|2013031314045 column=info:apmac, timestamp=1544022103345, value=20-7C-8F-70-68-1F:CMCC
10007032831|2013031314045 column=info:downPackNum, timestamp=1544022103345, value=3
10007032831|2013031314045 column=info:downPayLoad, timestamp=1544022103345, value=180
10007032831|2013031314045 column=info:host, timestamp=1544022103345, value=
10007032831|2013031314045 column=info:httpStatus, timestamp=1544022103345, value=200
10007032831|2013031314045 column=info:reportTime, timestamp=1544022103345, value=1363157985079
10007032831|2013031314045 column=info:siteType, timestamp=1544022103345, value=
10007032831|2013031314045 column=info:unPayLoad, timestamp=1544022103345, value=360
10007032831|2013031314045 column=info:upPackNum, timestamp=1544022103345, value=6

Hbase 表的Rowkey设计避免数据热点的更多相关文章

  1. Hbase表类型的设计

    HBase表类型的设计 1.短宽 这种设计一般适用于: * 有大量的列 * 有很少的行 2.高瘦 这种设计一般适用于: * 有很少的列 * 有大量的行 3.短宽-高瘦的对比 短宽 * 使用列名进行查询 ...

  2. HBase(九)HBase表以及Rowkey的设计

    一 命名空间 1 命名空间的结构 1) Table:表,所有的表都是命名空间的成员,即表必属于某个命名空间,如果没有指定, 则在 default 默认的命名空间中. 2) RegionServer g ...

  3. hbase实践之rowkey设计

    rowkey设计的重要性 rowkeys是HBase表设计中唯一重要的一点. rowkey设计要求 唯一性 存储特性 按照字典顺序排序存储 查询特性 由于其存储特性导致查询特性: 查询单个记录: 查定 ...

  4. hbase表的高性能设计

    第7章 HBase优化 7.1 高可用 在HBase中Hmaster负责监控RegionServer的生命周期,均衡RegionServer的负载,如果Hmaster挂掉了,那么整个HBase集群将陷 ...

  5. hbase实践之Rowkey设计之道

    笔者从一开始接触hbase就在思考rowkey设计,希望rowkey设计得好,能够支持查询的需求.使用hbase一段时间后,再去总结一些hbase的设计方法,无外乎以下几种: reverse salt ...

  6. 大数据性能调优之HBase的RowKey设计

    1 概述 HBase是一个分布式的.面向列的数据库,它和一般关系型数据库的最大区别是:HBase很适合于存储非结构化的数据,还有就是它基于列的而不是基于行的模式. 既然HBase是采用KeyValue ...

  7. HBase Rowkey 设计指南

    为什么Rowkey这么重要 RowKey 到底是什么 我们常说看一张 HBase 表设计的好不好,就看它的 RowKey 设计的好不好.可见 RowKey 在 HBase 中的地位.那么 RowKey ...

  8. HBase之六:HBase的RowKey设计

    数据模型 我们可以将一个表想象成一个大的映射关系,通过行健.行健+时间戳或行键+列(列族:列修饰符),就可以定位特定数据,Hbase是稀疏存储数据的,因此某些列可以是空白的, Row Key Time ...

  9. HBase(三): Azure HDInsigt HBase表数据导入本地HBase

    目录: hdfs 命令操作本地 hbase Azure HDInsight HBase表数据导入本地 hbase hdfs命令操作本地hbase: 参见  HDP2.4安装(五):集群及组件安装 , ...

随机推荐

  1. 论组织资源 #F315

    论组织资源 #F315 2016-02-29 yevon_ou 水库论坛 论组织资源  #F315 首先,让我们看一段长微博. 转:五毛大战美分,基层民猪那破事... 就是小区业委会的事,根据业委会相 ...

  2. tree 向下查找 (删除整条tree)

    需求:通过点击获取需要删除的id(即获取到整条信息),如果该条数据没有子集,通过id删除即可,如果有子集,则该数据下所有的子集都需要删 删除后页面的数据更新在 下一篇 讲解 1 const id =' ...

  3. 【设计模式最终总结】桥接模式 VS 外观模式

    差异点 外观模式,是把功能通过一个接口提供出来,方便日后更换实现,或者这种实现可以由多方提供,但同时只用一个.典型例子:@Slf4j 桥接模式,多个维度,每个维度提供一个接口,这些接口集中在一个类中, ...

  4. 023re模块(正则)

    之前我刚学的python知识点,没有题目进行熟悉,后面的知识点会有练习题,并且慢慢补充.看到很多都是很简单的练习,碰到复杂.需要运用的再补充吧#字符串中使用到正则表达式 s='hello world' ...

  5. BZOJ1770:[USACO]lights 燈(高斯消元,DFS)

    Description 貝希和她的閨密們在她們的牛棚中玩遊戲.但是天不從人願,突然,牛棚的電源跳閘了,所有的燈都被關閉了.貝希是一個很膽小的女生,在伸手不見拇指的無盡的黑暗中,她感到驚恐,痛苦與絕望. ...

  6. MyBatis(8)延迟加载&缓存

    什么是延迟加载? resultMap可以实现高级映射,association,collection具有延迟加载的功能.   当我们需要查询某个信息的时候,再去查询,达到按需查询,就是延迟加载   可以 ...

  7. scp出现ssh port 22: Connection refused 问题解决具体步骤

    [root(0)@sys11 09:20:29 /home/work/Code_release/bj]# scp ./release.sh  root@192.168.161.151:/Users/a ...

  8. 自动化构建工具grunt的学习

    关于grunt的一些记录,记的比较乱... 0.删除node_modules文件夹 命令行: npm install rimraf -g //先运行 rimraf node_modules //然后运 ...

  9. [转]表单提交:button input submit 的区别

    博客转自于   http://harttle.com/2015/08/03/form-submit.html  ,同时自己做了稍微改动 最近项目代码中的表单提交的方式已经百花齐放了,现在用这篇文章来整 ...

  10. Multicast Routing

    Multicasting Source S sends packets to multicast group G1 (and minimize the number of copies) Revers ...