十六、Hive常用命令

版权声明:转载请注明:https://blog.csdn.net/u010285974 https://blog.csdn.net/u010285974/article/details/86306774

一、创建表

创建新表
hive> CREATE TABLE t_hive (a int, b int, c int) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

创建表并从其他表导入数据

hive> CREATE TABLE t_hive AS SELECT * FROM t_hive2 ;

仅复制表结构不导数据

hive> CREATE TABLE t_hive3 LIKE t_hive;

二、修改表

重命令表名
hive> ALTER TABLE t_hive RENAME TO t_hadoop;
增加一个字段
hive> ALTER TABLE t_hive ADD COLUMNS (new_col String);
修改字段

alter table 库名.表名 change 字段名 修改后的字段名 修改后的数据类型 COMMENT '中文说明';
alter table dim.dim_null_fy171012140322 change zd002 zd002 float COMMENT '手机号';

删表
drop table if exists t_hft;
删除表中数据,但要保持表的结构定义
hive> dfs -rmr /user/hive/warehouse/records;

三、导数据

导入数据t_hive.txt到t_hive表
hive> LOAD DATA LOCAL INPATH '/home/cos/demo/t_hive.txt' OVERWRITE INTO TABLE t_hive ;
从其他表导入数据
hive> INSERT OVERWRITE TABLE t_hive2 SELECT * FROM t_hive ;
从HDFS加载数据
hive> LOAD DATA INPATH '/user/hive/warehouse/t_hive/t_hive.txt' OVERWRITE INTO TABLE t_hive2;
通过Hive导出到本地文件系统
hive> INSERT OVERWRITE LOCAL DIRECTORY '/tmp/t_hive' SELECT * FROM t_hive;

四、查找表

展示所有表
hive> SHOW TABLES;

正则匹配表名
hive>show tables 't';

显示表的结构信息
hive> DESCRIBE invites;
显示表的各种信息
show create table dw.dw_com_rsa_phone;

Hive查询HiveQL
from ( select b,c as c2 from t_hive) t select t.b, t.c2 limit 2;
select b,c from t_hive limit 2;

五、分区

创建分区表
DROP TABLE IF EXISTS logs;
hive> create table logs(ts bigint,line string) partitioned by (dt String,country String);
导入分区数据
hive> load data local inpath '/home/Hadoop/input/hive/partitions/file1' into table logs partition (dt='2001-01-01',country='GB');
hive> load data local inpath '/home/BlueBreeze/data/t_hft_1.csv' overwrite into table t_hft partition(tradeDate=20130627);
查看分区表
hive> SHOW PARTITIONS t_hft;

六、视图

创建视图
hive> CREATE VIEW v_hive AS SELECT a,b FROM t_hive;

查看视图详细信息

hive> DESCRIBE EXTENDED valid_records;

七、函数

显示所有函数
hive> show functions;
查看函数用法
hive> describe function substr;

八、多表关联

内连接
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> select col1[0],col2['b'],col3.c from complex;

#创建新表

hive> CREATE TABLE t_hive (a int, b int, c int) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

#导入数据t_hive.txt到t_hive表

hive> LOAD DATA LOCAL INPATH '/home/cos/demo/t_hive.txt' OVERWRITE INTO TABLE t_hive ;

#正则匹配表名

hive>show tables '*t*';

#增加一个字段

hive> ALTER TABLE t_hive ADD COLUMNS (new_col String);

#重命令表名

hive> ALTER TABLE t_hive RENAME TO t_hadoop;

#从HDFS加载数据

hive> LOAD DATA INPATH '/user/hive/warehouse/t_hive/t_hive.txt' OVERWRITE INTO TABLE t_hive2;

#从其他表导入数据

hive> INSERT OVERWRITE TABLE t_hive2 SELECT * FROM t_hive ;

#创建表并从其他表导入数据

hive> CREATE TABLE t_hive AS SELECT * FROM t_hive2 ;

#仅复制表结构不导数据

hive> CREATE TABLE t_hive3 LIKE t_hive;

#通过Hive导出到本地文件系统

hive> INSERT OVERWRITE LOCAL DIRECTORY '/tmp/t_hive' SELECT * FROM t_hive;

#Hive查询HiveQL

from ( select b,c as c2 from t_hive) t select t.b, t.c2 limit 2;

select b,c from t_hive limit 2;

#创建视图

hive> CREATE VIEW v_hive AS SELECT a,b FROM t_hive;

#删表

drop table if exists t_hft;

#创建分区表

DROP TABLE IF EXISTS t_hft;
CREATE TABLE t_hft(
SecurityID STRING,
tradeTime STRING,
PreClosePx DOUBLE
) PARTITIONED BY (tradeDate INT)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

#导入分区数据

hive> load data local inpath '/home/BlueBreeze/data/t_hft_1.csv' overwrite into table t_hft partition(tradeDate=20130627);

#查看分区表

hive> SHOW PARTITIONS t_hft;

猜你喜欢

转载自blog.csdn.net/u010285974/article/details/86306774