Hive中自定义Map/Reduce示例 In Java
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




三、编写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的更多相关文章
- Hive中自定义Map/Reduce示例 In Python
Hive支持自定义map与reduce script.接下来我用一个简单的wordcount例子加以说明.使用Python开发(如果使用Java开发,请看这里). 开发环境: python:2.7.5 ...
- Hive中自定义函数
Hive的自定义的函数的步骤: 1°.自定义UDF extends org.apache.hadoop.hive.ql.exec.UDF 2°.需要实现evaluate函数,evaluate函数支持重 ...
- ES6中的Map集合(与java里类似)
Set类型可以用来处理列表中的值,但是不适用于处理键值对这样的信息结构.ES6也添加了Map集合来解决类似的问题 一.Map集合 JS的对象(Object),本质上是键值对的集合(Hash结构),但是 ...
- perl编程中的map函数示例
转自:http://www.jbxue.com/article/14854.html 发布:脚本学堂/Perl 编辑:JB01 2013-12-20 10:20:01 [大 中 小] 本文介绍 ...
- Hadoop Map/Reduce 示例程序WordCount
#进入hadoop安装目录 cd /usr/local/hadoop #创建示例文件:input #在里面输入以下内容: #Hello world, Bye world! vim input #在hd ...
- Python中的Map/Reduce
MapReduce是一种函数式编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都是从函数 ...
- Hive中自定义序列化器(带编码)
hive SerDe的简介 https://www.jianshu.com/p/afee9acba686 问题 数据文件为文本文件,每一行为固定格式,每一列的长度都是定长或是有限制范围,考虑采用hiv ...
- python 中的map(), reduce(), filter
据说是函数式编程的一个函数(然后也有人tucao py不太适合干这个),在我看来算是pythonic的一种写法. 简化了我们的操作,比方我们想将list中的数字都加1,最基本的可能是编写一个函数: I ...
- Python中 filter | map | reduce | lambda的用法
1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...
随机推荐
- 射线与平面的相交检测(Ray-Plane intersection test)【转】
射线的定义 在欧几里德几何中,射线的定义是:直线上一点和它一旁的部分.由此可知,射线有两个性质,一是只有一个端点,二是一端无限延伸. 射线的参数方程 其中p0是射线的起点, u是射线的方向向量,t & ...
- .NET Core 运行时标识符 (RID) 目录
RID 是什么? RID 是运行时标识符的缩写. RID 用于标识其中将运行应用程序或资产(即程序集)的目标操作系统. 其外观类似如下:“ubuntu.14.04-x64”.“win7-x64”.“o ...
- Spring Batch学习笔记(一)
Spring Batch简介 Spring Batch提供了可重复使用的功能,用来处理大量数据.包括记录.跟踪,事务管理,作业处理统计,作业重启,跳过和资源管理. 此外还提供了更高级的技术服务和功能, ...
- php CI框架log写入
1.首先,打开application下的config.php文件,将log配置打开如下 /* |---------------------------------------------------- ...
- silverlight chart 折线图 的线颜色如何修改???
silverlight chart 折线图 的线颜色如何修改??? 我做出来都是这些偏黄色,请问如何修改线的颜色,以及线的宽度?谢谢
- LINQ to SQL语句大全
LINQ to SQL语句大全 LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判 ...
- STL在数组算法的使用
find(a:起始位置 , b: 终止位置 , c: 要查找的内容) ------>查找寻找内容的位置 count(a:起始位置 , b: 终止位置 , c: 要查找的内容) -- ...
- socket agent统一模板
# -*- coding: utf- -*- # data:-- : # user:DIY # file:agent_eay.py import socket def work(i): sock = ...
- jzoj100031
這題是結論題 好玄學,不懂 代碼: #include<bits/stdc++.h> using namespace std; struct no{ int v,p; bool operat ...
- 静态分析第三发 so文件分析(小黄人快跑)
本文作者:i春秋作家——HAI_ 0×00 工具 1.IDA pro 2.Android Killer 0×01 环境 小黄人快跑 下载地址http://download.csdn.net/downl ...