Hive 使用RegexSerDe来处理标准格式Apache Web日志

来源:互联网 发布:js给下拉列表赋值 编辑:程序博客网 时间:2024/05/16 07:32

感谢分享:http://blog.csdn.net/niityzu/article/details/42103297

我们以一个例子来介绍如何使用RegexSerDe来处理标准格式的Apache Web日志,并对其进行统计分析。我的Hive版本是apache-hive-0.13.1-bin

一、在Hive中创建表serde_regex

[sql] view plain copy
  1. CREATE TABLE serde_regex(   
  2.           host STRING,      
  3.    identity STRING ,  
  4.    user STRING,  
  5.    time STRING,  
  6.    request STRING,  
  7.    status STRING,  
  8.    size STRING,  
  9.    referer STRING,  
  10.    agent STRING )  
  11. ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe'   
  12. WITH SERDEPROPERTIES (  
  13. "input.regex" = "([\\d|.]+)\\s+([^ ]+)\\s+([^ ]+)\\s+\\[(.+)\\]\\s+\"([^ ]+)\\s(.+)\\s([^ ]+)\"\\s+([^ ]+)\\s+([^ ]+)\\s+\"(.+)\"\\s+\"(.+)\"?",  
  14. "output.format.string" = "%1$s %2$s %3$s %4$s %5$s %6$s %7$s %8$s %9$s"  
  15.   )  
  16.    STORED AS TEXTFILE ;  

二、往表中导入数据

    Apache Web 日志内容格式见附件下载链接(可下载)

    往表中导入事例数据

[plain] view plain copy
  1. hive> LOAD DATA LOCAL INPATH "./data/apache_log.txt" INTO TABLE serde_regex;  

三、查询分析

        查询分析的时候报了以下错误:主要就是缺少RegexSerDe处理类,也就是说,hive会话环境中缺少该类所在的包。

[java] view plain copy
  1. hive> select host,request,agent from serde_regex limit 10;  
  2. Total jobs = 1  
  3. Launching Job 1 out of 1  
  4. Number of reduce tasks is set to 0 since there's no reduce operator  
  5. Starting Job = job_1419317102229_0001, Tracking URL = http://secondmgt:8088/proxy/application_1419317102229_0001/  
  6. Kill Command = /home/hadoopUser/cloud/hadoop/programs/hadoop-2.2.0/bin/hadoop job  -kill job_1419317102229_0001  
  7. Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0  
  8. 2014-12-23 14:46:15,389 Stage-1 map = 0%,  reduce = 0%  
  9. 2014-12-23 14:47:02,249 Stage-1 map = 100%,  reduce = 0%  
  10. Ended Job = job_1419317102229_0001 with errors  
  11. Error during job, obtaining debugging information...  
  12. Job Tracking URL: http://secondmgt:8088/proxy/application_1419317102229_0001/  
  13. Examining task ID: task_1419317102229_0001_m_000000 (and more) from job job_1419317102229_0001  
  14.   
  15. Task with the most failures(4):  
  16. -----  
  17. Task ID:  
  18.   task_1419317102229_0001_m_000000  
  19.   
  20. URL:  
  21.   http://secondmgt:8088/taskdetails.jsp?jobid=job_1419317102229_0001&tipid=task_1419317102229_0001_m_000000  
  22. -----  
  23. Diagnostic Messages for this Task:  
  24. Error: java.lang.RuntimeException: Error in configuring object  
  25.         at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:109)  
  26.         at org.apache.hadoop.util.ReflectionUtils.setConf(ReflectionUtils.java:75)  
  27.         at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:133)  
  28.         at org.apache.hadoop.mapred.MapTask.runOldMapper(MapTask.java:425)  
  29.         at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)  
  30.         at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:162)  
  31.         at java.security.AccessController.doPrivileged(Native Method)  
  32.         at javax.security.auth.Subject.doAs(Subject.java:415)  
  33.         at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1491)  
  34.         at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:157)  
  35. Caused by: java.lang.reflect.InvocationTargetException  
  36.         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  37.         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)  
  38.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
  39.         at java.lang.reflect.Method.invoke(Method.java:606)  
  40.         at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:106)  
  41.         ... 9 more  
  42. Caused by: java.lang.RuntimeException: Error in configuring object  
  43.         at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:109)  
  44.         at org.apache.hadoop.util.ReflectionUtils.setConf(ReflectionUtils.java:75)  
  45.         at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:133)  
  46.         at org.apache.hadoop.mapred.MapRunner.configure(MapRunner.java:38)  
  47.         ... 14 more  
  48. Caused by: java.lang.reflect.InvocationTargetException  
  49.         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  50.         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)  
  51.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
  52.         at java.lang.reflect.Method.invoke(Method.java:606)  
  53.         at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:106)  
  54.         ... 17 more  
  55. Caused by: java.lang.RuntimeException: Map operator initialization failed  
  56.         at org.apache.hadoop.hive.ql.exec.mr.ExecMapper.configure(ExecMapper.java:154)  
  57.         ... 22 more  
  58. Caused by: org.apache.hadoop.hive.ql.metadata.HiveException: java.lang.ClassNotFoundException: Class org.apache.hadoop.hive.contrib.serde2.RegexSerDe not found  
  59.         at org.apache.hadoop.hive.ql.exec.MapOperator.getConvertedOI(MapOperator.java:335)  
  60.         at org.apache.hadoop.hive.ql.exec.MapOperator.setChildren(MapOperator.java:353)  
  61.         at org.apache.hadoop.hive.ql.exec.mr.ExecMapper.configure(ExecMapper.java:123)  
  62.         ... 22 more  
  63. Caused by: java.lang.ClassNotFoundException: <span style="color:#ff0000;">Class org.apache.hadoop.hive.contrib.serde2.RegexSerDe not found</span>  
  64.         at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:1626)  
  65.         at org.apache.hadoop.hive.ql.exec.MapOperator.getConvertedOI(MapOperator.java:305)  
  66.         ... 24 more  

四、异常解决办法

      往hive会话中加入 hive-contrib-0.13.1.jar,该包位置在hive安装环境的lib目录下,加入命令如下:

[html] view plain copy
  1. hive> add jar /home/hadoopUser/cloud/hive/apache-hive-0.13.1-bin/lib/hive-contrib-0.13.1.jar;  
  2. Added /home/hadoopUser/cloud/hive/apache-hive-0.13.1-bin/lib/hive-contrib-0.13.1.jar to class path  
  3. Added resource: /home/hadoopUser/cloud/hive/apache-hive-0.13.1-bin/lib/hive-contrib-0.13.1.jar  

   再次执行第三步的SELECT查询命令,结果如下:

[html] view plain copy
  1. hive> select host,request,agent from serde_regex limit 10;  
  2. Total jobs = 1  
  3. Launching Job 1 out of 1  
  4. Number of reduce tasks is set to 0 since there's no reduce operator  
  5. Starting Job = job_1419317102229_0002, Tracking URL = http://secondmgt:8088/proxy/application_1419317102229_0002/  
  6. Kill Command = /home/hadoopUser/cloud/hadoop/programs/hadoop-2.2.0/bin/hadoop job  -kill job_1419317102229_0002  
  7. Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0  
  8. 2014-12-23 14:48:50,163 Stage-1 map = 0%,  reduce = 0%  
  9. 2014-12-23 14:49:01,666 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 3.31 sec  
  10. MapReduce Total cumulative CPU time: 3 seconds 310 msec  
  11. Ended Job = job_1419317102229_0002  
  12. MapReduce Jobs Launched:  
  13. Job 0: Map: 1   Cumulative CPU: 3.31 sec   HDFS Read: 4321 HDFS Write: 238 SUCCESS  
  14. Total MapReduce CPU Time Spent: 3 seconds 310 msec  
  15. OK  
  16. 61.160.224.138  GET     7519  
  17. 61.160.224.138  GET     709  
  18. 61.160.224.138  GET     815  
  19. 113.17.174.44   POST    653  
  20. 61.160.224.138  GET     1670  
  21. 61.160.224.144  GET     2887  
  22. 61.160.224.143  GET     2947  
  23. 61.160.224.145  GET     2581  
  24. 61.160.224.145  GET     2909  
  25. 61.160.224.144  GET     15879  
  26. Time taken: 26.811 seconds, Fetched: 10 row(s)  

问题解决,但是该解决方法只能对本次Hive会话有用,Hive使用命令exit退出后再进入依旧会出现该问题。

遗留问题:个人觉得应该有一种办法,将该包加入到Hadoop/lib下,重启集群,可以长久解决该问题。但是没尝试过,待验证。



阅读全文
0 0
原创粉丝点击