`
wbj0110
  • 浏览: 1586428 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

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

    博客分类:
  • Hive
阅读更多

为何要设置此功能是由于 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 

 

 

  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. }    



 

 

 

  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 plaincopyprint?
 
  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 打包 类文件

 

  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';  

http://blog.csdn.net/iquicksandi/article/details/8533699

 

大家可以加我个人微信号:scccdgf

 

 

或者关注soledede的微信公众号:soledede
微信公众号:
分享到:
评论

相关推荐

    hive inputformat

    Hive的InputFormat允许我们灵活地处理各种数据源和格式,通过自定义InputFormat,我们可以按需定制数据读取逻辑,比如这里演示的按照空格拆分日志文件。理解并掌握InputFormat对于优化Hive查询性能和处理复杂数据...

    hive 创建自定义函数 和 hive加载说明

    这篇博文主要探讨了如何在Hive中创建自定义函数以及如何加载它们,这对于深化Hive的使用和解决复杂的数据处理问题至关重要。 首先,我们来看一下创建自定义函数的过程。在Hive中,UDF分为三种类型:UDF(User ...

    hive的自定义函数

    ### Hive的自定义函数(UDF)详解 #### 一、引言 在大数据处理领域,Apache Hive 是一个广泛使用的数据仓库工具,它提供了一种SQL-like查询语言——HiveQL,使用户能够轻松地对存储在Hadoop文件系统中的大规模数据...

    hive数仓、hive SQL 、 hive自定义函数 、hive参数深入浅出

    Hive是大数据处理领域中的一个关键组件,它作为基于Hadoop的数据仓库工具,为企业提供了对大规模数据集的SQL-like查询和分析能力。本教程将深入探讨Hive数仓的架构与设计,Hive SQL的基本语法及高级特性,以及如何...

    Spark不能使用hive自定义函数.doc

    ### Spark与Hive自定义函数兼容性问题解析 在大数据处理领域,Apache Spark 和 Apache Hive 都是非常重要的工具。Spark 是一种快速通用的大规模数据处理系统,而Hive 则是一种数据仓库工具,主要用于对存储在 ...

    hive2.0+开启审计日志.pdf

    在Hive 2.0及更高版本中,启用Metastore审计日志是确保数据安全性与合规性的重要步骤。审计日志记录了用户对Hive Metastore的所有操作,包括元数据的创建、修改和查询等,这对于追踪系统活动、故障排查以及满足法规...

    基于Hive的搜狗日志分析

    基于Hive的搜狗日志分析 本文档主要介绍了基于Hive的搜狗日志分析的整个过程,从数据预处理、构建数据仓库、数据分析到其他数据操作等方面进行了详细的介绍。 一、 数据预处理 数据预处理是整个日志分析的第一步...

    05.hive中如何自定义函数--json解析函数示例.mp4

    05.hive中如何自定义函数--json解析函数示例.mp4

    Hive多字节分隔符解决方案.docx

    自定义InputFormat是Hive提供的一种机制,允许用户自定义数据加载的方式。使用自定义InputFormat,可以实现特殊的数据加载逻辑,以适应复杂的数据格式。 本文总结了Hive多字节分隔符解决方案的三个方法,包括替换...

    自定义hive函数

    综上所述,自定义Hive函数(UDF、UDAF)极大地增强了Hive的功能,使其能处理各种复杂的业务逻辑,适应不同的数据处理需求。开发和使用这些自定义函数时,需要理解Hive的生命周期、执行模型以及如何将Java代码集成到...

    HIVE自定义UDF函数

    Hive 是一个基于 Hadoop 的数据仓库工具,用于处理大规模数据集。它使用类似 SQL 的语言 HiveQL 来查询和管理数据。而自定义用户定义函数(UDF)是 Hive 中的一个重要功能,允许用户根据自己的需求编写自定义函数,...

    hive自定义UDF编写函数.docx

    Hive 自定义 UDF 编写函数 本文主要讲解了 Hive 中自定义 UDF 函数的编写方法,包括创建 UDF 类、实现自定义函数逻辑、编译和打包 UDF jar 包、上传至 Hive 服务器并注册自定义函数。 一、创建 UDF 类 为了实现...

    hive自定义函数demo

    Hive自定义函数(User Defined Function,UDF)是用户编写并集成到Hive系统中的函数,用来处理Hive不内置支持的特定计算或转换任务。UDF接受单个输入参数并返回一个结果,非常适合进行简单的数据转换和计算。 2. *...

    hive UDF需要jar包

    在Hive中,UDF(User Defined Functions)是用户自定义函数,允许开发人员扩展Hive的内置功能,以满足特定的数据处理需求。Hive UDF的实现通常涉及到编写Java代码,并将其打包成JAR(Java Archive)文件,然后在Hive...

    hive-udf:hive自定义函数

    hive-udfhive自定义函数主要实现hive3种自定义函数1,udf函数,主要用于处理一对一数据处理2,udtf函数,主要用于处理一对多数据处理2,udaf函数,主要用与处理多对一数据聚合处理

    基于Hadoop/Hive的web日志分析系统的设计

    Hadoop/Hive系统通过HDFS存储Web日志数据,并通过Hive处理这些数据,最终实现日志分析的功能。 设计Web日志分析系统时,需要考虑到以下几个核心功能模块: 1. 日志采集模块:负责实时或定时从Web服务器获取日志...

    论文研究-基于Hive的海量搜索日志分析系统研究.pdf

    针对传统分布式模型在海量日志并行处理时的可扩展性和并行程序编写困难的问题, 提出了基于Hive的Web海量搜索日志分析机制。利用HQL语言以及Hadoop分布式文件系统(HDFS)和MapReduce编程模式对海量搜索日志进行分析...

    设计开发 Hive 编程指南 完整版

    《设计开发 Hive 编程指南 完整版》是一份详尽的教程,旨在帮助开发者深入理解和高效使用 Apache Hive 进行大数据处理。Hive 是一个基于 Hadoop 的数据仓库工具,可将结构化的数据文件映射为一张数据库表,并提供 ...

Global site tag (gtag.js) - Google Analytics