4 HQL
4.1 官网
4.1.1 https://cwiki.apache.org/confluence/display/Hive/LanguageManual
4.1.2 性能调优
4.1.2.1 Explain Execution Plan
4.1.2.2 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Explain
4.2 示例
4.2.1 增加分区
4.2.1.1 alter table tab_ip change id id_alter string; ALTER TABLE tab_cts ADD PARTITION (partCol = 'dt') location '/external/hive/dt';
4.2.2 结果写到HDFS
4.2.2.1 insert overwrite local directory './hivetest/' select * from tbl_order_pt where month='201602'; insert overwrite directory '/hivetest/' select * from tbl_order_pt where month='201602';
4.2.3 集合类型字段
4.2.3.1 ARRAY
4.2.3.1.1 create table tab_array(a array<int>,b array<string>) row format delimited fields terminated by '\t' collection items terminated by ','; //示例数据 tobenbrone,laihama,woshishui 13866987898,13287654321 abc,iloveyou,itcast 13866987898,13287654321 //操作 select a[0] from tab_array; select * from tab_array where array_contains(b,'word'); insert into table tab_array select array(0),array(name,ip) from tab_ext t;
4.2.3.2 MAP
4.2.3.2.1 create table tab_map(name string,info map<string,string>) row format delimited fields terminated by '\t' collection items terminated by ';' map keys terminated by ':'; //示例数据: fengjie age:18;size:36A;addr:usa furong age:28;size:39C;addr:beijing;weight:180KG //操作 load data local inpath '/home/hadoop/hivetemp/tab_map.txt' overwrite into table tab_map; insert into table tab_map select name,map('name',name,'ip',ip) from tab_ext;
4.2.3.3 STRUCT
4.2.3.3.1 create table tab_struct(name string,info struct<age:int,tel:string,addr:string>) row format delimited fields terminated by '\t' collection items terminated by ',' //操作 load data local inpath '/home/hadoop/hivetemp/tab_st.txt' overwrite into table tab_struct; insert into table tab_struct select name,named_struct('age',id,'tel',name,'addr',country) from tab_ext;
4.2.4 自定义函数
4.2.4.1 select if(id=1,first,no-first),name from tab_ext; hive>add jar /home/hadoop/myudf.jar; hive>CREATE TEMPORARY FUNCTION my_lower AS 'org.dht.Lower'; select my_upper(name) from tab_ext;
4.2.5 高级查询
4.2.5.1 select * from tbl_order_pt where month='201602' sort by id desc limit 0,3; select a.ip,b.book from tab_ext a join tab_ip_book b on(a.name=b.name);
4.2.6 HQL CLI
4.2.6.1 hive -S -e 'select country,count(*) from tab_ext' > /home/hadoop/hivetemp/e.txt 有了这种执行机制,就使得我们可以利用脚本语言(bash shell,python)进行hql语句的批量执行,示例如下: hive -S -e 'use db_order;select * from tbl_order_pt where month='201602' sort by id desc limit 0,3;' > result.txt
 

自定义函数示例代码:

package cn.itcast.bigdata;

import java.util.HashMap;

import org.apache.hadoop.hive.ql.exec.UDF;

public class PhoneNbrToArea extends UDF{

    private static HashMap<String, String> areaMap = new HashMap<>();
static {
areaMap.put("", "beijing");
areaMap.put("", "tianjin");
areaMap.put("", "nanjing");
} //一定要用public修饰才能被hive调用
public String evaluate(String pnb) { String result = areaMap.get(pnb.substring(,))==null? (pnb+" huoxing"):(pnb+" "+areaMap.get(pnb.substring(,))); return result;
} }

【Hadoop】Hive HSQ 使用 && 自定义HQL函数的更多相关文章

  1. Hadoop Hive概念学习系列之hive里的用户定义函数UDF(十七)

    Hive可以通过实现用户定义函数(User-Defined Functions,UDF)进行扩展(事实上,大多数Hive功能都是通过扩展UDF实现的).想要开发UDF程序,需要继承org.apache ...

  2. hive自定义UDTF函数叉分函数

    hive自定义UDTF函数叉分函数 1.介绍 从聚合体日志中需要拆解出来各子日志数据,然后单独插入到各日志子表中.通过表生成函数完成这一过程. 2.定义ForkLogUDTF 2.1 HiveUtil ...

  3. Hive(九)【自定义函数】

    目录 自定义函数 编程步骤 案例 需求 1.创建工程 2.导入依赖 3.创建类 4.打jar包 5.上传hive所在服务器 6.将jar添加到hive的classpath 7.创建临时函数与开发好的j ...

  4. hive自定义udaf函数

    自定义udaf函数的代码框架 //首先继承一个类AbstractGenericUDAFResolver,然后实现里面的getevaluate方法 public GenericUDAFEvaluator ...

  5. Hadoop Hive基础sql语法

     目录 Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构 化的数据文件映射为一张数据库表,并提供完整的 ...

  6. Hadoop Hive sql语法详解

    Hadoop Hive sql语法详解 Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构 化的数据文件 ...

  7. [转]Hadoop Hive sql语法详解

    转自 : http://blog.csdn.net/hguisu/article/details/7256833 Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式 ...

  8. Hadoop Hive sql 语法详解

    Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构化的数据文件映射为一张数据库表,并提供完整的SQL查询 ...

  9. 自定义UDF函数应用异常

    自定义UDF函数应用异常 版权声明:本文为yunshuxueyuan原创文章.如需转载请标明出处: http://www.cnblogs.com/sxt-zkys/QQ技术交流群:299142667 ...

随机推荐

  1. git 使用教程整理

    饥人谷最优技术博客,Git使用三部曲系列--朱维(直播10班)<创建版本库>http://t.cn/RfRbSY8<查看状态>http://t.cn/Rfn2TkP<版本 ...

  2. Action+Service +Dao三层的功能划分

    来源:http://blog.csdn.net/inter_peng/article/details/41021727 1. Action/Service/DAO简介: Action是管理业务(Ser ...

  3. Qt学习思考

    对各个部件基本了解,初步理解GUI应用程序的创建 2D图形文字绘制,3D图形(openGL)等 模型/视图框架编程,处理复杂的数据 多媒体框架 数据库,xml,文件读写等 网络编程 做出比较美观的界面 ...

  4. xcode7 __weak 导致报错 is unavailable

    Build Settings Apple LLVM7.1 Laguage-Object-c Weak References in Manual Retain Release 选 Yes;

  5. jquery 插件之 点赞“+1” 特效

    一般用户点个赞后,都会有个 +1 的特效飘过,用户已经点过赞了,会有“已点过赞”的特效提示 在这里,我们写了一个点赞的插件 //扩展对象点赞插件.点赞特效 //用法:jQuery('.praisebt ...

  6. Java基础-String、StringBuffer、StringBuilder

    看下面这段代码: public class Main { public static void main(String[] args) { String string = ""; ...

  7. c#创建ISS站点

    private void CreateWebSite() { try { string installPath = "C:\\Program Files\\MyWeb"; stri ...

  8. Oracle创建DBLink的方法

    文章从http://blog.csdn.net/davidhsing/article/details/6408770拷贝过来的 1.如果需要创建全局 DBLink,则需要先确定用户有创建 dblink ...

  9. jauery加入项目中,但是在页面中显示没有找到这个文件--springMVC框架

    遇到一件很不爽的事情,自己明明已经把jquery的文件放在了项目中,但是在页面中总是看不到效果,开发者模式提示没有找到文件,当时都要郁闷疯了,后来无意间看到了Eclipse中报的错,怎么与Spring ...

  10. 【bzoj1042】 HAOI2008—硬币购物

    http://www.lydsy.com/JudgeOnline/problem.php?id=1042 (题目链接) 题意 共有4种硬币,面值分别为c1,c2,c3,c4.某人去商店买东西,去了to ...