Hbase 表的Rowkey设计避免数据热点
一、案例分析
常见避免数据热点问题的处理方式有:加盐、哈希、反转等方法结合预分区使用。
由于目前原数据第一字段为时间戳形式,第二字段为电话号码,直接存储容易引起热点问题,通过加随机列、组合时间戳、字段反转的方式来设计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设计避免数据热点的更多相关文章
- Hbase表类型的设计
HBase表类型的设计 1.短宽 这种设计一般适用于: * 有大量的列 * 有很少的行 2.高瘦 这种设计一般适用于: * 有很少的列 * 有大量的行 3.短宽-高瘦的对比 短宽 * 使用列名进行查询 ...
- HBase(九)HBase表以及Rowkey的设计
一 命名空间 1 命名空间的结构 1) Table:表,所有的表都是命名空间的成员,即表必属于某个命名空间,如果没有指定, 则在 default 默认的命名空间中. 2) RegionServer g ...
- hbase实践之rowkey设计
rowkey设计的重要性 rowkeys是HBase表设计中唯一重要的一点. rowkey设计要求 唯一性 存储特性 按照字典顺序排序存储 查询特性 由于其存储特性导致查询特性: 查询单个记录: 查定 ...
- hbase表的高性能设计
第7章 HBase优化 7.1 高可用 在HBase中Hmaster负责监控RegionServer的生命周期,均衡RegionServer的负载,如果Hmaster挂掉了,那么整个HBase集群将陷 ...
- hbase实践之Rowkey设计之道
笔者从一开始接触hbase就在思考rowkey设计,希望rowkey设计得好,能够支持查询的需求.使用hbase一段时间后,再去总结一些hbase的设计方法,无外乎以下几种: reverse salt ...
- 大数据性能调优之HBase的RowKey设计
1 概述 HBase是一个分布式的.面向列的数据库,它和一般关系型数据库的最大区别是:HBase很适合于存储非结构化的数据,还有就是它基于列的而不是基于行的模式. 既然HBase是采用KeyValue ...
- HBase Rowkey 设计指南
为什么Rowkey这么重要 RowKey 到底是什么 我们常说看一张 HBase 表设计的好不好,就看它的 RowKey 设计的好不好.可见 RowKey 在 HBase 中的地位.那么 RowKey ...
- HBase之六:HBase的RowKey设计
数据模型 我们可以将一个表想象成一个大的映射关系,通过行健.行健+时间戳或行键+列(列族:列修饰符),就可以定位特定数据,Hbase是稀疏存储数据的,因此某些列可以是空白的, Row Key Time ...
- HBase(三): Azure HDInsigt HBase表数据导入本地HBase
目录: hdfs 命令操作本地 hbase Azure HDInsight HBase表数据导入本地 hbase hdfs命令操作本地hbase: 参见 HDP2.4安装(五):集群及组件安装 , ...
随机推荐
- 【Leetcode】【Medium】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【Leetcode】【Medium】4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- ElasticSearch之常用插件安装命令
#head监控安装,推荐 bin/plugin -install mobz/elasticsearch-head #bigdesk集群状态,推荐 bin/plugin -install lukas-v ...
- 设计模式:解释器(Interpreter)模式
设计模式:解释器(Interpreter)模式 一.前言 这是我们23个设计模式中最后一个设计模式了,大家或许也没想到吧,竟然是编译原理上的编译器,这样说可能不对,因为编译器分为几个部分组成呢,比如词 ...
- Jenkins的job执行arquilian test时总是报JBoss启动失败错误
我的Jenkins环境是一个Master+一个slave,Job的执行主要由slave来进行.Master只负责调度. slave上安装有JDK7.JDK8.slave agent用的是java we ...
- 薄弱的交互页面之新浪微博到博客的储存型xss漏洞
首先分享一片博文到微博,然后 在微博评论xss code 最后回到博客点击举报就触发xss了 点击举报 Xss之2 首先还是分享一片博文到微博,然后评论xsscode 回到我的博客个人中心,查看评论 ...
- sublime text html5开发学习 插件篇记录
1.第一步先按照 Package Control,具体步骤自行百度,Google. 2. view in browser 默认的快捷键应该是这样的,我用的是IE浏览器.所以ctrl+alt+i 即可让 ...
- 十一.安装Redis
redis是一个NOSql数据库,在java web中,经常用来存储session等,官网是https://redis.io/ 当前官网指出的稳定版本为3.2.6,下载地址为http://downlo ...
- Jupyter Notebook 设置黑色背景主题、字体大小、代码自动补全
1.背景主题.字体大小设置 安装Jupyter主题: pip install jupyterthemes 然后,更新Jupyter主题: pip install --upgrade jupyterth ...
- 使用@SuppressWarnings("unchecked")消除非受检警告
使用泛型编程时,会遇到许多编译器警告,如:非受检强制转化警告,非受检方法调用警告,非受检普通数组创建警告,费受精转换警告.这次的内容就是遇到这些警告的时候你该怎么办 PS:非受检警告就是代码上黄色的感 ...