上篇:

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. linux下访问window的共享文件,在命令行实现方法

    1.挂载共享目录 mount -t cifs //192.168.0.1/aa  /tmp/export -o username=text,password=test //192.168.0.1/aa ...

  2. NumPy 矩阵库(Matrix)

    NumPy 矩阵库(Matrix) NumPy 中包含了一个矩阵库 numpy.matlib,该模块中的函数返回的是一个矩阵,而不是 ndarray 对象. 一个 的矩阵是一个由行(row)列(col ...

  3. SVN集成compare4比较软件

    打开TortoiseSVN的Setting,选择左边的Diff Viewer 设置如下: "D:\Program Files\Beyond Compare 4\BComp.exe" ...

  4. struct和union,enum分析

    空结构体占用的内存多大? struct d { }; int main() { struct d d1; struct d d2; printf("%d,%0x\n",sizeof ...

  5. 【Linux 进程】exec族函数详解

    exec族的组成: 在Linux中,并不存在一个exec()的函数形式,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **e ...

  6. tomcat启动闪退之内存不足及显著优化

    增大内存: 打开catalina.bat,@echo off回车输入  set JAVA_OPTS=-server -Xms256m -Xmx512m -XX:PermSize=128M -XX:Ma ...

  7. python中logging模块使用

    1.logging模块使用场景 在写程序的时候,尤其是大型的程序,在程序中加入日志系统是必不可少的,它能记录很多的信息.刚刚接触python的时候肯定都在用print来输出信息,这样是最简单的输出,正 ...

  8. 项目打包 TestFlight用法

    TestFlight用法 包教包会(iOS APP官方测试工具) https://www.jianshu.com/p/4be185e4069c

  9. swift - label 的font 设置 文字字体和大小

    设置字体和颜色 lab.textColor = UIColor.init(hexColor: "795928") lab.font = UIFont.systemFont(ofSi ...

  10. dUMP:A new value is to be assigned to the field "<L_BOX>"

    DUMP: A new value is to be assigned to the field "<L_BOX>", although this field is e ...