表、程序名命名规则:层级+模块+业务+行为+周期;
创建存储格式为ORC格式的表:压缩比1:50
查询性能最快
hive的常用操作命令
查看库 show databases;
使用库(要操作的库) use 库名;
查看库中有那些表 show tables;
查看表结构 desc tdw_credit_bass_m; --- tdw_credit_bass_m 表名
查看表结构 describe extended table_name
查看建表语句 show create table tdw_credit_bass_m ;
查看分区 show partitions tdw_credit_bass_m;
删除分区
alter table tdw_credit_bass_m drop if exists partition(statis_month=201701);
alter table tdw_credit_bass_m drop partition(statis_month=201701);
清空分区:TRUNCATE table proj_mbgprc_dcp_dev.mbg3s_mbg_inventory_cdl PARTITION(report_date= '2019-07-10')
增加分区
alter table tdw_credit_bass_m add if not exists partition(statis_month=201701);
alter table tdw_credit_bass_m add partition(statis_month=201701);
往分区插数据
insert into tdw_credit_bass_m partiton(statis_month=201701) --指定分区插入,必须有201701这个分区
select ...
insert into tdw_credit_bass_m partiton(statis_month) --动态分区,根据statis_month值自动判断分区,如果没有这个分区,会自动创建
select ...
往分区插入数据一般使用
alter table tdw_credit_bass_m drop if exists partition(statis_month=201701);
alter table tdw_credit_bass_m add if not exists partition(statis_month=201701);
insert into tdw_credit_bass_m partiton(statis_month=201701)
...
或
alter table tdw_credit_bass_m drop if exists partition(statis_month=201701);
insert into tdw_credit_bass_m partiton(statis_month)
...
增加列
alter table tdw_credit_bass_m add columns (imei string);
修改列名
alter table tdw_credit_bass_m change imei imei_new string;
// 删除字段(使用新schema替换原有的)
ALTER TABLE test REPLACE COLUMNS(id BIGINT, name STRING);
原有Hive表test_change中有a,b,c,d,e这几个字段
将从test_change中删除“d”列:
ALTER TABLE test_change REPLACE COLUMNS (a int, b int,c string,e string);
将d和e两列一起删除:
ALTER TABLE test_change REPLACE COLUMNS (a int, b int,c string);
获取日期数据第几周:
select weekofyear(to_date(from_unixtime(unix_timestamp(substr(activation_time,1,8),'yyyymmdd'),'yyyy-mm-dd'))) from proj_mbgprc_dcp_dev.mid_mbg_report_so_cdl
查询两张表不同的数据
select count(a.bill_date) from proj_mbgprc_dcp_dev.ludb_time_test11 a left outer join
proj_mbgprc_dcp_dev.ludb_time_test1 b on a.bill_date=b.bill_date and a.bill_date is null
2、替换函数
select cast(substring(regexp_replace('2016-06-05 00:00:00.0', '-', ''),1,8) as int);
结果--》20160605