大数据全系列 教程
1869个小节阅读:467.5k
408考研
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
编写NewInstallUserMapper类
xxxxxxxxxx
package cn.itbaizhan.tranformer.mr;
import cn.itbaizhan.common.DateEnum;
import cn.itbaizhan.common.EventLogConstants;
import cn.itbaizhan.tranformer.model.*;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.util.List;
public class NewInstallUserMapper extends TableMapper<StatsUserDimesion, TimeOutputValue> {
//定义日志对象
private static final Logger LOGGER = Logger.getLogger(NewInstallUserMapper.class);
//定义输出的value对象
private TimeOutputValue valueOut = new TimeOutputValue();
//定义输出的key对象
private StatsUserDimesion statsUserDimesion = new StatsUserDimesion();
//定义时间、浏览器、平台、模块 维度
private DateDimension dateDimension = null;
//新增用户,不需要浏览器维度对象为了避免空指针异常,定义一个默认的对象
private BrowserDimension defaultBrowserDimension =
new BrowserDimension("","");
private List<BrowserDimension> browserDimensionList;
//新增用户 或者 浏览器新增用户,都需要平台维度
private List<PlatformDimension> platformDimensionList;
//定义KpiDimension对象
private KpiDemension kpiNewInstallUser =
new KpiDemension(EventLogConstants.NEW_INSTALL_USER);
private KpiDemension kpiBrowserNewInstallUser =
new KpiDemension(EventLogConstants.NEW_INSTALL_USER);
protected void map(ImmutableBytesWritable key, Result value, Mapper<ImmutableBytesWritable, Result, StatsUserDimesion, TimeOutputValue>.Context context) throws IOException, InterruptedException {
//获取数据:en,s_time,pl,u_ud,browser,browser_v
//getColumnLatestCell():适合使用在Cell中保存了多个版本的数据
//getValue():适合使用在Cell中保存一个版本的数据
String uuId = Bytes.toString(value.getValue(EventLogConstants.EVENT_LOGS_FAMILY_NAME.getBytes(),
EventLogConstants.LOG_COLUMN_NAME_UUID.getBytes()));
String serverTime = Bytes.toString(value.getValue(EventLogConstants.EVENT_LOGS_FAMILY_NAME.getBytes(),
EventLogConstants.LOG_COLUMN_NAME_SERVER_TIME.getBytes()));
String platForm = Bytes.toString(value.getValue(EventLogConstants.EVENT_LOGS_FAMILY_NAME.getBytes(),
EventLogConstants.LOG_COLUMN_NAME_PLATFORM.getBytes()));
//以上三个参数如果有一个为空,都不继续进行分析
if(StringUtils.isBlank(uuId)||StringUtils.isBlank(serverTime)||StringUtils.isBlank(platForm)){
LOGGER.error("uuId or serverTime or platForm is blank!");
return;
}
//定义long型的时间
long sTime = Long.parseLong(serverTime);
//封装输出的vlaue对象
valueOut.setId(uuId);
valueOut.setTime(sTime);
//封装输出key涉及到的对象
StatsCommonDimesion statsCommon = statsUserDimesion.getStatsCommon();
//构造日期维度的实体类的对象
dateDimension = DateDimension.buildDate(sTime, DateEnum.DAY);
//将时间维度对象封装到statsCommon中
statsCommon.setDate(dateDimension);
//构造平台维度的对象集合
platformDimensionList = PlatformDimension.buildList(platForm);
//获取浏览器的名称和版本
String browserName = Bytes.toString(value.getValue(EventLogConstants.EVENT_LOGS_FAMILY_NAME.getBytes(),
EventLogConstants.LOG_COLUMN_NAME_BROWSER_NAME.getBytes()));
String browserVersion = Bytes.toString(value.getValue(EventLogConstants.EVENT_LOGS_FAMILY_NAME.getBytes(),
EventLogConstants.LOG_COLUMN_NAME_BROWSER_VERSION.getBytes()));
//构造浏览器维度的集合对象
browserDimensionList = BrowserDimension.buildList(browserName,browserVersion);
/*//用户基本信息新增用户 key:date,platform,kpi1 value:uuId,stime
for(PlatformDimension platformDimension:platformDimensionList){
statsCommon.setPlatform(platformDimension);
statsCommon.setKpi(kpiNewInstallUser);
statsUserDimesion.setBrowser(defaultBrowserDimension);
//输出
context.write(statsUserDimesion,valueOut);
}
//浏览器维度的新增用户 key:date,platform,kpi2,browser value:uuId,stime
for(PlatformDimension platformDimension:platformDimensionList){
statsCommon.setPlatform(platformDimension);
statsCommon.setKpi(kpiBrowserNewInstallUser);
//遍历浏览器集合对象
for(BrowserDimension browserDimension:browserDimensionList){
statsUserDimesion.setBrowser(browserDimension);
//输出
context.write(statsUserDimesion,valueOut);
}
}*/
//优化代码
//用户基本信息新增用户 key:date,platform,kpi1 value:uuId,stime
//浏览器维度的新增用户 key:date,platform,kpi2,browser value:uuId,stime
for(PlatformDimension platformDimension:platformDimensionList){
statsCommon.setPlatform(platformDimension);
statsCommon.setKpi(kpiNewInstallUser);
statsUserDimesion.setBrowser(defaultBrowserDimension);
//输出 -用户基本信息新增用户
context.write(statsUserDimesion,valueOut);
//遍历浏览器集合对象
for(BrowserDimension browserDimension:browserDimensionList){
statsCommon.setKpi(kpiBrowserNewInstallUser);
statsUserDimesion.setBrowser(browserDimension);
//输出-浏览器维度的新增用户
context.write(statsUserDimesion,valueOut);
}
}
}
}