Hive GenericUDF2
再来看一个分数统计的小例子。
在Hive中存在如下一张表:
- hive> describe tb_test2;
- OK
- name string
- score_list array<map<string,int>>
- Time taken: 0.074 seconds
- hive> select * from tb_test2;
- OK
- A [{"math":100,"english":90,"history":85}]
- B [{"math":95,"english":80,"history":100}]
- C [{"math":80,"english":90,"histroy":100}]
- Time taken: 0.107 seconds
编写genericUDF.
- package com.wz.udf;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
- import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
- import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
- import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
- import org.apache.hadoop.hive.ql.metadata.HiveException;
- import org.apache.hadoop.hive.serde2.lazy.LazyString;
- import org.apache.hadoop.hive.serde2.lazy.LazyMap;
- import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
- import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
- import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
- import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
- import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
- import org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector;
- import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
- import org.apache.hadoop.hive.serde2.objectinspector.StructField;
- import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
- import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
- import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
- import java.util.ArrayList;
- public class helloGenericUDFNew extends GenericUDF {
- ////输入变量定义
- private ObjectInspector nameObj;
- private ListObjectInspector listoi;
- private MapObjectInspector mapOI;
- private ArrayList<Object> valueList = new ArrayList<Object>();
- @Override
- public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
- nameObj = (ObjectInspector)arguments[0];
- listoi = (ListObjectInspector)arguments[1];
- mapOI = ((MapObjectInspector)listoi.getListElementObjectInspector());
- //输出结构体定义
- ArrayList structFieldNames = new ArrayList();
- ArrayList structFieldObjectInspectors = new ArrayList();
- structFieldNames.add("name");
- structFieldNames.add("totalScore");
- structFieldObjectInspectors.add( PrimitiveObjectInspectorFactory.writableStringObjectInspector );
- structFieldObjectInspectors.add( PrimitiveObjectInspectorFactory.writableIntObjectInspector );
- StructObjectInspector si2;
- si2 = ObjectInspectorFactory.getStandardStructObjectInspector(structFieldNames, structFieldObjectInspectors);
- return si2;
- }
- @Override
- public Object evaluate(DeferredObject[] arguments) throws HiveException{
- LazyString LName = (LazyString)(arguments[0].get());
- String strName = ((StringObjectInspector)nameObj).getPrimitiveJavaObject( LName );
- int nelements = listoi.getListLength(arguments[1].get());
- int nTotalScore=0;
- valueList.clear();
- //遍历list
- for(int i=0;i<nelements;i++)
- {
- LazyMap LMap = (LazyMap)listoi.getListElement(arguments[1].get(),i);
- //获取map中的所有value值
- valueList.addAll(mapOI.getMap(LMap).values());
- for (int j = 0; j < valueList.size(); j++)
- {
- nTotalScore+=Integer.parseInt(valueList.get(j).toString());
- }
- }
- Object[] e;
- e = new Object[2];
- e[0] = new Text(strName);
- e[1] = new IntWritable(nTotalScore);
- return e;
- }
- @Override
- public String getDisplayString(String[] children) {
- assert( children.length>0 );
- StringBuilder sb = new StringBuilder();
- sb.append("helloGenericUDFNew(");
- sb.append(children[0]);
- sb.append(")");
- return sb.toString();
- }
- }
在Hive中执行,结果如下:
- hive> add jar /home/wangzhun/hive/hive-0.8.1/lib/helloGenericUDFNew.jar;
- Added /home/wangzhun/hive/hive-0.8.1/lib/helloGenericUDFNew.jar to class path
- Added resource: /home/wangzhun/hive/hive-0.8.1/lib/helloGenericUDFNew.jar
- hive> create temporary function hellonew as 'com.wz.udf.helloGenericUDFNew';
- OK
- Time taken: 0.016 seconds
- hive> select hellonew(tb_test2.name,tb_test2.score_list) from tb_test2;
- Total MapReduce jobs = 1
- Launching Job 1 out of 1
- Number of reduce tasks is set to 0 since there's no reduce operator
- Starting Job = job_201312091733_0018, Tracking URL = http://localhost:50030/jobdetails.jsp?jobid=job_201312091733_0018
- Kill Command = /home/wangzhun/hadoop/hadoop-0.20.2/bin/../bin/hadoop job -Dmapred.job.tracker=localhost:9001 -kill job_201312091733_0018
- Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
- 2013-12-09 22:31:22,328 Stage-1 map = 0%, reduce = 0%
- 2013-12-09 22:31:25,354 Stage-1 map = 100%, reduce = 0%
- 2013-12-09 22:31:28,390 Stage-1 map = 100%, reduce = 100%
- Ended Job = job_201312091733_0018
- MapReduce Jobs Launched:
- Job 0: Map: 1 HDFS Read: 99 HDFS Write: 18 SUCESS
- Total MapReduce CPU Time Spent: 0 msec
- OK
- {"people":"A","totalscore":275}
- {"people":"B","totalscore":275}
- {"people":"C","totalscore":270}
- Time taken: 21.7 seconds
Hive GenericUDF2的更多相关文章
- 初识Hadoop、Hive
2016.10.13 20:28 很久没有写随笔了,自打小宝出生后就没有写过新的文章.数次来到博客园,想开始新的学习历程,总是被各种琐事中断.一方面确实是最近的项目工作比较忙,各个集群频繁地上线加多版 ...
- Hive安装配置指北(含Hive Metastore详解)
个人主页: http://www.linbingdong.com 本文介绍Hive安装配置的整个过程,包括MySQL.Hive及Metastore的安装配置,并分析了Metastore三种配置方式的区 ...
- Hive on Spark安装配置详解(都是坑啊)
个人主页:http://www.linbingdong.com 简书地址:http://www.jianshu.com/p/a7f75b868568 简介 本文主要记录如何安装配置Hive on Sp ...
- HIVE教程
完整PDF下载:<HIVE简明教程> 前言 Hive是对于数据仓库进行管理和分析的工具.但是不要被“数据仓库”这个词所吓倒,数据仓库是很复杂的东西,但是如果你会SQL,就会发现Hive是那 ...
- 基于Ubuntu Hadoop的群集搭建Hive
Hive是Hadoop生态中的一个重要组成部分,主要用于数据仓库.前面的文章中我们已经搭建好了Hadoop的群集,下面我们在这个群集上再搭建Hive的群集. 1.安装MySQL 1.1安装MySQL ...
- hive
Hive Documentation https://cwiki.apache.org/confluence/display/Hive/Home 2016-12-22 14:52:41 ANTLR ...
- 深入浅出数据仓库中SQL性能优化之Hive篇
转自:http://www.csdn.net/article/2015-01-13/2823530 一个Hive查询生成多个Map Reduce Job,一个Map Reduce Job又有Map,R ...
- Hive读取外表数据时跳过文件行首和行尾
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 有时候用hive读取外表数据时,比如csv这种类型的,需要跳过行首或者行尾一些和数据无关的或者自 ...
- Hive索引功能测试
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 从Hive的官方wiki来看,Hive0.7以后增加了一个对表建立index的功能,想试下性能是 ...
随机推荐
- translclude
.transclude:true 启用transclude,启用以后,有两个地方会发生变化: ①.使用指令的元素内部的所有内容都会被保存起来.不妨先把这一段内容称为一坨. 比如指令元素是这样的: &l ...
- Centos7部署kubelet(六)
1.二进制包准备将软件包从linux-node1复制linux-node2.linux-node3中去 [root@linux-node1 ssl]# cd /usr/local/src/kubern ...
- Docker(4):Dockerfile命令一览
1.FROM 指定基础镜像 FROM 指令用于指定其后构建新镜像所使用的基础镜像.FROM 指令必是 Dockerfile 文件中的首条命令,启动构建流程后,Docker 将会基于该镜像构建新镜像,F ...
- Docker(3):Dockerfile介绍及简单示例
Dockerfile 概念 Dockerfile是由一系列命令和参数构成的脚本,这些命令应用于基础镜像并最终创建一个新的镜像.它们简化了从头到尾的流程并极大的简化了部署工作.Dockerfile从FR ...
- Effective Java Chapter4 Classes and Interface
MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...
- 究竟 javascript 错误处理有哪些类型?
有时候,在自己封装的工具函数中,不传参或传入了错误类型的参数,也要适当的抛出一些错误以示警告:使用框架不正常情况下也会抛出错误,如果对错误一无所知,便无从下手调试.综合上述,了解错误的处理机制是多么必 ...
- 《RECURRENT BATCH NORMALIZATION》
原文链接 https://arxiv.org/pdf/1603.09025.pdf Covariate 协变量:在实验的设计中,协变量是一个独立变量(解释变量),不为实验者所操纵,但仍影响实验结果. ...
- shell脚本-实战防dos攻击
根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟.防火墙命令为:iptables -I INPUT -s 10.0 ...
- js编写轮播图,广告弹框
1.轮播图 js编写轮播图,需要用到setInterval(计时器):先给一个div,里面放轮播图的图片,将轮播图的图片明明为相同样式的:如:banner1.jpg,banner2.jpg,banne ...
- 微软Power BI 每月功能更新系列——10月Power BI 新功能学习
Power BI Desktop10月产品功能摘要 本月Power Plus Desktop的更新充满了整个产品的小型和大型改进.一个巨大的更新是Power BI服务支持我们的复合模型和聚合预览.这实 ...