上篇:

Hadoop3集群搭建之——虚拟机安装

Hadoop3集群搭建之——安装hadoop,配置环境

Hadoop3集群搭建之——配置ntp服务

Hadoop3集群搭建之——hive安装

Hadoop3集群搭建之——hbase安装及简单操作

Hadoop3集群搭建之——hive添加自定义函数UDF

Hadoop3集群搭建之——hive添加自定义函数UDTF

上篇中,udtf函数,只有为一行输入,一行输出。udtf是可以一行输入,多行输出的。

简述下需求:  

输入开始时间,结束时间,返回每个小时的时长

直接上代码:

package com.venn.udtf;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; import java.util.ArrayList; /**
* Created by venn on 5/20/2018.
* SplitHour : split hour
*/
public class SplitHour extends GenericUDTF { /**
* add the column name
* @param args
* @return
* @throws UDFArgumentException
*/
@Override
public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException {
if (args.length != 1) {
throw new UDFArgumentLengthException("ExplodeMap takes only one argument");
}
if (args[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
throw new UDFArgumentException("ExplodeMap takes string as a parameter");
} ArrayList<String> fieldNames = new ArrayList<String>();
ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
fieldNames.add("begintime");
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
fieldNames.add("endtime");
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
fieldNames.add("hour");
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
fieldNames.add("seconds");
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector); return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
} /**
* process the column
* @param objects
* @throws HiveException
*/
public void process(Object[] objects) throws HiveException { String [] input = objects[0].toString().split(",");
// 2018-06-06 10:25:35
String beginTime = input[0];
String endTime = input[1]; String[] result = new String[4];
result[0] = beginTime;
result[1] = endTime; // begintime
int bhour = Integer.parseInt(beginTime.substring(11, 13));
int bmin = Integer.parseInt(beginTime.substring(14, 16));
int bsecond = Integer.parseInt(beginTime.substring(17, 19));
// endtime
int ehour = Integer.parseInt(endTime.substring(11, 13));
int emin = Integer.parseInt(endTime.substring(14, 16));
int esecond = Integer.parseInt(endTime.substring(17, 19)); // 1.if begin hour equal end hour, second is : (emin - bmin) * 60 + (esecond - bsecond)
if (bhour == ehour) {
result[2] = String.valueOf(bhour);
result[3] = String.valueOf((emin - bmin) * 60 + (esecond - bsecond));
forward(result);
return;
} boolean flag = true;
//TODO 待优化,先输出第一个循环的时长,再循环后面的就不用判断
while (bhour != ehour) {
result[2] = String.valueOf(bhour); if(flag){
flag = false;
// 2. if begintime hour != endtime, the first hour, second is : 3600 - bmin * 60 - bsecond
result[3] = String.valueOf(3600 - bmin * 60 - bsecond);
}else {
// 3. next hour is 3600
result[3] = String.valueOf();
}
bhour += 1;
// 输出到hive
forward(result);
} result[2] = String.valueOf(bhour);
// 4. the end hour is : emin * 60 + esecond
result[3] = String.valueOf( emin * 60 + esecond);
forward(result); } public void close() throws HiveException { } }

udtf 函数介绍参加上篇

使用方式见上篇

Hadoop3集群搭建之——hive添加自定义函数UDTF

样例:

hive> select split_hour( concat(begintime,',',endtime)) from viewlog where log_date= limit ;
OK
begintime endtime hour seconds
-- :: -- ::
-- :: -- ::
-- :: -- ::
-- :: -- ::
-- :: -- ::
-- :: -- ::
-- :: -- ::
-- :: -- ::
-- :: -- ::
2018-04-01 12:15:07 2018-04-01 12:15:11 12 4
2018-04-01 06:53:40 2018-04-01 07:02:09 6 380
2018-04-01 06:53:40 2018-04-01 07:02:09 7 129

Time taken: 2.238 seconds, Fetched: row(s)

搞定

Hadoop3集群搭建之——hive添加自定义函数UDTF (一行输入,多行输出)的更多相关文章

  1. Hadoop3集群搭建之——hive添加自定义函数UDTF

    上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...

  2. Hadoop3集群搭建之——hive添加自定义函数UDF

    上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...

  3. Hadoop3集群搭建之——hive安装

    Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hbase安装及简单操作 现在到 ...

  4. Hadoop3集群搭建之——hbase安装及简单操作

    折腾了这么久,hbase终于装好了 ------------------------- 上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hado ...

  5. Hadoop3集群搭建之——配置ntp服务

    上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 下篇: Hadoop3集群搭建之——hive安装 Hadoop3集群搭建之——hbase安装及简 ...

  6. Hadoop3集群搭建之——安装hadoop,配置环境

    接上篇:Hadoop3集群搭建之——虚拟机安装 下篇:Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoop3集群搭建之——hbase安装及简单操作 上篇已 ...

  7. Hadoop3集群搭建之——虚拟机安装

    现在做的项目是个大数据报表系统,刚开始的时候,负责做Java方面的接口(项目前端为独立的Java web 系统,后端也是Java web的系统,前后端系统通过接口传输数据),后来领导觉得大家需要多元化 ...

  8. 集群搭建之Hive配置要点

    注意点: 在启动Hive 的时候要先启动Hadoop和MySQL服务. Mysql 和 Hive 搭建在 yan00机器上. part1:MySQL配置相关 安装和配置相关命令: Yum instal ...

  9. Week08_day01 (Hive 自定义函数 UDF 一个输入,一个输出(最常用))

    当我们进入企业就会发现,很多时候,企业的数据都是加密的,我们拿到的数据没办法使用Hive自带的函数去解决,我们就需要自己去定义函数去查看,哈哈,然而企业一般不会将解密的代码给你的,只需要会用,但是我们 ...

随机推荐

  1. NumPy 字节交换

    NumPy 字节交换 在几乎所有的机器上,多字节对象都被存储为连续的字节序列.字节顺序,是跨越多字节的程序对象的存储规则. 大端模式:指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地 ...

  2. jasper打印实例2 ----通过文件字节流获得PDF格式图片

    public class IspReportUtil { public static String exportReportToByte(CommonReportHandler handler)thr ...

  3. Centos7安装Wkhtmltopdf -- nodejs将html转pdf

    安装wkhtmltopdf wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.1 ...

  4. 【nlp】中文分词基础原则及正向最大匹配法、逆向最大匹配法、双向最大匹配法的分析

    分词算法设计中的几个基本原则: 1.颗粒度越大越好:用于进行语义分析的文本分词,要求分词结果的颗粒度越大,即单词的字数越多,所能表示的含义越确切,如:“公安局长”可以分为“公安 局长”.“公安局 长” ...

  5. java实现rabbitMQ延时队列详解以及spring-rabbit整合教程

    在实际的业务中我们会遇见生产者产生的消息,不立即消费,而是延时一段时间在消费.RabbitMQ本身没有直接支持延迟队列功能,但是我们可以根据其特性Per-Queue Message TTL和 Dead ...

  6. TableView下拉刷新崩溃解决办法

    return cell;上边加判断 if(self.dataArray.count<1){ return cell; }

  7. how2j网站前端项目——天猫前端(第一次)学习笔记4

    开始产品页面的学习.项目里面有900多种商品,但是每种商品的布局是一致的:1.产品图片 2.基本信息 3.产品详情 4.累计评价 5.交互.从第一个产品图片开始吧! 一.产品图片 产品图片用到了分类页 ...

  8. 线特征---LineMatching原理(四)

    参考文章:An efficient and robust line segment matching approach based on LBD descriptor and pairwise geo ...

  9. django分页的东西, 不详细, 但是也足够了。

    视图函数中的代码 from django.shortcuts import render, HttpResponse, redirect import json from django.core.pa ...

  10. jquery插件之选项卡

    jQuery插件编写 首先来一个简拓展jQuery对象的方法 <body > <p>23</p> <script src="js/jquery-1. ...