hive添加UDF

步骤如下:
  • 函数分为永久和临时函数,后者会话退出则消失,前者不会

  • 查看已有函数(创建好后也可以通过这个来查看是否成功

show functions;
  • 写UDF的java文件,如:
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.apache.hadoop.hive.ql.udf; import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.serde2.ByteStream;
import org.apache.hadoop.hive.serde2.io.ByteWritable;
import org.apache.hadoop.hive.serde2.io.DoubleWritable;
import org.apache.hadoop.hive.serde2.io.ShortWritable;
import org.apache.hadoop.hive.serde2.io.TimestampWritable;
import org.apache.hadoop.hive.serde2.lazy.LazyInteger;
import org.apache.hadoop.hive.serde2.lazy.LazyLong;
import org.apache.hadoop.io.BooleanWritable;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text; /**
* UDFToString.
*
*/
public class UDFToString extends UDF {
private final Text t = new Text();
private final ByteStream.Output out = new ByteStream.Output(); public UDFToString() {
} public Text evaluate(NullWritable i) {
return null;
} private final byte[] trueBytes = {'T', 'R', 'U', 'E'};
private final byte[] falseBytes = {'F', 'A', 'L', 'S', 'E'}; public Text evaluate(BooleanWritable i) {
if (i == null) {
return null;
} else {
t.clear();
t.set(i.get() ? trueBytes : falseBytes);
return t;
}
} public Text evaluate(ByteWritable i) {
if (i == null) {
return null;
} else {
out.reset();
LazyInteger.writeUTF8NoException(out, i.get());
t.set(out.getData(), 0, out.getCount());
return t;
}
} public Text evaluate(ShortWritable i) {
if (i == null) {
return null;
} else {
out.reset();
LazyInteger.writeUTF8NoException(out, i.get());
t.set(out.getData(), 0, out.getCount());
return t;
}
} public Text evaluate(IntWritable i) {
if (i == null) {
return null;
} else {
out.reset();
LazyInteger.writeUTF8NoException(out, i.get());
t.set(out.getData(), 0, out.getCount());
return t;
}
} public Text evaluate(LongWritable i) {
if (i == null) {
return null;
} else {
out.reset();
LazyLong.writeUTF8NoException(out, i.get());
t.set(out.getData(), 0, out.getCount());
return t;
}
} public Text evaluate(FloatWritable i) {
if (i == null) {
return null;
} else {
t.set(i.toString());
return t;
}
} public Text evaluate(DoubleWritable i) {
if (i == null) {
return null;
} else {
t.set(i.toString());
return t;
}
} public Text evaluate(Text i) {
if (i == null) {
return null;
}
i.set(i.toString());
return i;
} public Text evaluate(TimestampWritable i) {
if (i == null) {
return null;
} else {
t.set(i.toString());
return t;
}
} public Text evaluate (BytesWritable bw) {
if (null == bw) {
return null;
}
t.set(bw.getBytes(),0,bw.getLength());
return t;
}
}
  • 将写好的java文件打包成jar:
jar cvf UDFUpper.jar -c bin UDFUpper.java
  • 进入hive,添加jar文件
hive> add jar UDFToString.jar;
Added [UDFToString.jar] to class path
Added resources: [UDFToString.jar]
  • 添加临时函数(会话结束函数消失

    • 进入hive,添加jar文件

      hive> add jar UDFToString.jar;
      Added [UDFToString.jar] to class path
      Added resources: [UDFToString.jar]
    • 添加函数(注意class所在包)

      语法为:
      CREATE TEMPORARY FUNCTION function_name AS class_name; hive> create temporary function mytest as 'org.apache.hadoop.hive.ql.udf.UDFToString';
      OK
      Time taken: 0.009 seconds 路径出错会提示:
      FAILED: Class default.udf.Upper not found
      FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.FunctionTask 具体类路径为UDF java文件里的package+'.'+java文件名
    • 删除临时函数

      DROP TEMPORARY FUNCTION [IF EXISTS] function_name;
  • 添加永久函数

    • 添加

      语法:
      CREATE FUNCTION [db_name.]function_name AS class_name
      [USING JAR|FILE|ARCHIVE 'file_uri' [, JAR|FILE|ARCHIVE 'file_uri'] ]; 注意:
      如果hive非本地模式运行,则后面应该是为非本地文件等URI,如hdfs路径,否则会报错; 例子:
      hive> create function default.hah as "org.apache.hadoop.hive.ql.udf.UDFToString" using jar "UDFToString.jar";
      FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.FunctionTask. Hive warehouse is non-local, but UDFToString.jar specifies file on local filesystem. Resources on non-local warehouse should specify a non-local scheme/path hive> create function default.hah as "org.apache.hadoop.hive.ql.udf.UDFToString" using jar "hdfs:///hdfs_home/UDFToString.jar";
      converting to local hdfs:///hdfs_home/UDFToString.jar
      Added [/tmp/fda83e6d-e1af-4005-affa-9f9c4ee226a6_resources/UDFToString.jar] to class path
      Added resources: [hdfs:///hdfs_home/UDFToString.jar]
      OK
      Time taken: 0.521 seconds
    • 删除

      DROP FUNCTION [IF EXISTS] function_name;
引用第三方包的情况

假如在你的UDF文件里引用了第三方包,那么只需要在生成jar文件的时候改变一下命令就可以了,如下:

javac -classpath hive-0.4.1.jar:commons-io-2.5.jar:bcprov-jdk15on-158.jar  com/example/hive/udf/UDFDecrypt.java

jar -cvf UDFDecrypt.jar ./com/example/hive/udf/UDFDecrypt.class

上面javac命令中classpath跟的是用到的第三方包名,使用:做间隔,后面跟的是java文件路径

此处参考了这里

参考

hive添加UDF的更多相关文章

  1. hive 添加UDF(user define function) hive的insert语句

    add JAR /home/hadoop/study/study2/utf.jar; package my.bigdata.udf; import org.apache.hadoop.hive.ql. ...

  2. Hive 10、Hive的UDF、UDAF、UDTF

    Hive自定义函数包括三种UDF.UDAF.UDTF UDF(User-Defined-Function) 一进一出 UDAF(User- Defined Aggregation Funcation) ...

  3. hive中UDF、UDAF和UDTF使用

    Hive进行UDF开发十分简单,此处所说UDF为Temporary的function,所以需要hive版本在0.4.0以上才可以. 一.背景:Hive是基于Hadoop中的MapReduce,提供HQ ...

  4. hive下UDF函数的使用

    1.编写函数 [java] view plaincopyprint?package com.example.hive.udf;    import org.apache.hadoop.hive.ql. ...

  5. 在hive中UDF和UDAF使用说明

    Hive进行UDF开发十分简单,此处所说UDF为Temporary的function,所以需要hive版本在0.4.0以上才可以. 一.背景:Hive是基于Hadoop中的MapReduce,提供HQ ...

  6. 【转】hive中UDF、UDAF和UDTF使用

    原博文出自于: http://blog.csdn.net/liuj2511981/article/details/8523084 感谢! Hive进行UDF开发十分简单,此处所说UDF为Tempora ...

  7. hive的UDF读取配置文件

    hive的UDF读取配置文件 实现步骤 在读取配置文件的写为./file_name,然后在添加UDF的时候把配置文件也加入资源就好了: add jar xxx.jar; add file file_n ...

  8. 【Hive五】Hive函数UDF

    Hive函数 系统自带的函数 查看系统自带的函数 查看系统自带的函数 show functions; 显示自带的函数的用法 desc function upper; 详细显示自带的函数的用法 desc ...

  9. hive premanent udf 发布...

    起因: hive premanent udf 发布成功,但是hue 无法加载使用(但是cli 是可用的) ,处理半天,依然不可用!后来发现重启hiveserver2 就可以了     具体步骤如下:  ...

随机推荐

  1. lua闭包

    function MakeCounter() return function() t = t + return t end end local func = MakeCounter() , do pr ...

  2. node下使用jquery

    node使用jquery的两种方式 在node下,使用jquery有两种方法: 使用jsdom模拟一个window对象 使用cheerio,cheerio只实现了jquery的dom部分功能,相当于j ...

  3. ios实例开发精品文章推荐(8.12)11个处理触摸事件和多点触摸的JS库

    11个处理触摸事件和多点触摸的JS库 触摸屏是现在所有智能手机的标配,还包括各种平板设备,而且很多桌面也慢慢在开始支持触摸操作.要开发支持触摸屏设备的Web应用,我们需要借助浏览器的触摸事件来实现. ...

  4. #探究# HTTP长连接和短连接

    本文速读: HTTP协议与TCP/IP协议的关系 因TCP协议可靠,所以HTTP通常基于TCP实现 如何理解HTTP协议是无状态的 多次请求之间没有关联关系 什么是长连接.短连接? 每次请求都建立TC ...

  5. linux下的工作模型以及Nginx工作原理

      Web服务器主要任务就是处理来自客户端的请求,一般情况下Web服务器处理并发连接请求的工作模型有以下几种方式: 1.单线程web服务器(Single-threaded web servers) 此 ...

  6. 【java】详解集合

    目录结构: contents structure [-] 集合概述 什么是集合 Collection和Map的区别 List和Set的区别 ArrayList和LinkedList的区别 HashSe ...

  7. C++中的Thunk技术 / 非静态类成员函数作为回调函数 的实现方法

    原文:https://blog.twofei.com/616/ 用我的理解通俗地解释一下什么是C++中的Thunk技术吧! Thunk技术就是申请一段可执行的内存, 并通过手动构造CPU指令的形式来生 ...

  8. Error Code: 1030. Got error -1 from storage engine

    这个问题通常是数据库可以建表,旧表可以插入数据,正常:可是新表无法插入数据,无法改名等操作: 先从文件权限找方法,没法解决: 在网上搜了一通,大家都说的磁盘满了,但是我们的磁盘还空着呢! 后来,发现! ...

  9. MongoDB学习笔记(5)--document

    MongoDB 插入文档 本章节中我们将向大家介绍如何将数据插入到MongoDB的集合中. 文档的数据结构和JSON基本一样. 所有存储在集合中的数据都是BSON格式. BSON是一种类json的一种 ...

  10. js获取日期:昨天今天和明天、后天

    <html> <head> <meta http-equiv="Content-Type" content="textml; charset ...