Hive之命令

说明:此博客只记录了一些常见的hql,create/select/insert/update/delete这些基础操作是没有记录的。

一、时间级

select
day -- 时间
,date_add(day,1 - dayofweek(day)) as week_first_day -- 本周第一天_周日
,date_add(day,7 - dayofweek(day)) as week_last_day -- 本周最后一天_周六
,date_add(day,1 - case when dayofweek(day) = 1 then 7 else dayofweek(day) - 1 end) as week_first_day -- 本周第一天_周一
,date_add(day,7 - case when dayofweek(day) = 1 then 7 else dayofweek(day) - 1 end) as week_last_day -- 本周最后一天_周日
,next_day(day,'TU') as next_tuesday -- 当前日期的下个周二
,trunc(day,'MM') as month_first_day -- 当月第一天
,last_day(day) as month_last_day -- 当月最后一天
,to_date(concat(year(day),'-',lpad(ceil(month(day)/3) * 3 -2,2,0),'-01')) as season_first_day -- 当季第一天
,last_day(to_date(concat(year(day),'-',lpad(ceil(month(day)/3) * 3,2,0),'-01'))) as season_last_day -- 当季最后一天
,trunc(day,'YY') as year_first_day -- 当年第一天
,last_day(add_months(trunc(day,'YY'),12)) as year_last_day -- 当年最后一天
,weekofyear(day) as weekofyear -- 当年第几周
,second(day) as second -- 秒钟
,minute(day) as minute -- 分钟
,hour(day) as hour -- 小时
,day(day) as day -- 日期
,month(day) as month -- 月份
,lpad(ceil(month(day)/3),2,0) as season -- 季度
,year(day) as year -- 年份
from (
select '2018-01-02 01:01:01' as day union all
select '2018-02-02 02:03:04' as day union all
select '2018-03-02 03:05:07' as day union all
select '2018-04-02 04:07:10' as day union all
select '2018-05-02 05:09:13' as day union all
select '2018-06-02 06:11:16' as day union all
select '2018-07-02 07:13:19' as day union all
select '2018-08-02 08:15:22' as day union all
select '2018-09-02 09:17:25' as day union all
select '2018-10-02 10:19:28' as day union all
select '2018-11-02 11:21:31' as day union all
select '2018-12-02 12:23:34' as day
) t1
;

1.1 获取年、月、日、小时、分钟、秒、当年第几周

select
year('2018-02-27 10:00:00') as year
,month('2018-02-27 10:00:00') as month
,day('2018-02-27 10:00:00') as day
,hour('2018-02-27 10:00:00') as hour
,minute('2018-02-27 10:00:00') as minute
,second('2018-02-27 10:00:00') as second
,weekofyear('2018-02-27 10:00:00') as weekofyear
;
+-------+--------+------+-------+---------+---------+-------------+--+
| year | month | day | hour | minute | second | weekofyear |
+-------+--------+------+-------+---------+---------+-------------+--+
| 2018 | 2 | 27 | 10 | 0 | 0 | 9 |
+-------+--------+------+-------+---------+---------+-------------+--+

1.2 系统时间

-- 获取系统时间
select from_unixtime(unix_timestamp());
select from_unixtime(unix_timestamp(),'yyyy--MM--dd HH:mm:ss') as current_time;
-- 获取当天时间(yyyy-MM-dd)
select current_date;
-- 获取系统时间戳
select unix_timestamp();
-- 时间戳转日期
select from_unixtime(unix_timestamp(),'yyyyMMdd');
-- 日期加减:date_add(时间,增加天数) date_sub(时间,减少天数),返回日期减少天后的日期
select regexp_replace(date_add(FROM_UNIXTIME(UNIX_TIMESTAMP()),-1),'-','');
select regexp_replace(date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP()),1),'-',''); -- 日期差值:datediff(结束日期,开始日期),返回结束日期减去开始日期的天数。
select datediff(CURRENT_DATE,'2017-01-01') as datediff; -- 查询当前系统时间(包括毫秒数)
select current_timestamp;
-- 查询当月第几天
select dayofmonth(current_date);
-- 月末
select last_day(current_date);
-- 当月第1天
select date_sub(current_date,dayofmonth(current_date)-1);

二、库表级

-- 查看所有库信息
show database;
-- 进入指定数据库
use {dbName};
-- 查看指定库的所有表
show tables ;
-- 正则表达式过滤表
show tables 'table*'; -- 查看hive表信息
desc formatted {dbName.tabName};
-- 查看hive库信息
describe database extended {dbName};
-- 查询表的某一列
describe {dbName.tabName.fieldName};
-- 查看表结构详情
show create table {dbName.tabName};
-- 查看表的分区
show partitions tablename;

三、字段级

-- 删除hive库以及库中的表
drop database {dbName} CASCADE;
-- hive增加主键
alter table {tabName} set TBLPROPERTIES('PRIMARY_KEY'='field1,field2,field3');
-- 添加字段
alter table {dbName.tabName} add columns(column_1 int,column_2 string);
-- 修改字段名称
alter table {dbName.tabName} change column_1 new_column_1 int;
-- 删除字段(hive暂不支持)
alter table {dbName.tabName} drop columns column_1;
-- 修改字段注释
alter table {dbName.tabName} change column column_1 column_1 string COMMENT '字段注释';

四、其他

-- 字符串拼接
select concat('c1','c2') as c3;
-- 查看数据库中所有的表
show tables in {dbName};
-- 添加jar包
add jar hdfs://testUDF.jar;
-- 创建临时函数
create temporary function dbName.funName as '{com.libt.testUDF}';
-- 创建永久函数
create function dbName.funName as '{com.libt.testUDF}';

五、优化

-- 设置执行引擎
set hive.execution.engine=mr;
-- 设置队列
set mapreduce.job.queuename=dev;
--开启动态分区
set hive.exec.dynamic.partition=true;
--允许所有分区为动态分区
set hive.exec.dynamic.partition.mode=nonstrict; - analyze 普通表
analyze table dbName.tableName compute statistics;
- analyze 分区表
analyze table dbName.tableName partition(ds) compute statistics;
- 重新计算元数据
msck repair table tableName;

Hive之命令的更多相关文章

  1. 分区表,桶表,外部表,以及hive一些命令行小工具

    hive中的表与hdfs中的文件通过metastore关联起来的.Hive的数据模型:内部表,分区表,外部表,桶表受控表(managed table):包括内部表,分区表,桶表 内部表: 我们删除表的 ...

  2. [Spark][Hive]Hive的命令行客户端启动:

    [Spark][Hive]Hive的命令行客户端启动: [training@localhost Desktop]$ chkconfig | grep hive hive-metastore 0:off ...

  3. Hive Shell 命令详解

    Hive服务介绍 Hive默认提供的cli(shell)服务,如果需要启动其他服务,那么需要service参数来启动其他服务,比如thrift服务.metastore服务等.可以通过命令hive -- ...

  4. Hive shell 命令

    Hive shell 命令. 连接 hive shell 直接输入 hive 1.显示表 hive> show tables; OK test Time taken: 0.17 seconds, ...

  5. 【原创】官方文档-hive 启动命令

    [一起学Hive]之十六-Hive的WEB页面接口-HWI Apache Hive 管网 hive metrics hive常用命令整理 Hive学习之HiveServer2服务端配置与启动 启动hi ...

  6. Hive 常用命令和语句

    示例数据库为 db_hive 1. 创建表 create-table.sql create table if not exists db_hive.tb_user ( id int, username ...

  7. 1.10-1.11 hive交互式命令讲解

    一.hive 交互式命令参数 #帮助 [root@hadoop-senior hive-0.13.1]# bin/hive -h Missing argument for option: h usag ...

  8. Hive常用命令

    本位为转载,原地址为:http://www.cnblogs.com/BlueBreeze/p/4232421.html #创建新表 hive> CREATE TABLE t_hive (a in ...

  9. hive lock命令的使用

    1.hive锁表命令 hive> lock table t1 exclusive;锁表后不能对表进行操作 2.hive表解锁: hive> unlock table t1; 3.查看被锁的 ...

随机推荐

  1. 并发刺客(False Sharing)——并发程序的隐藏杀手

    并发刺客(False Sharing)--并发程序的隐藏杀手 前言 前段时间在各种社交平台"雪糕刺客"这个词比较火,简单的来说就是雪糕的价格非常高!其实在并发程序当中也有一个刺客, ...

  2. Redis系列5:深入分析Cluster 集群模式

    Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) 1 背景 前面我们 ...

  3. Visual Studio Code 中文设置教程

    本文仅供学习交流使用,如侵立删!demo下载见文末 Pycharm中文设置教程 1.首先打开VisualStudioCode,点击扩展:extensions. 2.搜索chinese. 3.选择需要的 ...

  4. 888. 公平的糖果交换--LeetCode

    来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/fair-candy-swap 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 假 ...

  5. [HDU6057] Kanade‘s convolution (FWT)

    题面 出自HDU6057 给你两个数列 A [ 0... 2 m − 1 ] A[0...2^m-1] A[0...2m−1] 和 B [ 0... 2 m − 1 ] B[0...2^m-1] B[ ...

  6. Docker 搭建 Nexus3 私服 | 基本操作

    1 Docker 安装 Nexus3 1.1 创建目录 在硬盘上创建 Nexus3 的主目录: mkdir -p /Users/yygnb/dockerMe/nexus3 为该目录添加权限: chmo ...

  7. docker访问外部https数字证书问题

    一般我们构建的 docker 镜像使用的都是 alpine linux 系统,默认是不带 ca-certificates 根证书的,导致无法识别外部 https 携带的数字证书. 在访问的时候,会抛出 ...

  8. MariaDB数据库 主-从 部署

    〇.前言 好久没碰数据库了 准备环境: centos7自带的MariaDB,没有的话下面是安装命令 yum install -y mariadb mariadb-server systemctl re ...

  9. Java 多线程:并发编程的三大特性

    Java 多线程:并发编程的三大特性 作者:Grey 原文地址: 博客园:Java 多线程:并发编程的三大特性 CSDN:Java 多线程:并发编程的三大特性 可见性 所谓线程数据的可见性,指的就是内 ...

  10. Java 多线程:锁(三)

    Java 多线程:锁(三) 作者:Grey 原文地址: 博客园:Java 多线程:锁(三) CSDN:Java 多线程:锁(三) StampedLock StampedLock其实是对读写锁的一种改进 ...