爲什麼要自建落地表
1、公司的大數據資源非常緊張,
2、導數邏輯很複雜;
3、日常導數要求時效性高;
4、部門數倉的表太多字段;
綜上四點,還有其他的原因;都需要我們自己落建地表,方便自己查詢,以及加快BI報表直接用複雜的SQL語句跑數時間。
HIVE SQL創建內/外部表、分區表
1、創建內部表:
CREATE TABLE `test`(
`order_id` decimal(22,0) COMMENT '源表自增id',
`kh_code` string COMMENT '客戶編碼',
`user_name` string COMMENT '用戶名',
`kh_name` string COMMENT '客戶姓名',
`company` string COMMENT '子公司名稱',
`kh_lx` string COMMENT '客戶類型',
`lx_ren` string COMMENT '聯繫人',
`cell_num` string COMMENT '手機號碼',
`sale_ren` string COMMENT '銷售人員',
`customer_service` string COMMENT '客服人員',
`status` string COMMENT '狀態')
COMMENT '雜費類型維度表' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\001' STORED AS
textfile LOCATION '/bigdata/test'
執行語句後,沒有報錯;則創建表成功
desc test
--查看自建表的字段
輸出結果:
備註:數據類型一列,如果是時間可以用時間戳:timestamp,數值可以用decimal(32,10),後面的10是保留的小數點位數;
1、創建外部表:
CREATE EXTERNAL TABLE `test`(
`order_id` decimal(22,0) COMMENT '源表自增id',
`kh_code` string COMMENT '客戶編碼',
`user_name` string COMMENT '用戶名',
`kh_name` string COMMENT '客戶姓名',
`company` string COMMENT '子公司名稱',
`kh_lx` string COMMENT '客戶類型',
`lx_ren` string COMMENT '聯繫人',
`cell_num` string COMMENT '手機號碼',
`sale_ren` string COMMENT '銷售人員',
`customer_service` string COMMENT '客服人員',
`status` string COMMENT '狀態')
COMMENT '雜費類型維度表' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\001' STORED AS
textfile LOCATION '/bigdata/test'
備註:外部表跟內部表差別就是CREATE EXTERNAL TABLE test
,外部表多了EXTERNAL;其他的語句是一樣的
!!!!!!!
內部表/外部表的區別:內部表數據由Hive自身管理,外部表數據由HDFS管理
內部表可以被drop掉刪除後什麼都沒有,再建立表名稱一樣的表,裏面數據是
爲空的,
外部表被drop 掉,還可以恢復,再建立表名稱一樣的表,裏面數據是還存在的
建議重要的數據都建立外部表,否則刪除後就找不回來了
!!!!!!!
3、創建分區表:
--1、建外部表,並建立分區:
create table `dss_lgt_package_big_mid_f`
(
`ship_time` timestamp comment '發貨時間',
`bu_code` string comment '訂單事業部編碼',
`oms_weight` decimal(32,10) comment '出庫重量(KG)',
`oms_volume` decimal(32,10) comment '體積重量(KG)',
`stock_name` string comment '訂單倉庫名稱',
`pick_create_time` timestamp comment '配貨時間',
`express_level_name` string comment '物流級別名稱',
`express_name` string comment '訂單物流方式名稱',
`logistics_cn_name` string comment '物流產品名稱',
`generate_time` timestamp comment '包裹生成時間',
`handover_finish_time` timestamp comment '交接完成時間',
`entity_stock_name` string comment '實體倉',
`ship_stock_name` string comment '發貨倉庫',
`time_level_name` string comment '時效等級',
`big_area` string comment '大區域',
`area` string comment '區域',
`des_country_code` string comment '目的國家',
`weight` decimal(32,10) comment '實重(KG)',
`volume_weight` decimal(32,10) comment '體積重(KG)',
`package_created_at` timestamp comment '裝箱時間',
`opt_warehouse` string comment '操作倉庫',
`online_date` timestamp comment '上網時間',
`sign_date` timestamp comment '簽收時間',
`etl_update_date` timestamp comment '數據更新時間',
`etl_insert_date` timestamp comment '數據創建時間'
)
partitioned by (fq_day string)
LOCATION '/bigdata/dss_lgt_package_big_mid_f'
分區字段:fq_day
踩過的坑:
分區字段不要用時間戳,否則分區粒度太小沒有起到分區的作用
desc dss_lgt_package_big_mid_f
輸出結果:
insert overwrite 向分區表插入數據
1.代碼如下:
--2、開啓動態分區,然後用insert overwrite方式更新,用分區做條件進行覆蓋更新,類似帶where條件的更新
set hive.exec.dynamic.partition=true;--使用動態分區
set hive.exec.dynamic.partition.mode=nonstrict; --無限制模式
--如果模式是strict,則必須有一個靜態分區,且放在最前面。
insert overwrite table dss_lgt_package_big_mid_f
partition(fq_day)
select
*
from
(
select
xb.ship_time,
xb.bu_code,
xb.oms_weight,
xb.oms_volume,
xb.stock_name,
xb.pick_create_time,
xb.express_level_name,
xb.express_name,
xb.logistics_cn_name,
xb.generate_time,
xb.handover_finish_time,
xb.entity_stock_name,
xb.ship_stock_name,
xb.time_level_name,
xb.big_area,
xb.area,
xb.des_country_code,
xb.weight,
xb.volume_weight,
xb.package_created_at,
xb.opt_warehouse,
xb.online_date,
xb.sign_date,
xb.etl_update_date,
xb.etl_insert_date,
date_format(xb.etl_update_date,'yyyyMM') as fq
from test1 xb) a
where
fq >=date_format(date_sub(from_unixtime(unix_timestamp()),10),'yyyyMM')
運行成功就可以了。
輸出結果:
插入數踩過的坑:
① dynamic 這個單詞寫錯,網上有些大神寫成了dynamici,害我一直運行報錯;
② 分區插入數據時,需要與目標表創建的字段排序一樣,字段個數一樣;然後需要把分區字段放在最後一個(系統默認最後一個爲分區字段)
③ 提交查詢語句的時候,一定要全選(點一下hive編輯區,ctrl+A,在點擊運行),然後再按執行,否則就會一直報錯
然後中間表的數據導入成功了。
然後在做一個定時任務,每天下班後跑數據,明天直接用中間表導數,那就非常快了,而且消耗的資源會少很多。