大家好,又见面了,我是你们的朋友全栈君。
简介
Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行。 其优点是学习成本低,可以通过类SQL语句快速实现简单的MapReduce统计。以下介绍常用的Hive的类SQL语句。
创建表:
hive>create table tablename(id int,name string,password string);
创建表时指定分隔符
hive> create table tablename(name string,password string)row format delimited fields terminated by ‘,’; (指定源数据的分隔符为”逗号”)
加载表
hive> load data inpath ‘/user/hadoop/output7/part-r-00000’ into table tablename;
创建一个新表,结构与某表一样
hive> create table table02 like table01;
创建分区表
hive> create table tablename(id int,line string) partitioned by (dt string,country string);
显示表里有多少条记录(count 数大于50的有多少条记录)
hive>select count(*) from tablename where count>50;
排序用法order by (查询count 数大于50并排序)
select * from tablename where count > 50 order by count;
显示表中有多少分区
hive> show partitions tablename;
显示所有表
hive> show tables;
显示所有与t开头的表
hive> show tables ‘t*’;
显示表的结构信息
hive> describe tablename;
修改表名字
hive> alter table table01 rename to table02;
在原表上新添加一列
hive> alter table tablename add columns(new_col2 int comment ‘a commment’);
hive> alter table tablename add columns(new_col3 int);
删除表
hive> drop table tablename;
从本地文件加载数据:
hive> LOAD DATA LOCAL INPATH ‘/home/hadoop/input/sample.txt’ OVERWRITE INTO TABLE records;
加载分区表
hive> load data inpath ‘/user/hive/warehouse/part-r-00000’ overwrite into table clickstream_log PARTITION(dt = ‘2018-11-30’);
显示所有函数
hive> show functions;
查看函数的用法
hive> describe function substr;
查看数组、map、结构
hive> select col1[0],col2[‘b’],col3.c from complex;
查看数组、map、结构
hive> select col1[0],col2[‘b’],col3.c from complex;
内连接:
hive> SELECT sales., things. FROM sales JOIN things ON (sales.id = things.id);
查看hive为某个查询使用多少个MapReduce作业
hive> Explain SELECT sales., things. FROM sales JOIN things ON (sales.id = things.id);
外连接:
hive> SELECT sales., things. FROM sales LEFT OUTER JOIN things ON (sales.id = things.id); hive> SELECT sales., things. FROM sales RIGHT OUTER JOIN things ON (sales.id = things.id); hive> SELECT sales., things. FROM sales FULL OUTER JOIN things ON (sales.id = things.id);
in查询:Hive不支持,但可以使用LEFT SEMI JOIN
hive> SELECT * FROM things LEFT SEMI JOIN sales ON (sales.id = things.id);
Map连接:Hive可以把较小的表放入每个Mapper的内存来执行连接操作
hive> SELECT / MAPJOIN(things) / sales., things. FROM sales JOIN things ON (sales.id = things.id);
INSERT OVERWRITE TABLE …SELECT:新表预先存在
hive> FROM records2 > INSERT OVERWRITE TABLE stations_by_year SELECT year, COUNT(DISTINCT station) GROUP BY year > INSERT OVERWRITE TABLE records_by_year SELECT year, COUNT(1) GROUP BY year > INSERT OVERWRITE TABLE good_records_by_year SELECT year, COUNT(1) WHERE temperature != 9999 AND (quality = 0 OR quality = 1 OR quality = 4 OR quality = 5 OR quality = 9) GROUP BY year;
CREATE TABLE … AS SELECT:新表表预先不存在
hive>CREATE TABLE target AS SELECT col1,col2 FROM source;
创建视图:
hive> CREATE VIEW valid_records AS SELECT * FROM records2 WHERE temperature !=9999;
查看视图详细信息:
hive> DESCRIBE EXTENDED valid_records;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/190693.html原文链接:https://javaforall.cn