Hive字段動態分區、parquet表的創建、從回收站恢復數據


創建parquet分區的表 速度更快

----創建parquet表,並指定壓縮格式

create table xyy_temp_data.temp_dwd_b2b_ec_tb_order_detail
(
id bigint comment '訂單明細',
order_no string comment '訂單編號',
branch_code string comment '區域編碼',
shop_pattern_code string comment '店鋪業務模式編碼'
)
COMMENT '銷售訂單明細表_全量表'
stored as parquet
TBLPROPERTIES ('parquet.compression'='SNAPPY');
;

TBLPROPERTIES ('parquet.compression'='GZIP');  ----GZIP壓縮格式  可以大大降低存儲

 

COMMENT '銷售訂單明細表_全量表'
partitioned by (dt string,erp_code string )
stored as parquet
TBLPROPERTIES ('parquet.compression'='SNAPPY');

 

CREATE TABLE xyy_test.demo(
merchant_id string COMMENT '商戶id')
stored as parquet
;

-------創建臨時表 指定 parquet

create table xyy_test.demo1 stored as parquet
as select * from xyy_test.demo
;

create table test (
id int,
name string,
score string
)partitioned by(dt string)
stored as parquet;


drop table xyy_temp_data.temp_new_so_fact ;
create table xyy_temp_data.temp_new_so_fact
stored as parquet TBLPROPERTIES ('parquet.compression'='SNAPPY')
as
SELECT
order_no,
sub_order_no,
outside_order_code, -- 外部訂單號, 目前只有智鹿b2c, 使用神農訂單號
parent_id,
status,
org_id,
sub_branch_code,
merchant_oem, -- 御元集協議葯店tag
order_source -- 訂單終端 0手動添加(管理后台下單-基本為客服操作補單) 1Android 2IOS 3H5 4PC
FROM xyy_bigdata_dwd.dwd_b2b_ec_subsidy_order_all
WHERE create_time >= '2020-01-01 00:00:00.0' -- 常量, 無需調整
;

 

 

CREATE TABLE if not exists xyy_bigdata_dim.order_activity_sponsor_table(
id int comment '主鍵',
activity_id bigint comment '活動id' ,
activity_type string comment '活動類型 活動類型 1-原價,2-特惠價格,3-秒殺價格,4-套餐價格,5-一口價價格,6-直降,7-贈品,8-拼團,9-滿減,10-滿折,11-滿贈,12-滿減贈,13-返點返券,14-通用券,15-商品券,16-折扣券,17-禮品券,18-新人券,19-疊加券,20-店鋪券,22-手動優惠,23-KA公司優惠,24-KA廠商優惠,26-余額抵扣,28-大轉盤' ,
initiator_id bigint comment '活動發起方ID' ,
name string comment '發起方名稱'
)
comment '訂單活動發起方匯總表'
partitioned by (dt string )
;

 

CREATE TABLE  if not exists xyy_app_data.purchase_stock_turnover_days
(
  provice_code string comment '省份編碼', 
  drug_code string comment '商品編碼' , 
  qualified_stock_amount double comment '期末庫存合格庫庫存金額', 
  storage_total_amt_tax double comment '期初庫存總金額含稅' , 
  sale_gmv double comment 'gmv' ,  
  kucn_day double comment '庫存周轉天數' 
  )
comment '采購報表_庫存周轉天數'
PARTITIONED BY ( dt string)
stored as parquet;

 

創建 \t  分割符的表

CREATE TABLE `xyy_test.purcase_20200330_data`(
`buyer` string, 
`user_name` string, 
`org_code` string, 
`drug_code` string, 
`qualified_stock_amount` double, 
`storage_total_amt_tax` double, 
`sale_gmv` double, 
`_c7` double)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' 

hdfs dfs -ls /data/hive/warehouse/xyy_test.db/purcase_20200330_data    查看當前表的存儲路徑

----執行結果如下:
< dev@node04-bigdata-prod-bj1:~/liu >$ hdfs dfs -ls /data/hive/warehouse/xyy_test.db/purcase_20200330_data
Found 1 items
-rwxrwxrwx   2 admin hive     459959 2020-03-30 16:16 /data/hive/warehouse/xyy_test.db/purcase_20200330_data/000000_0

hdfs dfs -get /data/hive/warehouse/xyy_test.db/purcase_20200330_data/000000_0 ./

下載到當前路徑下   
< dev@node04-bigdata-prod-bj1:~/liu >$  hdfs dfs -get  /data/hive/warehouse/xyy_test.db/purcase_20200330_data/000000_0  ./
< dev@node04-bigdata-prod-bj1:~/liu >$ ls -l
total 56932
-rw-r--r--. 1 dev dev   459959 Mar 30 16:20 000000_0

 

下載到本地
< dev@node04-bigdata-prod-bj1:~/liu >$ sz 000000_0
rz
 zmodem trl+C ȡ

  100%     449 KB  449 KB/s 00:00:01       0 Errors
--  刪除庫
drop database if exists db_name;
--  強制刪除庫
drop database if exists db_name cascade;

--  刪除表
drop table if exists employee;

--  清空表
truncate table employee;
--  清空表,第二種方式
insert overwrite table employee select * from employee where 1=0; 

--  刪除分區
alter table employee_table drop partition (stat_year_month>='2018-01');

--  按條件刪除數據
insert overwrite table employee_table select * from employee_table where id>'180203a15f';
-- 覆蓋分區里的數據
insert overwrite table part_test_3 partition(month_id='201805',day_id='20180509') select * from part_test_temp;
---修改表的注釋
ALTER TABLE xyy_app_data.user_portrait_ec_sku_coun SET TBLPROPERTIES('comment' = '用戶畫像es用到的表');

 

Hive根據表中某個字段動態分區

使用hive儲存數據時,需要對做分區,如果從kafka接收數據,將每天的數據保存一個分區(按天分區),保存分區時需要根據某個字段做動態分區,而不是傻傻的將數據寫到某一個臨時目錄最后倒入到某一個分區,這是靜態分區。

Hive動態分區步驟如下:

1、建立某一個源表模擬數據源並插入一些數據

create table t_test_p_source (
    id string,
    name string,
    birthday string
) 
row format delimited fields terminated by '\t'
stored as textfile;

insert into t_test_p_source values ('a1', 'zhangsan', '2018-01-01');
insert into t_test_p_source values ('a2', 'lisi', '2018-01-02');
insert into t_test_p_source values ('a3', 'zhangsan', '2018-01-03');
insert into t_test_p_source values ('a4', 'wangwu', '2018-01-04');
insert into t_test_p_source values ('a5', 'sanzang', '2018-01-05');
insert into t_test_p_source values ('a6', 'zhangsan2', '2018-01-01');

2、建立一張分區表 (按ds字段分區)

create table t_test_p_target (
    id string,
    name string
)
partitioned by (ds string)
row format delimited fields terminated by '\t'
stored as textfile;

3、向分區表中插入數據

SET hive.exec.dynamic.partition=true;   #是否開啟動態分區,默認是false,所以必須要設置成true
SET hive.exec.dynamic.partition.mode=nonstrict;    # 動態分區模式,默認為strict, 表示表中必須一個分區為靜態分區,nostrict表示允許所有字段都可以作為動態分區

insert into table t_test_p_target partition (ds) select id, name, birthday as ds from t_test_p_source;



set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
set hive.exec.max.dynamic.partitions.pernode=1000; 限制最大分區的個數
set hive.exec.max.dynamic.partitions =1000; 限制最大分區的個數

4、測試是否動態分區了

 

  2018-01-01這個分區只有2條數據,再來看下HDFS上的分區目錄

 

 至此,hive動態分區已經完成了。

hive從回收站恢復歷史數據

恢復分區數據,帶分區的表

步驟一、hdfs dfs -ls /user/admin/.Trash/Current/data/hive/warehouse/xyy_test.db/sale4_core_commodity_gmv  查找出回收站里的數據
    從/user/dev  或user/admin 一層一層往下找  一般在 .Trash這個里面
步驟二、執行之前的建表語句
CREATE TABLE if not exists   xyy_test.sale4_core_commodity_gmv(
province_name string comment '省份', 
create_time string comment '時間', 
hexin_gmv double comment '核心商品GMV')
comment '省區報表_核心商品GMV'
partitioned by (dt string)
stored as parquet
通過 show create  table xyy_test.sale4_core_commodity_gmv   /data/hive/warehouse/xyy_test.db/sale4_core_commodity_gmv  查看地址
步驟三、通過命令查看出 和步驟二 里的路徑一致  
< dev@node04-bigdata-prod-bj1:~ >$ hdfs dfs -ls /user/admin/.Trash/Current/data/hive/warehouse/xyy_test.db/sale4_core_commodity_gmv
Found 3 items
drwxrwxrwx   - admin hive          0 2020-04-10 17:17 /user/admin/.Trash/Current/data/hive/warehouse/xyy_test.db/sale4_core_commodity_gmv/dt=20200301
drwxrwxrwx   - admin hive          0 2020-04-10 17:17 /user/admin/.Trash/Current/data/hive/warehouse/xyy_test.db/sale4_core_commodity_gmv/dt=20200302
drwxrwxrwx   - admin hive          0 2020-04-10 17:17 /user/admin/.Trash/Current/data/hive/warehouse/xyy_test.db/sale4_core_commodity_gmv/dt=20200303

步驟四、執行下面的命令把文件 從回收站里 移出來  
hdfs dfs -mv /user/admin/.Trash/Current/data/hive/warehouse/xyy_test.db/sale4_core_commodity_gmv/*  /data/hive/warehouse/xyy_test.db/sale4_core_commodity_gmv
步驟五、將文件加載到表里 
alter table xyy_test.sale4_core_commodity_gmv add partition (dt = '20200301');  --- 一次性加載一個分區
alter table xyy_app_data.sale6_core_commodity_gmv add if not exists partition (dt = '20200302')       --- 一次性加載一個分區
alter  table xyy_test.sale4_core_commodity_gmv drop partition (dt >='20200301')  --批量刪除分區
alter table siebel_member drop if exists
partition(dt='20180401'),
partition(dt='20180402'),
partition(dt='20180403'),
partition(dt='20180404');

--批量加載分區不怎么好用
alter table xyy_test.sale4_core_commodity_gmv  add 
partition(dt='20200301') location '20200301'
partition(dt='20200302') location '20200302'
partition(dt='20200303') location 'dt='20200303';

恢復不帶分區數據   按照以上步驟,執行到步驟四就行了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM