常用HQL

进入hive客户端后:

1、建表:

create table page_view(viewTime int, userid bigint,page_url string, referrer_url string,ip string comment 'IP Address of the User')comment 'This is the page view table'partitioned by(dt string, country string)row format delimited fields terminated by '\t'  //指定字段间的分隔符stored as sequencefile; //textfile
//sequencefile 是以键值对组织的二进制文件
//hive的默认库是default,表也是一个文件夹,置于hdfs的/user/hive/warehouse下
//新建的库就是在hdfs的/user/hive/warehouse下新建一个库名文件夹


2、在hive客户端将本地文件上传至数据仓库

实际就是将文件上传至对应的hdfs中表文件夹下 

load data local inpath '~/data.txt' into table table_name;

//此操作也可通过hadoop fs -put ~/data.txt /user/hive/warehouse/库名/表名 来实现

load data inpath '/data.txt' into table table_name;//将hdfs上的文件移动至对应的hdfs表文件夹下



3、外表 external

表:文件找表

外表:表找文件

create external table tab_ip_ext(id int, name string, ip string, country string)
row format delimited fields terminated by ','
stored as testfile
location '/hdfs的文件路径;'


 
4、类似视图  CTAS  用于创建一些临时表存储中间结果,在hdfs中没有建立目录

create table tab_ip_ctas
as
select id new_id, name new_name, ip new_ip, country new_country
from tab_ip_ext
sort by new_id;


5、向表中写入查询数据

create table tab_ip_like like tab_ip; 
insert overwrite table tab_ip_like  //insert into为追加,overwrite为重写select * from tab_ip;


6、分区 PARTITION

分区就是在表的文件夹下再建一层文件夹,文件夹名称为"分区字段名=分区值",如"part_flag=part1"

create table tab_ip_part(id int,name string,ip string,country string) partitioned by (part_flag string)  //分区标志可以是表中字段,也可以是另一个自定义字段row format delimited fields terminated by ',';load data local inpath '/home/hadoop/ip.txt' overwrite into table tab_ip_part partition(part_flag='part1');
load data local inpath '/home/hadoop/ip_part2.txt' overwrite into table tab_ip_part partition(part_flag='part2');
select * from tab_ip_part;
select * from tab_ip_part  where part_flag='part2';  //查询时,将分区当做一个字段来使用;末尾会有part2
select count(*) from tab_ip_part  where part_flag='part2'; 


7、将查询结果写入文件

//写入本地文件
insert overwrite local directory '/tmp/test.txt' select * from tab_ip_part where part_flag='part1'; 
//写入hdfs   
insert overwrite directory '/hiveout.txt' select * from tab_ip_part where part_flag='part1';



8、数据类型为数组 array 

create table tab_array(a array<int>,b array<string>)
row format delimited
fields terminated by '\t'
collection items terminated by ',';
示例数据
one,two,three    1,2,3

d,e,f    4,5,6

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; 




9、数据类型为map
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 ':';
示例数据:
name key1:value1;key2:value2;key3:value3
insert into table tab_map select name,map('name',name,'ip',ip) from tab_ext; 


10、数据类型为struct

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 ','
insert into table tab_struct select name,named_struct('age',id,'tel',name,'addr',country) from tab_ext;


11、在shell环境下执行HQL
hive -S -e 'select country,count(*) from 库名.表名' > /tmp/query.txt

可用脚本(bash,python)进行hql批量查询

12、用户自定义函数 UDF
select udf(id=1,first,no-first),name from tab_ext;

写一个Java类,定义udf函数逻辑,此类继承UDF,重写evaluate方法,public修饰,参数与返回值按需指定;

打成jar包,告知hive此jar包的位置:hive>add jar /..../xxx.jar

在hive中创建函数udf,将udf和此Java类挂钩即可:

hive>CREATE TEMPORARY FUNCTION my_udf AS 'org.dht.Lower(类全名)'; //退出后,my_udf失去作用


13、sequencefile
create table tab_ip_seq(id int,name string,ip string,country string) row format delimitedfields terminated by ','stored as sequencefile;
insert overwrite table tab_ip_seq select * from tab_ext;


14、分区将不同的记录放在不同的文件夹中,分桶将不同的记录置于不同的文件 CLUSTER 

create table tab_ip_cluster(id int,name string,ip string,country string)
clustered by(id) into 3 buckets;load data local inpath '/.../ip.txt' overwrite into table tab_ip_cluster;
set hive.enforce.bucketing=true;
insert into table tab_ip_cluster select * from tab_ip;
select * from tab_ip_cluster tablesample(bucket 2 out of 3 on id); 
用于抽样,保证均匀抽样。





本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/387418.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

阿里云天池 金融风控训练营Task1 广东工业站

Task1 赛题理解 一、学习知识点概要 本次学习先是介绍了赛题的背景和概况&#xff0c;题目以金融风控中的个人信贷为背景&#xff0c;给所给的47列特征中&#xff0c;根据贷款申请人的数据信息预测其是否有违约的可能&#xff0c;以此判断是否通过贷款。随后介绍了比赛中的评…

如何将.crt的ssl证书文件转换成.pem格式

如何将.crt的ssl证书文件转换成.pem格式摘自&#xff1a;https://www.landui.com/help/show-8127 2018-07-04 14:55:41 2158次 准备:有一台安装了php的linux操作系统执行下面的openssl命令即可&#xff1a;openssl x509 -in www.xx.com.crt -out www.xx.com.pem转载于:https://…

SpringMVC学习记录--Validator验证分析

一.基于Validator接口的验证. 首先创建User实例,并加入几个属性 ?12345678910111213141516171819202122232425262728293031323334<code class"hljs cs">public class User {private String username;private String password;private String nickname;public …

NTP时间服务器实现Linux时间同步

在linux下&#xff0c;可以通过自带的NTP(Network Time Protocol)协议通过网络使自己的系统保持精确的时间。 什么是NTP&#xff1f; NTP是用来使系统和一个时间源保持时间同步的协议。 在自己管理的网络中建立至少一台时间服务器来同步本地时间&#xff0c;这样可以使得在不同…

阿里云天池 Python训练营Task1:从变量到异常处理

本学习笔记为阿里云天池龙珠计划Python训练营的学习内容&#xff0c;学习链接为&#xff1a;https://tianchi.aliyun.com/specials/promotion/aicamppython?spm5176.22758685.J_6770933040.1.6f103da1tESyzu 目录 一、学习知识点概要 二、学习内容 I.变量、运算符与数据类…

python回收机制

目录 Python的垃圾回收机制引子:一、什么是垃圾回收机制&#xff1f;二、为什么要用垃圾回收机制&#xff1f;三、垃圾回收机制原理分析1、什么是引用计数&#xff1f;2、引用计数扩展阅读&#xff1f;&#xff08;折叠&#xff09;Python的垃圾回收机制 引子: 我们定义变量会申…

安装openssl-devel命令

centos&#xff1a; yum install openssl-devel ubuntu&#xff1a; sudo apt-get install openssl sudo apt-get install libssl-dev

阿里云天池 Python训练营Task2: Python基础练习:数据结构大汇总 学习笔记

本学习笔记为阿里云天池龙珠计划Python训练营的学习内容&#xff0c;学习链接为&#xff1a;https://tianchi.aliyun.com/specials/promotion/aicamppython?spm5176.22758685.J_6770933040.1.6f103da1tESyzu 目录 一、学习知识点概要 二、学习内容 I.列表&#xff08;list…

windows文件与Linux文件互转

使用命令 unix2dos filename dos2unix filename

1G.小a的排列(C++)

小a的排列&#xff08;C&#xff09; 点击做题网站链接 题目描述 小a有一个长度为n的排列。定义一段区间是"萌"的&#xff0c;当且仅当把区间中各个数排序后相邻元素的差为1现在他想知道包含数x,y的长度最小的"萌"区间的左右端点 也就是说&#xff0c;我们…

阿里云天池 Python训练营Task3: Python基础进阶:从函数到高级魔法方法 学习笔记

本学习笔记为阿里云天池龙珠计划Python训练营的学习内容&#xff0c;学习链接为&#xff1a;https://tianchi.aliyun.com/specials/promotion/aicamppython?spm5176.22758685.J_6770933040.1.6f103da1tESyzu 目录 一、学习知识点概要 二、学习内容 I.函数 1.定义自己的函…

C# 获取句柄程序

这个小程序需要用到系统API&#xff0c;也就是需要用到user32中的三个函数。 第一个&#xff1a;WindowFromPoint 返回一个窗口句柄 第二个&#xff1a;GetWindowText 获取窗口标题 第三个&#xff1a;GetClassName 获取类名 当然&#xff0c;最重要的一点就是要引用命名空间…

HBase安装配置

HBase的安装配置&#xff1a; 4台主机&#xff1a;hdp0 hdp1 hdp2 hdp3 hdp0 hdp1 跑HMaster hdp2 hdp3 跑HRegionServer 将HBase解压之后 1、确保安装ZooKeeper&#xff1b; 2、修改hbase-env.sh export JAVA_HOME/.../jdk export HBASE_MANAGES_ZKfalse //使用外部的…

python cook读书笔记第2章字符串和文本

使用多个界定符分割字符串 line asdf fjdk; afed, fjek,asdf, fooimport re# line re.split(r[;,\s]\s*,line)# print(line)# [asdf, fjdk, afed, fjek, asdf, foo]"""当你使用 re.split() 函数时候&#xff0c;需要特别注意的是正则表达式中是否包含一个括号…

centos7安装oracle12c 一

本文 基本参考了下面这篇文章http://blog.csdn.net/gq5251/article/details/42004035 和http://www.linuxidc.com/Linux/2017-08/146528.htm 但是改正了一些错误操作系统:CentOS Linux release 7.2.1511 (Core) oracle: oarcle (12.1.0.2.0) - Standard Edition (SE2)几点要注…

Bigtable的些许重点

分布式数据库系统 针对于海量数据&#xff0c;可扩展&#xff0c;高吞吐量&#xff0c;低时延 不支持关系模型 通过row和column进行索引&#xff0c;row和column可以是任意字符串 所存储的数据也是字符串 Bigtable是一个map&#xff0c;value是array of bytes&#xff0c;通…

阿里云天池 Python训练营Task4: Python数据分析:从0完成一个数据分析实战 学习笔记

本学习笔记为阿里云天池龙珠计划Python训练营的学习内容&#xff0c;学习链接为&#xff1a;https://tianchi.aliyun.com/specials/promotion/aicamppython?spm5176.22758685.J_6770933040.1.6f103da1tESyzu 一、学习知识点概要 本次主要通过阿里云天池的赛题【Python入门系…

JMETER从JSON响应中提取数据

如果你在这里&#xff0c;可能是因为你需要使用JMeter从Json响应中提取变量。 好消息&#xff01;您正在掌握掌握JMeter Json Extractor的权威指南。作为Rest API测试指南的补充&#xff0c;您将学习掌握Json Path Expressions 所需的一切。 我们走吧&#xff01;并且不要惊慌&…

centos7安装oracle12c 二

环境&#xff1a;CentOS7VMware12&#xff0c;分配资源&#xff1a;CPU&#xff1a;2颗&#xff0c;内存&#xff1a;4GB&#xff0c;硬盘空间&#xff1a;30GB Oracle 12C企业版64位 下载地址&#xff1a;http://www.oracle.com/technetwork/database/enterprise-edition/down…

阿里云天池 Python训练营Task5:Python训练营测试 学习笔记

一、学习知识点概要 本次是Python训练营的测试&#xff0c;在45分钟内完成25题&#xff0c;满分100分及格80分。题目主要考察Task1到Task3里面的Python基础知识。在我随到的25道题里&#xff0c;知识点有&#xff1a; 变量&#xff08;包括数据类型和容器类型&#xff09;运算…