1. 程式人生 > >2018-09-27#hive寫資料方式總結

2018-09-27#hive寫資料方式總結

hive 表寫入資料的方式

少量資料 insert into

create table dw.dim_area_code (
country_name string comment "國家名稱", country_code string comment "國家程式碼", province_name string comment "省份名稱", city_name string comment "地級市", city_area_code string comment "城市程式碼", city_zip_code string comment "城市郵編", county_name string comment "縣級市、區名稱", county_area_code string comment "縣級市、區名程式碼", county_zip_code string comment "縣級市、區名郵編" )STORED as ORC TBLPROPERTIES ("comment"="區域程式碼表 by zhangjiqiang") ; insert into dw.dim_area_code values ('中國',86,'北京市','北京市',010,100000,'東城區',010,100000), ('中國',86,'北京市','北京市',010,100000,'西城區',010,100000), ('中國',86,'北京市','北京市',010,100000,'崇文區',010,100000), ('中國',86,'北京市','北京市',010,100000,'宣武區',010,100000), ('中國',86,'北京市','北京市',010,100000,'朝陽區',010,100000); 

大檔案 load data

資料量比較大的,可以將資料存放成 csv 格式的檔案,通過 hive 的 load data 命令載入到 hive 表,由於資料是文字檔案,因此,此處的 hive 表也必須是普通表。 否則在使用 load data 命令時,hive 會提示SemanticException Unable to load data to destination table. Error: The file that you are trying to load does not match the file format of the destination table.

建立普通表

DROP TABLE IF EXISTS `dw.tmp_dim_phone_segment_info`; CREATE TABLE `dw.tmp_dim_phone_segment_info` ( `id` int comment '原來 mysql 自增 id', `phone_pref` string comment '號碼字首', `phone_segment` string comment '號段', `province_name` string comment '省份名稱', `city_county_name` string comment '地級市 or 縣級市 or 區城市名稱', `vendor` string comment '運營商', `zip_code` string comment '郵編', `phone_area_code` string comment '電話號碼區域程式碼', `addr_area_code` string comment '行政區域劃分程式碼' ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' TBLPROPERTIES ("comment"="手機號碼段詳細資訊 by zhangjiqiang"); load data local inpath '/home/dev/zhangjq/dim_phone_segment_info_insert.csv' into table dw.tmp_dim_phone_segment_info; 

資料壓縮

普通檔案格式的表比較佔用空間,orc 是 Apache 頂級專案,高壓縮率

DROP TABLE IF EXISTS `dw.dim_phone_segment_info`; CREATE TABLE `dw.dim_phone_segment_info` ( `id` int comment '原來 mysql 自增 id', `phone_pref` string comment '號碼字首', `phone_segment` string comment '號段', `province_name` string comment '省份名稱', `city_county_name` string comment '地級市 or 縣級市 or 區城市名稱', `vendor` string comment '運營商', `zip_code` string comment '郵編', `phone_area_code` string comment '電話號碼區域程式碼', `addr_area_code` string comment '行政區域劃分程式碼' ) stored as ORC TBLPROPERTIES ("comment"="手機號碼段詳細資訊 by zhangjiqiang"); insert overwrite table dw.dim_phone_segment_info select a.id, a.phone_pref, a.phone_segment, a.province_name, a.city_county_name, a.vendor, a.zip_code, a.phone_area_code, a.addr_area_code from dw.tmp_dim_phone_segment_info a 

建立表時同時寫入資料

drop table temp.zhjq_tmp_cc_phone;
create table temp.zhjq_tmp_cc_phone as select aa.user_num, aa.user_area_code, aa.user_type, aa.user_vendor, aa.user_province, aa.user_city from temp.icsoc_call_detail_bill_bill201807 aa where length(trim(a.user_num)) > 0 ;