Hive支持自定义map与reduce script。接下来我用一个简单的wordcount例子加以说明。

如果自己使用Java开发,需要处理System.in,System,out以及key/value的各种逻辑,比较麻烦。有人开发了一个小框架,可以让我们使用与Hadoop中map与reduce相似的写法,只关注map与reduce即可。如今此框架已经集成在Hive中,就是$HIVE_HOME/lib/hive-contrib-2.3.0.jar,hive版本不同,对应的contrib名字可能不同。

开发工具:intellij
JDK:jdk1.7
hive:2.3.0
hadoop:2.8.1

一、开发map与reduce

“map类
public class WordCountMap {
public static void main(String args[]) throws Exception{
new GenericMR().map(System.in, System.out, new Mapper() {
@Override
public void map(String[] strings, Output output) throws Exception {
for(String str:strings){
String[] strs=str.split("\\W+");//如果源文本文件是以\t分隔的,则不需要再拆分,传入的strings就是每行拆分好的单词
for(String str_2:strs) {
output.collect(new String[]{str_2, "1"});
}
}
}
});
}
}
"reduce类
public class WordCountReducer {
public static void main(String args[]) throws Exception{
new GenericMR().reduce(System.in, System.out, new Reducer() {
@Override
public void reduce(String s, Iterator<String[]> iterator, Output output) throws Exception {
int sum=0;
while(iterator.hasNext()){
Integer count=Integer.valueOf(iterator.next()[1]);
sum+=count;
}
output.collect(new String[]{s,String.valueOf(sum)});
}
});
}
}

二、导出jar包

然后导出Jar包(包含hive-contrib-2.3.0),假如导出jar包名为wordcount.jar

 
File->Project Structure
 
 
add Artifacts
 

不用填写Main Class,直接点击OK
 

jar包配置

 
生成jar包
 

三、编写hive sql

drop table if exists raw_lines;

-- create table raw_line, and read all the lines in '/user/inputs', this is the path on your local HDFS
create external table if not exists raw_lines(line string)
ROW FORMAT DELIMITED
stored as textfile
location '/user/inputs'; drop table if exists word_count; -- create table word_count, this is the output table which will be put in '/user/outputs' as a text file, this is the path on your local HDFS create external table if not exists word_count(word string, count int)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
lines terminated by '\n' STORED AS TEXTFILE LOCATION '/user/outputs/'; -- add the mapper&reducer scripts as resources, please change your/local/path
--must use "add file",not "add jar",or,hive won't find map and reduce main class
add file your/local/path/wordcount.jar; from (
from raw_lines
map raw_lines.line
--call the mapper here
using 'java -cp wordcount.jar WordCountMap'
as word, count
cluster by word) map_output
insert overwrite table word_count
reduce map_output.word, map_output.count
--call the reducer here
using 'java -cp wordcount.jar WordCountReducer'
as word,count;

此hive sql保存为wordcount.hql

四、执行hive sql

beeline -u [hiveserver] -n username -f wordcount.hql

简单说下Hive的自定义map与reduce内部原理:
hive读取文本文件,然后将其一行行输入系统标准输入中,用户自定义的Map读取标准输入流中数据,一行行处理,然后将其按照一定格式(例如:"key\tvalue")输出到标准输出流中,然后hive会将输出的字符串进行排序,然后再送到标准输入流中,Reduce再从标准输入流中读取数据进行相应处理,处理完成后,再送到标准输出流中,Hive再对Reduce结果进行处理存入表中。

Hive中自定义Map/Reduce示例 In Java的更多相关文章

  1. Hive中自定义Map/Reduce示例 In Python

    Hive支持自定义map与reduce script.接下来我用一个简单的wordcount例子加以说明.使用Python开发(如果使用Java开发,请看这里). 开发环境: python:2.7.5 ...

  2. Hive中自定义函数

    Hive的自定义的函数的步骤: 1°.自定义UDF extends org.apache.hadoop.hive.ql.exec.UDF 2°.需要实现evaluate函数,evaluate函数支持重 ...

  3. ES6中的Map集合(与java里类似)

    Set类型可以用来处理列表中的值,但是不适用于处理键值对这样的信息结构.ES6也添加了Map集合来解决类似的问题 一.Map集合 JS的对象(Object),本质上是键值对的集合(Hash结构),但是 ...

  4. perl编程中的map函数示例

    转自:http://www.jbxue.com/article/14854.html 发布:脚本学堂/Perl  编辑:JB01   2013-12-20 10:20:01  [大 中 小] 本文介绍 ...

  5. Hadoop Map/Reduce 示例程序WordCount

    #进入hadoop安装目录 cd /usr/local/hadoop #创建示例文件:input #在里面输入以下内容: #Hello world, Bye world! vim input #在hd ...

  6. Python中的Map/Reduce

    MapReduce是一种函数式编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都是从函数 ...

  7. Hive中自定义序列化器(带编码)

    hive SerDe的简介 https://www.jianshu.com/p/afee9acba686 问题 数据文件为文本文件,每一行为固定格式,每一列的长度都是定长或是有限制范围,考虑采用hiv ...

  8. python 中的map(), reduce(), filter

    据说是函数式编程的一个函数(然后也有人tucao py不太适合干这个),在我看来算是pythonic的一种写法. 简化了我们的操作,比方我们想将list中的数字都加1,最基本的可能是编写一个函数: I ...

  9. Python中 filter | map | reduce | lambda的用法

      1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...

随机推荐

  1. [翻译]NUnit---Range and Repeat Attributes(十五)

    RangeAttribute (NUnit 2.5) Range特性用于为参数话测试方法的参数的值范围指定一个值,与Random特性一样,NUnit会将每个参数的值组合为一些了测试用例,所以如果为一个 ...

  2. AI_ 视频监控-人体移动捕捉监测

    总目录地址:AI 系列 总目录 需要最新源码,或技术提问,请加QQ群:538327407 我的各种github 开源项目和代码:https://github.com/linbin524 需求 为了实现 ...

  3. 纸壳CMS替换默认实现

    简介 纸壳CMS是一个开源免费的可视化内容管理建站系统,拖拽就可以轻松建网站. GitHub: http://github.com/SeriaWei/ZKEACMS 纸壳CMS在设计上使用的是ASP. ...

  4. .net core 与ELK(4)后台运行els可视化工具和Kibana

    which nohup .bash_profile中并source加载 如果没有就安装吧 yum provides */nohup nohup npm run start & nohup ./ ...

  5. MVVM Light 新手入门(2) :ViewModel / Model 中定义“属性” ,并在View中调用

    今天学习MVVM架构中“属性”的添加并调用,特记录如下,学习资料均来自于网络,特别感谢翁智华的利刃 MVVMLight系列. 一个窗口的基本模型如下: View(视图) -> ViewModel ...

  6. C#八大排序算法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. python 中 使用sys模块 获取运行脚本时在命令行输入的参数

    在python项目的开发的过程中, 经常需要运行各种python脚本, 有时候还需要根据不同的使用情况输入不同的参数, 如果每次都去编辑一下脚本那就太麻烦,太耗费时间了, 这时就可以使用Python自 ...

  8. ajax post 400 bad request

    是前端ajax没有加声明:contentType:'application/json',

  9. elasticsearch中 refresh 和flush区别

    elasticsearch中有两个比较重要的操作:refresh 和 flush refresh操作 当我们向ES发送请求的时候,我们发现es貌似可以在我们发请求的同时进行搜索.而这个实时建索引并可以 ...

  10. java 中什么是aop

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...