操作代码(提前启动集群(start-all.sh)、zookeeper(zkServer.sh start)、启动历史任务服务器(mr-jobhistory-daemon.sh start historyserver)、hbase(start-hbase.sh start)

然后在hbase中创建表

create 'eventlog','log';

  1. AnalyserLogDataRunner

下边内容有可能会报错,添加如下两句

  1. configuration.set("hbase.master", "master:60000");
    configuration.set("hbase.zookeeper.property.clientPort", "2181");

获取输入路径,下面这样设置也可以,表现形式不同而已

  1. AnalyserLogDataMapper

}

上述生成的主键是很长的,经过crc32使得他们不至于那么长

  1. package com.yjsj.etl.mr;
  2.  
  3. import com.yjsj.common.EventLogConstants;
  4. import com.yjsj.common.GlobalConstants;
  5. import com.yjsj.util.TimeUtil;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.apache.hadoop.conf.Configuration;
  8. import org.apache.hadoop.fs.FileSystem;
  9. import org.apache.hadoop.fs.Path;
  10. import org.apache.hadoop.hbase.HBaseConfiguration;
  11. import org.apache.hadoop.hbase.client.Put;
  12. import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
  13. import org.apache.hadoop.io.NullWritable;
  14. import org.apache.hadoop.mapreduce.Job;
  15. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  16. import org.apache.hadoop.util.Tool;
  17. import org.apache.hadoop.util.ToolRunner;
  18.  
  19. //import java.util.logging.Logger;
  20. import org.apache.log4j.Logger;
  21.  
  22. import java.io.IOException;
  23.  
  24. public class AnalyserLogDataRunner implements Tool {
  25. //public static final Logger log=Logger.getGlobal();
  26. public static final Logger log=Logger.getLogger(AnalyserLogDataRunner.class);
  27. //注意这次用的是log4j的日志
  28. private Configuration conf=null;
  29.  
  30. public static void main(String[] args) {
  31. try {
  32. ToolRunner.run(new Configuration(),new AnalyserLogDataRunner(),args);
  33. } catch (Exception e) {
  34. log.error("执行日志解析job异常",e);
  35. throw new RuntimeException(e);
  36. }
  37. }
  38.  
  39. @Override
  40. public Configuration getConf() {
  41. return this.conf;
  42. }
  43.  
  44. @Override
  45. public void setConf(Configuration configuration) {
  46.  
  47. configuration.set("hbase.zookeeper.quorum", "master,node1,node2");
  48. configuration.set("fs.defaultFS","hdfs://master:9000");
  49. configuration.set("hbase.master", "master:60000");
  50. configuration.set("hbase.zookeeper.property.clientPort", "2181");
  51. this.conf=HBaseConfiguration.create(configuration);
  52.  
  53. }
  54.  
  55. @Override
  56. public int run(String[] args) throws Exception {
  57. Configuration conf=this.getConf();
  58. this.processArgs(conf,args);
  59. Job job=Job.getInstance(conf,"analyser_logdata");
  60. //设置本地提交job,集群运行,需要代码
  61. //File jarFile=EJob.createTempJar("target/classes");
  62. //((JobCong) job.getConfiguration()).setJar(jarFile.toString());
  63. //设置本地提交,集群运行,需要代码结束
  64. job.setJarByClass(AnalyserLogDataRunner.class);
  65. job.setMapperClass(AnalyserLogDataMapper.class);
  66. job.setMapOutputKeyClass(NullWritable.class);
  67. job.setMapOutputValueClass(Put.class);
  68. //设置reduce配置
  69. //1.集群上运行,打成jar运行(要求addDependencyJars参数为true,默认为true)
  70. //TableMapReduceUtil.initTableReduceJob(EventLogConstants.HBASE_NAME_EVENT_LOGS,null,job);
  71. //2、本地运行,要求参数为addDependencyJars为false
  72. TableMapReduceUtil.initTableReducerJob(EventLogConstants.HBASE_NAME_EVENT_LOGS,null,job,null,null,null,null,false);
  73. job.setNumReduceTasks(0);//上面红色是表名,封装的名为eventlog的值
  74. this.setJobInputPaths(job);
  75. return job.waitForCompletion(true)?0:-1;
  76. }
  77. private void setJobInputPaths(Job job){
  78. Configuration conf=job.getConfiguration();
  79. FileSystem fs=null;
  80. try {
  81. fs=FileSystem.get(conf);
  82. String date=conf.get(GlobalConstants.RUNNING_DATE_PARAMES);
  83. Path inputPath=new Path("/project/log/"+TimeUtil.parseLong2String(
  84. TimeUtil.parseString2Long(date),"yyyyMMdd"
  85. )+"/");
  86. if (fs.exists(inputPath)){
  87. FileInputFormat.addInputPath(job,inputPath);
  88. }else {
  89. throw new RuntimeException("文件不存在:"+inputPath);
  90. }
  91. System.out.println("*******"+inputPath.toString());
  92. } catch (IOException e) {
  93. throw new RuntimeException("设置job的mapreduce输入路径出现异常",e);
  94. }finally {
  95. if (fs!=null){
  96. try {
  97. fs.close();
  98. } catch (IOException e) {
  99. //e.printStackTrace();
  100. }
  101. }
  102. }
  103.  
  104. }
  105. private void processArgs(Configuration conf,String[] args){
  106. String date=null;
  107. for (int i=0;i<args.length;i++){
  108. if("-d".equals(args[i])){
  109. if (i+1<args.length){
  110. date=args[++i];
  111. break;
  112. }
  113. }
  114. }
  115. System.out.println("------"+date);
  116. //要求格式为yyyy-MM-dd
  117. //注意下面是org.apache.commons.lang包下面的
  118. if (StringUtils.isBlank(date)||!TimeUtil.isValidateRunningDate(date)){
  119. //date是一个无效数据
  120. date=TimeUtil.getYesterday();
  121. System.out.println(date);
  122. }
  123. conf.set(GlobalConstants.RUNNING_DATE_PARAMES,date);
  124. }
  125. }
  1. package com.yjsj.etl.mr;
  2.  
  3. import com.yjsj.common.EventLogConstants;
  4. import com.yjsj.common.GlobalConstants;
  5. import com.yjsj.etl.util.LoggerUtil;
  6. import com.yjsj.util.TimeUtil;
  7. import org.apache.commons.lang.StringUtils;
  8. import org.apache.hadoop.conf.Configuration;
  9. import org.apache.hadoop.hbase.client.Put;
  10. import org.apache.hadoop.hbase.util.Bytes;
  11. import org.apache.hadoop.io.LongWritable;
  12. import org.apache.hadoop.io.NullWritable;
  13. import org.apache.hadoop.io.Text;
  14. import org.apache.hadoop.mapreduce.Mapper;
  15. import org.apache.log4j.Logger;
  16.  
  17. import java.io.IOException;
  18. import java.util.Map;
  19. import java.util.zip.CRC32;
  20.  
  21. public class AnalyserLogDataMapper extends Mapper<LongWritable,Text,NullWritable,Put> {
  22. private final Logger logger=Logger.getLogger(AnalyserLogDataMapper.class);
  23. private int inputRecords,filterRecords,outputRecords;//用于标志,方便查看过滤数据
  24. private byte[] family=Bytes.toBytes(EventLogConstants.EVENT_LOGS_FAMILY_NAME);
  25. private CRC32 crc32=new CRC32();
  26.  
  27. @Override
  28. protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  29. this.inputRecords++;
  30. this.logger.debug("Analyse data of:"+value);
  31. try {
  32. //解析日志
  33. Map<String,String> clientInfo=LoggerUtil.handleLog(value.toString());
  34. //过滤解析失败的日志
  35. if (clientInfo.isEmpty()){
  36. this.filterRecords++;
  37. return;
  38. }
  39. String eventAliasName =clientInfo.get(EventLogConstants.LOG_COLUMN_NAME_EVENT_NAME);
  40. EventLogConstants.EventEnum event= EventLogConstants.EventEnum.valueOfAlias(eventAliasName);
  41. switch (event){
  42. case LAUNCH:
  43. case PAGEVIEW:
  44. case CHARGEREQUEST:
  45. case CHARGEREFUND:
  46. case CHARGESUCCESS:
  47. case EVENT:
  48. //处理数据
  49. this.handleData(clientInfo,event,context);
  50. break;
  51. default:
  52. this.filterRecords++;
  53. this.logger.warn("该事件无法解析,事件名称为"+eventAliasName);
  54. }
  55. } catch (Exception e) {
  56. this.filterRecords++;
  57. this.logger.error("处理数据发出异常,数据为"+value,e);
  58. }
  59. }
  60.  
  61. @Override
  62. protected void cleanup(Context context) throws IOException, InterruptedException {
  63. super.cleanup(context);
  64. logger.info("输入数据:"+this.inputRecords+"输出数据"+this.outputRecords+"过滤数据"+this.filterRecords);
  65. }
  66. private void handleData(Map<String,String> clientInfo, EventLogConstants.EventEnum event,Context context)
  67. throws IOException,InterruptedException{
  68. String uuid=clientInfo.get(EventLogConstants.LOG_COLUMN_NAME_UUID);
  69. String memberId=clientInfo.get(EventLogConstants.LOG_COLUMN_NAME_MEMBER_ID);
  70. String serverTime=clientInfo.get(EventLogConstants.LOG_COLUMN_NAME_SERVER_TIME);
  71. if (StringUtils.isNotBlank(serverTime)){
  72. //要求服务器时间不为空
  73. clientInfo.remove(EventLogConstants.LOG_COLUMN_NAME_USER_AGENT);//去掉浏览器信息
  74. String rowkey=this.generateRowKey(uuid,memberId,event.alias,serverTime);//timestamp
  75. Put put=new Put(Bytes.toBytes(rowkey));
  76. for (Map.Entry<String,String> entry:clientInfo.entrySet()){
  77. if (StringUtils.isNotBlank(entry.getKey())&&StringUtils.isNotBlank(entry.getValue())){
  78. put.add(family,Bytes.toBytes(entry.getKey()),Bytes.toBytes(entry.getValue()));
  79. }
  80. }
  81. context.write(NullWritable.get(),put);
  82. this.outputRecords++;
  83. }else {
  84. this.filterRecords++;
  85. }
  86. }
  87. private String generateRowKey(String uuid,String memberId,String eventAliasName,String serverTime){
  88. StringBuilder sb=new StringBuilder();
  89. sb.append(serverTime).append("_");
  90. this.crc32.reset();
  91. if (StringUtils.isNotBlank(uuid)){
  92. this.crc32.update(uuid.getBytes());
  93. }
  94. if (StringUtils.isNotBlank(memberId)){
  95. this.crc32.update(memberId.getBytes());
  96. }
  97. this.crc32.update(eventAliasName.getBytes());
  98. sb.append(this.crc32.getValue()%100000000L);
  99. return sb.toString();
  100. }
  101. }

通过hbase实现日志的转存(MR AnalyserLogDataRunner和AnalyserLogDataMapper)的更多相关文章

  1. HBase GC日志

    HBase依靠ZooKeeper来感知集群成员及其存活性.假设一个server暂停了非常长时间,它将无法给ZooKeeper quorum发送心跳信息,其他server会觉得这台server已死亡.这 ...

  2. 编写程序向HBase添加日志信息

    关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新) 承接上一篇文档<日志信息和浏览器信息获取及数据过滤> 上一个文档最好做个本地测试 ...

  3. flume学习以及ganglia(若是要监控hive日志,hive存放在/tmp/hadoop/hive.log里,只要运行过hive就会有)

    python3.6hdfs的使用 https://blog.csdn.net/qq_29863961/article/details/80291654 https://pypi.org/  官网直接搜 ...

  4. NoSql存储日志数据之Spring+Logback+Hbase深度集成

    NoSql存储日志数据之Spring+Logback+Hbase深度集成 关键词:nosql, spring logback, logback hbase appender 技术框架:spring-d ...

  5. <HBase><读写><LSM>

    Overview HBase中的一个big table,首先会按行划分成一些region(这些region之间是有序的,由startkey保证),每个region分配到不同的节点进行存储.因此,reg ...

  6. hbase官方文档(转)

    FROM:http://www.just4e.com/hbase.html Apache HBase™ 参考指南  HBase 官方文档中文版 Copyright © 2012 Apache Soft ...

  7. HBase官方文档

    HBase官方文档 目录 序 1. 入门 1.1. 介绍 1.2. 快速开始 2. Apache HBase (TM)配置 2.1. 基础条件 2.2. HBase 运行模式: 独立和分布式 2.3. ...

  8. hbase 的体系结构

    hbase的服务体系遵从的是主从结构,由HRegion(服务器)-HRegionServer(服务器集群)-HMaster(主服务器)构成, 从图中能看出多个HRegion 组成一个HRegionSe ...

  9. 【转】HBase 超详细介绍

    ---恢复内容开始--- http://blog.csdn.net/frankiewang008/article/details/41965543 1-HBase的安装 HBase是什么? HBase ...

随机推荐

  1. one by one 项目 part 3

    mysql error:Table 'performance_schema.session_variables' doesn't exist 打开cmd 输入:mysql_upgrade -u roo ...

  2. MongoDB服务无法启动,发生服务特定错误:100

    问题:MongoDB服务无法启动,发生服务特定错误:100 原因:没有正常关闭mongod服务,导致mongod被锁 解决方案:进入db文件夹,删除mongod.lock文件,然后重新启动服务即可

  3. 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:

    问题信息详细: 信息: The APR based Apache Tomcat Native library which allows optimal performance in productio ...

  4. 对象导航查询和OID查询(补)

    ----------------siwuxie095                                 对象导航查询         以客户和联系人为例(一对多)     1.应用场景 ...

  5. runloop - 面试题

    2.

  6. linux 下的 rsync 文件同步

    rsync是linux下的一款快速增量备份工具Remote Sync,是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限.时间.软硬链接等附加信息.rsync是用 “rsync ...

  7. 一款APP的交互文档从撰写到交付

    我第一份工作的设计总监是前百度设计师,34岁,一线设计12年:今年聊天说转了产品总监,如今39岁还活跃在行业中…… 我第二份工作的部门总监是前腾讯工程师,38岁,一线开发14年:2年前在Q群里跟我们说 ...

  8. window下装redis扩展(以php5.5为例)

    一.安装redis服务 1.下载并解压 https://github.com/dmajkic/redis/downloads 2.运行redis服务 3.检查能否正常访问  二.安装redis扩展 1 ...

  9. 编译器C1001问题

    https://ask.csdn.net/questions/184495 http://blog.sina.com.cn/s/blog_7822ce750100szed.html

  10. npm 如何设置镜像站为淘宝网

    转载 2015年06月24日 17:12:12 10542 淘宝镜像:http://npm.taobao.org/ 镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候 ...