hive终端常用指令

1、在shell里面进入beeline用户

beeline -u 'jdbc:hive2://100.01.01.01:10001' -n <账号> -p '<密码>' 

2、查看库/表

show databases/tables; 

3、查看表结构

desc table_name;

4、创建表

CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.] table_name [(col_name data_type [COMMENT col_comment], ...)] [COMMENT table_comment] [ROW FORMAT row_format] [STORED AS file_format]

5、删除表或数据库

drop table table_name;

drop database database_name;

6、删除表中数据

truncate table table_name;

7、添加分区

alter table my_partition_test_table if not exists add partition (pdate=’20191011’);

8、按分区删除数据

ALTER TABLE table_name DROP [IF EXISTS] PARTITION partition_spec, PARTITION partition_spec,...

alter table table_name drop partition (partition_name='分区名')

9、创建测试表

create table ods_cmos.ods_bas_cmft_cosop_t_client_abnormal_info_test as select * from ods_cmos.ods_bas_cmft_cosop_t_client_abnormal_info where 1=0;

10、从测试表插入数据到目标表

带分区

INSERT OVERWRITE TABLE ods_bak_t_branch_info PARTITION (pdate = '2018-10-29')
SELECT * from ods_bak_t_branch_info_test WHERE pdate='2018-10-29'

不带分区

INSERT OVERWRITE TABLE ods_bak_t_branch_info 
SELECT * from ods_bak_t_branch_info_test 

11、插入数据

LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]

  • LOCAL是标识符指定本地路径。它是可选的。
  • OVERWRITE 是可选的,覆盖表中的数据。
  • PARTITION 这是可选的

12、修改表

ALTER TABLE name RENAME TO new_name

ALTER TABLE name ADD COLUMNS (col_spec[, col_spec ...])

ALTER TABLE name DROP [COLUMN] column_name

ALTER TABLE name CHANGE column_name new_name new_type

ALTER TABLE name REPLACE COLUMNS (col_spec[, col_spec ...])

1)先添加字段到最后(add columns)

alter table table_name add columns (c_time string comment '当前时间');

2)再移动到指定位置(change)

alter table table_name change c_time c_time string after address ;

13、内置运算符

1)关系运算符

 

2)算术运算符

这些运算符支持的操作数各种常见的算术运算。所有这些返回数字类型

 

3)逻辑运算符

运算符是逻辑表达式。所有这些返回TRUE或FALSE。

 

4)复杂运算符

 

14、内置函数

函数

返回类型

描述

例子

round(double a)

BIGINT

四舍五入

SELECT round(2.6);
->3.0

floor(double a)

BIGINT

向下取整

SELECT floor(2.6);
->2.0

ceil(double a)

BIGINT

向上取整

SELECT ceil(2.6) ;
->3.0

rand(), rand(int seed)

double

它返回一个随机数,从行改变到行。

 

concat(string A, string B,...)

string

它返回从A后串联B产生的字符串

select concat('abcde','fg')

->abcdefg

substr(string A, int start)

string

返回字符串A从start位置到结尾的字符串

select substr('abcde',3)

->cde

substr(string A, int start, int length)

string

返回字符串A从start位置开始,长度为len的字符串

select substr('abcde',3,2)

->cd

upper(string A)

string

将字符串A中的字母转换成大写字母

 

ucase(string A)

string

将字符串A中的字母转换成大写字母

 

lower(string A)

string

字符串转小写函数

 

lcase(string A)

string

字符串转小写函数

 

trim(string A)

string

去除字符串两边的空格  

 

 

ltrim(string A)

string

去除字符串左边的空格

 

 

rtrim(string A)

string

去除字符串右边的空格

 

 

regexp_replace(string A, string B, string C)

string

B将字符串A中符合条件的部分成C所指定的字符串

 

size(Map<K.V>)

int

求map的长度

 

size(Array<T>)

int

求数组的长度

 

cast(<expr> as <type>)

value of <type>

将expr转换成type类型

cast(value as double) value

from_unixtime(int unixtime)

string

日期函数UNIX时间戳转日期函数

select from_unixtime(1323308943,'yyyyMMdd');

->20111208

to_date(string timestamp)

string

返回日期时间字段中的日期部分

select to_date('2011-12-08 10:03:01')

->2011-12-08

year(string date)

int

返回日期中的年

 

month(string date)

int

返回日期中的月份

 

 

day(string date)

int

返回日期中的天

 

hour   (string date)

int

返回日期中的小时

 

 

minute   (string date)

int

返回日期中的分钟

 

 

second   (string date)

int

返回日期中的秒

 

get_json_object(string json_string, string path)

string

 

 

nvl(表达式1,表达式2)

string

空值转换函数,1为空时则取2

 

 

 

15、聚合函数

 

count(*), count(expr)

BIGINT

count(*) - 返回检索行的总数。

 

sum(col), sum(DISTINCT col)

DOUBLE

返回该组或该组中的列的不同值的分组和所有元素的总和

 

avg(col), avg(DISTINCT col)

DOUBLE

返回上述组或该组中的列的不同值的元素的平均值。

 

min(col)

DOUBLE

返回该组中的列的最小值。

 

max(col)

DOUBLE

返回该组中的列的最大值。

 

推荐一个博文,函数整理的比较详细:

https://www.cnblogs.com/yejibigdata/p/6380744.html

16、视图和索引

1)创建视图

可以创建一个视图,在执行SELECT语句的时候。语法如下:

CREATE VIEW [IF NOT EXISTS] view_name [(column_name [COMMENT column_comment], ...) ] [COMMENT table_comment] AS SELECT ...

2)删除视图

DROP VIEW view_name

3)创建索引

CREATE INDEX index_name ON TABLE base_table_name (col_name, ...) AS 'index.handler.class.name' [WITH DEFERRED REBUILD] [IDXPROPERTIES (property_name=property_value, ...)] [IN TABLE index_table_name] [PARTITIONED BY (col_name, ...)] [ [ ROW FORMAT ...] STORED AS ... | STORED BY ... ] [LOCATION hdfs_path] [TBLPROPERTIES (...)]

4)删除索引

DROP INDEX <index_name> ON <table_name>

17、SELECT查询

SELECT [ALL | DISTINCT] select_expr, select_expr, ... FROM table_reference [WHERE where_condition] [GROUP BY col_list] [HAVING having_condition] [CLUSTER BY col_list | [DISTRIBUTE BY col_list] [SORT BY col_list]]

18、ORDER BY

SELECT [ALL | DISTINCT] select_expr, select_expr, ... FROM table_reference [WHERE where_condition] [GROUP BY col_list] [HAVING having_condition] [ORDER BY col_list]] [LIMIT number];

19、GROUP BY

SELECT [ALL | DISTINCT] select_expr, select_expr, ... FROM table_reference [WHERE where_condition] [GROUP BY col_list] [HAVING having_condition] [ORDER BY col_list]] [LIMIT number];

20、JOIN

join_table: table_reference JOIN table_factor [join_condition] | table_reference {LEFT|RIGHT|FULL} [OUTER] JOIN table_reference join_condition | table_reference LEFT SEMI JOIN table_reference join_condition | table_reference CROSS JOIN table_reference [join_condition]

21、日期格式转换

select from_unixtime(unix_timestamp('20180905','yyyymmdd'),'yyyy-mm-dd')

select from_unixtime(unix_timestamp('2018-09-05','yyyy-mm-dd'),'yyyy-mm-dd hh:mm:ss')

posted @ 2019-11-22 19:26  七彩木兰  阅读(755)  评论(0编辑  收藏  举报