收藏一些Hive数据库常用命令,方便平时准备测试数据集。
查看库、创建普通Hive表:
show databases;
user lch_databases;
show tables;
create table lch_user_table(
int id,
name varchar(8),
birthday date,
salary double(24.7),
dd1 decimal(18,4)
);
alert table lch_user_table add columns(性别 string comment '男女');
修改Hive表
alert table lch_user_table RENAME TO new_name;
alert table lch_user_table ADD COLUMNS (col_spec[, col_spec ...]);
alert table lch_user_table ADD COLUMNS(性别 string comment 男女');
alert table lch_user_table DROP [COLUMN] column_name;
alert table lch_user_table CHANGE column_name new_name new_type;
创建分区Hive表:
create table lch_test_table(
order_number string,
event_time string )
PARTITIONED BY(event_month string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
(1) 单分区建表
单分区表,按天分区,在表结构中存在id,content,dt三列。以dt为文件夹区分
create table day_table (id int, content string) partitioned by (dt string);
(2) 双分区建表
双分区表,按天和小时分区,在表结构中新增加了dt和hour两列。先以dt为文件夹,再以hour子文件夹区分
create table day_hour_table (id int, content string) partitioned by (dt string, hour string);
向有分区的表插入数据
(1) 覆盖现有分区数据,如果没有该指定分区,新建该分区,并且插入数据
INSERT OVERWRITE TABLE 库名.表名 PARTITION(dt='2018-09-12',name='Tom', ...)
SELECT ... FROM 库名.表名 where...
(2) 向现有的分区插入数据 (之前的数据不会被覆盖)
INSERT INTO TABLE 库名.表名 PARTITION(dt='2018-09-12',name='Tom',...)
SELECT ... FROM 库名.表名 WHERE ...
向无分区的表插入数据
(1) 覆盖原有表里的数据,命令和有分区的表类似,只是去掉后面的
PARTITION(dt=' ',name=' ')
insert overwrite table 库名.表名
select ... frome 库名.表名 where...
(2) 向现有的表插入数据 (之前的数据不会被覆盖)
insert into talbe 库名.表名
select ... from 库名.表名 where ...
insert into table lch_user_table values
('Lucy','2019-9-18',13493.13,0.12,'女');
跨库复制表
CREATE TABLE NEWDB.NEW_TABLE2 AS select * from OLDDB.OLD_TABLE2;
批量执行命令行
beeline -e "select count() from user;" >>output.txt
beeline -e "select count() from info;" >>output.txt
beeline -e "use bicoredata;" "show tables;" >>output.txt