HIVE 处理日志,自定义inputformat 完整版

网上找了很多材料都是写了部份代码的,今天在峰哥的帮助下实现了此功能。


为何要设置此功能是由于 hive fields terminated by '||||' 不支持 字符串导致


将你的inputformat类打成jar包,如MyInputFormat.jar
将MyInputFormat.jar放到 hive/lib里,然后就可以建表了
假设你的inputFormat类路径是com.hive.myinput
则建表语句为:create table tbname(name stirng,id int, ...) stored as INPUTFORMAT 'com.hive.myinput'   OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'

HiveIgnoreKeyTextOutputFormat是系统自带的outputformat类,你也可以自定义

由于hive是基于hadoop集群运行的,所以hadoop/lib里面也必须放入MyInputFormat.jar,


此功能需要二个CLASS 类:ClickstreamInputFormat ClickstreamRecordReader 


[java]  view plain copy print ?
  1. package com.jd.cloud.clickstore;  
  2.   
  3. import java.io.IOException;    
  4.   
  5. import org.apache.hadoop.io.LongWritable;    
  6. import org.apache.hadoop.io.Text;    
  7. import org.apache.hadoop.mapred.FileSplit;    
  8. import org.apache.hadoop.mapred.InputSplit;    
  9. import org.apache.hadoop.mapred.JobConf;    
  10. import org.apache.hadoop.mapred.JobConfigurable;    
  11. import org.apache.hadoop.mapred.RecordReader;    
  12. import org.apache.hadoop.mapred.Reporter;    
  13. import org.apache.hadoop.mapred.TextInputFormat;  
  14.   
  15. /**  
  16.  * 自定义hadoop的 org.apache.hadoop.mapred.InputFormat  
  17.  *   
  18.  * @author winston  
  19.  *   
  20.  */    
  21. public class ClickstreamInputFormat extends TextInputFormat implements    
  22.         JobConfigurable {    
  23.     
  24.     public RecordReader<LongWritable, Text> getRecordReader(    
  25.             InputSplit genericSplit, JobConf job, Reporter reporter)    
  26.             throws IOException {    
  27.     
  28.         reporter.setStatus(genericSplit.toString());    
  29.         return new ClickstreamRecordReader((FileSplit) genericSplit,job);    
  30.     }    
  31. }    



[java]  view plain copy print ?
  1. package com.jd.cloud.clickstore;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.fs.FSDataInputStream;  
  7. import org.apache.hadoop.fs.FileSystem;  
  8. import org.apache.hadoop.fs.Path;  
  9. import org.apache.hadoop.io.LongWritable;  
  10. import org.apache.hadoop.io.Text;  
  11. import org.apache.hadoop.io.compress.CompressionCodec;  
  12. import org.apache.hadoop.io.compress.CompressionCodecFactory;  
  13. import org.apache.hadoop.mapred.FileSplit;  
  14. import org.apache.hadoop.util.LineReader;  
  15. import org.apache.hadoop.mapred.RecordReader;  
  16.   
  17.   
  18. public class ClickstreamRecordReader implements  
  19.         RecordReader<LongWritable, Text> {  
  20.   
  21.   
  22.     private CompressionCodecFactory compressionCodecs = null;  
  23.     private long start;  
  24.     private long pos;  
  25.     private long end;  
  26.     private LineReader lineReader;  
  27.     int maxLineLength;  
  28.   
  29.     public ClickstreamRecordReader(FileSplit inputSplit, Configuration job)  
  30.             throws IOException {  
  31.         maxLineLength = job.getInt("mapred.ClickstreamRecordReader.maxlength",  
  32.                 Integer.MAX_VALUE);  
  33.         start = inputSplit.getStart();  
  34.         end = start + inputSplit.getLength();  
  35.         final Path file = inputSplit.getPath();  
  36.         compressionCodecs = new CompressionCodecFactory(job);  
  37.         final CompressionCodec codec = compressionCodecs.getCodec(file);  
  38.   
  39.         // Open file and seek to the start of the split  
  40.         FileSystem fs = file.getFileSystem(job);  
  41.         FSDataInputStream fileIn = fs.open(file);  
  42.         boolean skipFirstLine = false;  
  43.         if (codec != null) {  
  44.             lineReader = new LineReader(codec.createInputStream(fileIn), job);  
  45.             end = Long.MAX_VALUE;  
  46.         } else {  
  47.             if (start != 0) {  
  48.                 skipFirstLine = true;  
  49.                 --start;  
  50.                 fileIn.seek(start);  
  51.             }  
  52.             lineReader = new LineReader(fileIn, job);  
  53.         }  
  54.         if (skipFirstLine) {  
  55.             start += lineReader.readLine(new Text(), 0,  
  56.                     (int) Math.min((long) Integer.MAX_VALUE, end - start));  
  57.         }  
  58.         this.pos = start;  
  59.     }  
  60.   
  61.     public ClickstreamRecordReader(InputStream in, long offset, long endOffset,  
  62.             int maxLineLength) {  
  63.         this.maxLineLength = maxLineLength;  
  64.         this.lineReader = new LineReader(in);  
  65.         this.start = offset;  
  66.         this.pos = offset;  
  67.         this.end = endOffset;  
  68.     }  
  69.   
  70.     public ClickstreamRecordReader(InputStream in, long offset, long endOffset,  
  71.             Configuration job) throws IOException {  
  72.         this.maxLineLength = job.getInt(  
  73.                 "mapred.ClickstreamRecordReader.maxlength", Integer.MAX_VALUE);  
  74.         this.lineReader = new LineReader(in, job);  
  75.         this.start = offset;  
  76.         this.pos = offset;  
  77.         this.end = endOffset;  
  78.     }  
  79.   
  80.     public LongWritable createKey() {  
  81.         return new LongWritable();  
  82.     }  
  83.   
  84.     public Text createValue() {  
  85.         return new Text();  
  86.     }  
  87.   
  88.     /** 
  89.      * Reads the next record in the split. get usefull fields from the raw nginx 
  90.      * log. 
  91.      *  
  92.      * @param key 
  93.      *            key of the record which will map to the byte offset of the 
  94.      *            record's line 
  95.      * @param value 
  96.      *            the record in text format 
  97.      * @return true if a record existed, false otherwise 
  98.      * @throws IOException 
  99.      */  
  100.     public synchronized boolean next(LongWritable key, Text value)  
  101.             throws IOException {  
  102.         // Stay within the split  
  103.         while (pos < end) {  
  104.             key.set(pos);  
  105.             int newSize = lineReader.readLine(value, maxLineLength,  
  106.                     Math.max((int) Math.min(Integer.MAX_VALUE, end - pos),  
  107.                             maxLineLength));  
  108.   
  109.             if (newSize == 0)  
  110.                 return false;  
  111.   
  112.             String str = value.toString().toLowerCase()  
  113.                     .replaceAll("\\@\\_\\@""\001");  
  114.             value.set(str);  
  115.             pos += newSize;  
  116.   
  117.             if (newSize < maxLineLength)  
  118.                 return true;  
  119.         }  
  120.   
  121.         return false;  
  122.     }  
  123.   
  124.     public float getProgress() {  
  125.         if (start == end) {  
  126.             return 0.0f;  
  127.         } else {  
  128.             return Math.min(1.0f, (pos - start) / (float) (end - start));  
  129.         }  
  130.     }  
  131.   
  132.     public synchronized long getPos() throws IOException {  
  133.         return pos;  
  134.     }  
  135.   
  136.     public synchronized void close() throws IOException {  
  137.         if (lineReader != null)  
  138.             lineReader.close();  
  139.     }  
  140.       
  141.     // 测试 输出  
  142.     //public static void main(String ags[]){  
  143.     //    String str1 ="123@_@abcd@_@fk".replaceAll("\\@\\_\\@", "\001");  
  144.     //    System.out.println(str1);  
  145.     //}  
  146. }  




1.上传到 HIVE 服务器上 JAVAC 编译

[plain]  view plain copy print ?
  1. javac -cp ./:/usr/lib/hadoop/hadoop-common.jar:/home/op1/hadoop/hadoop-core-1.0.3.jar:/usr/lib/hadoop/lib/commons-logging-1.1.1.jar */**/*/*/*  


2.JAR 打包 类文件

[java]  view plain copy print ?
  1. jar -cf ClickstreamInputFormat.jar /home/op1/uerdwdb/src/  

3.复制 Hive/lib Hadoop/lib 文件夹内


4.Hive 创建表命令

  1. create table hive_text(num int,name string,`add` string)  
  2. stored as INPUTFORMAT 'com.jd.cloud.clickstore.ClickstreamInputFormat'   
  3. OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'   
  4. location '/home/op1/uerdwdb/text.txt';  
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值