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. (转)git使用收集

    由于最近项目开始弃SVN用git,特意整理下git命令.原文链接为http://www.jb51.net/article/55442.htm git branch 查看本地所有分支git status ...

  2. 关于hive分区,你知道多少呢?

    ​ 文末查看关键字,回复赠书 一.理论基础 1.Hive分区背景 在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入 ...

  3. Apache DolphinScheduler 架构演进介绍及开源经验分享 - eBay 阮文俊

    引言 来自 eBay 的文俊同学在近期的上海开源大数据 Meetup 上做了十分精彩的 "Apache DolphinScheduler 的架构演进" 分享.本次分享有近 200 ...

  4. React报错之React hook 'useState' cannot be called in a class component

    正文从这开始~ 总览 当我们尝试在类组件中使用useState 钩子时,会产生"React hook 'useState' cannot be called in a class compo ...

  5. 云服务器上搭建cobalt strike遇到的一些小问题

    一.前言: 当你兴高采烈的买了一台云服务器,迫不及待地想去搭建传说中的神器cobalt strike的时候,你可能会遇到以下的一些小问题,这里我会列出对应的解决方法. 二.遇到的一些小问题 1.上传文 ...

  6. 「雅礼集训 2017 Day2」水箱 (数据结构+dp ,一个log)

    题面 题解 在网上看到有些做法,有什么平衡树.启发式合并等等总之复杂度O(Tnlog^2(n))的不优做法,这里我就用一个O(Tnlogn)的做法好了 其实大体上推导的思路都是一样的. 我们很容易发现 ...

  7. java数组---初始化

    public class ArrayDemo { public static void main(String[] args) { int[] a={1,2,3,4,5,6,7,8,9}; //静态初 ...

  8. 给ShardingSphere提了个PR,不知道是不是嫌弃我

    说来惭愧,干了 10 来年程序员,还没有给开源做过任何贡献,以前只知道嘎嘎写,出了问题嘎嘎改,从来没想过提个 PR 去修复他,最近碰到个问题,发现挺简单的,就随手提了个 PR 过去. 问题 问题挺简单 ...

  9. 第四十八篇:webpack的基本使用(二) --安装和配置webpack-dev-server插件

    好家伙, 1.webpack中的默认约定 默认的打包入口文件为src  -->index.js 默认的输出文件路径为dist -->main.js 既然有默认,那么就说明肯定能改 2.en ...

  10. 闭包 与 js内存管理

    参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Memory_Management            https://blog ...