Hadoop3集群搭建之——hive添加自定义函数UDTF
上篇:
其他配置请参照上篇:Hadoop3集群搭建之——hive添加自定义函数UDF
简述下需求:
系统userid格式如下:
前三位代表国家
接下来三位代表省
再接下来三位代表市
剩下的所以代表 商店
(瞎掰的需求,大意就是要切割字符串)
直接上代码:
/**
* Created by venn on 5/20/2018.
* SplitString : split string
* first 3 string : country
* next 3 string : province
* next 3 string : city
* next all : story
*/
public class SplitString extends GenericUDTF { /**
* add the column name,添加列名,类型。使用的hive-exec 1.2.1,想用2.3.3的,但是不会初始化列名部分
* @param args
* @return
* @throws UDFArgumentException
*/
@Override
public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException {
if (args.length != ) {
throw new UDFArgumentLengthException("ExplodeMap takes only one argument");
}
if (args[].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("userid"); // 第一列将输入字符串原样输出,方便查看
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
fieldNames.add("country"); // 第二列为国家
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
fieldNames.add("province"); //第三列为省
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
fieldNames.add("city"); // 第四列为市
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
fieldNames.add("story"); // 第五列商店
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
// 返回
return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
} /**
* process the column
* @param objects
* @throws HiveException
*/
public void process(Object[] objects) throws HiveException { String[] result = new String[];
try {
/*System.out.println(objects[0].toString());
System.out.println(objects[0] != null);
System.out.println(StringUtils.isEmpty(objects[0].toString()));
System.out.println(objects[0].toString().length() < 10);*/
// 如果数据不满足要求,返回 0 0 0 0 0
if (objects[] == null || StringUtils.isEmpty(objects[].toString()) || objects[].toString().length() < ) { result[] = "";
result[] = "";
result[] = "";
result[] = "";
result[] = "";
} else {
result[] = objects[].toString();
result[] = objects[].toString().substring(, );
result[] = objects[].toString().substring(, );
result[] = objects[].toString().substring(, );
result[] = objects[].toString().substring(); }
// 将数据返回
forward(result); } catch (Exception e) { } } public void close() throws HiveException { }
}
hive UDTF函数编有三个部分:
initialize : 初始化列名
process : 处理字符串部分
forward : 返回结果 使用方式请见上篇:Hadoop3集群搭建之——hive添加自定义函数UDF打包、上传服务器,修改 $HIVE_HOME/bin/.hiverc
添加如下内容: jar包可以添加多个
[hadoop@venn05 bin]$ more .hiverc
add jar /opt/hadoop/idp_hd/viewstat/lib/hivefunction-1.0-SNAPSHOT.jar;
create temporary function split_area as 'com.venn.udtf.SplitString';
使用结果如下:
hive> select split_area(userid) from sqoop_test limit ;
OK
Hadoop3集群搭建之——hive添加自定义函数UDTF的更多相关文章
- Hadoop3集群搭建之——hive添加自定义函数UDTF (一行输入,多行输出)
上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...
- Hadoop3集群搭建之——hive添加自定义函数UDF
上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...
- Hadoop3集群搭建之——hive安装
Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hbase安装及简单操作 现在到 ...
- Hadoop3集群搭建之——hbase安装及简单操作
折腾了这么久,hbase终于装好了 ------------------------- 上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hado ...
- Hadoop3集群搭建之——配置ntp服务
上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 下篇: Hadoop3集群搭建之——hive安装 Hadoop3集群搭建之——hbase安装及简 ...
- Hadoop3集群搭建之——安装hadoop,配置环境
接上篇:Hadoop3集群搭建之——虚拟机安装 下篇:Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoop3集群搭建之——hbase安装及简单操作 上篇已 ...
- Hadoop3集群搭建之——虚拟机安装
现在做的项目是个大数据报表系统,刚开始的时候,负责做Java方面的接口(项目前端为独立的Java web 系统,后端也是Java web的系统,前后端系统通过接口传输数据),后来领导觉得大家需要多元化 ...
- 集群搭建之Hive配置要点
注意点: 在启动Hive 的时候要先启动Hadoop和MySQL服务. Mysql 和 Hive 搭建在 yan00机器上. part1:MySQL配置相关 安装和配置相关命令: Yum instal ...
- Hive2.1.1集群搭建
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...
随机推荐
- Appium+python自动化4-等待函数
4.1 等待函数癈使用 4.1.1 为什么要使用等待函数 我们在做自动化的时候很多时候都不是很顺利,不是因为app的问题,我们的脚本也没问题,但是很多时候都会报错,比如一个页面本来就有id为1的这个元 ...
- Unity3D无限奔跑者Playmaker v1.5
这款功能丰富的移动准备包内含使您自己的各种各样的3D游戏运行的一切要素.无需编程! 功能 1 - 响应式滑动控制2 - 投掷炮弹3 - 可破坏物体4 - 能力提升道具- 金币磁铁- 护盾- 继续密钥- ...
- javascript简单的滑动效果
利用setInterval实现简单的滑动效果 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- WISH开发API
https://merchant.wish.com/documentation/api#api http://wishquan.com/
- 上海大都会赛 I Matrix Game(最大流)
At the start of the matrix game, we have an N x M matrix. Each grid has some balls.The grid in (i,j) ...
- Java_6 方法
1方法的定义和使用的注意事项 方法:一些功能整合成一个功能模块 注意事项: a: 方法不能定义在另一个方法的里面 b: 写错方法名字 c: 写错了参数列表 d: 方法返回值是void,方法中可 ...
- Tomcat登陆mysql的密码设置
在登陆mysql的密码和数据库密码不一致时,可以修改Mysql数据库密码或者修改连接Mysql的配置文件: 1.修改连接Tomcat里连接Mysql的配置文件 需要修改两个配置文件 ,一个是在tom ...
- YII2中验证码的使用
验证码的使用是比较频繁的.YII2中已经帮我们做好了封装. 首先我们在控制器里创建一个actions方法,用于使用yii\captcha\CaptchaAction <?php namespac ...
- Aspose.Words二 基础
1.目录样式 doc.Styles[Aspose.Words.StyleIdentifier.Toc1].Font.Size = 14; doc.Styles[Aspose.Words.StyleId ...
- TLS1.1升级到TLS1.2(微信小程序要求TLS1.2以上)
检测是否具有TLS1.2:www.ssllabs.com 实验过的办法: https://www.cnblogs.com/wqcheng/p/6618070.html http://www.ocbc. ...