1.下載lzo源碼包珊搀,然后進(jìn)行編譯扒磁,下載lzo的源碼包地址
https://github.com/twitter/hadoop-lzo/tree/release-0.4.20
打包編譯
mvn clean package
獲取編譯之后的jar包
1.將jar 包上傳到/share/hadoop/common
scp hadoop-lzo-0.4.20.jar node02:PWD
2.修改core-site.xml 配置文件壓縮方式
<property>
<name>io.compression.codecs</name>
<value>
org.apache.hadoop.io.compress.GzipCodec,
org.apache.hadoop.io.compress.DefaultCodec,
org.apache.hadoop.io.compress.BZip2Codec,
org.apache.hadoop.io.compress.SnappyCodec,
com.hadoop.compression.lzo.LzoCodec,
com.hadoop.compression.lzo.LzopCodec
</value>
</property>
<property>
<name>io.compression.codec.lzo.class</name>
<value>com.hadoop.compression.lzo.LzoCodec</value>
</property>
scp core-site.xml node02:PWD
重啟hadoop集群
配置hive 支持lzo壓縮
將支持hadoop-lzo-0.4.20.jar這個(gè)jar包拷貝到hive的/lib包下面即可。
使用lzo壓縮方式支持?jǐn)?shù)據(jù)txt文本文件數(shù)據(jù)進(jìn)行壓縮遏乔,節(jié)約磁盤空間寿弱。
#創(chuàng)建分區(qū)表并顯示支持壓縮格式
CREATE TABLE ods_user_login(
plat_id string comment '平臺(tái)id',
server_id int comment '區(qū)服id',
channel_id string comment '渠道',
user_id string comment '用戶ID',
role_id string comment '角色I(xiàn)D',
role_name string comment '角色名稱',
client_ip string comment '客戶端IP',
event_time int comment '事件時(shí)間',
op_type string comment '操作類型(1:登錄,-1登出)',
online_time int comment '在線時(shí)長(s)',
operating_system string comment '操作系統(tǒng)名稱',
operating_version string comment '操作系統(tǒng)版本',
device_brand string comment '設(shè)備型號(hào)',
device_type string comment '設(shè)備品牌'
)
comment '游戲登錄登出'
PARTITIONED BY(part_date date)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS INPUTFORMAT
'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';
創(chuàng)建臨時(shí)表,用于load data local inpath 臨時(shí)表中
CREATE TABLE tmp_ods_user_login(
plat_id string comment '平臺(tái)id',
server_id int comment '區(qū)服id',
channel_id string comment '渠道',
user_id string comment '用戶ID',
role_id string comment '角色I(xiàn)D',
role_name string comment '角色名稱',
client_ip string comment '客戶端IP',
event_time int comment '事件時(shí)間',
op_type string comment '操作類型(1:登錄,-1登出)',
online_time int comment '在線時(shí)長(s)',
operating_system string comment '操作系統(tǒng)名稱',
operating_version string comment '操作系統(tǒng)版本',
device_brand string comment '設(shè)備型號(hào)',
device_type string comment '設(shè)備品牌'
)
comment '游戲登錄登出-臨時(shí)表按灶,用于將數(shù)據(jù)通過動(dòng)態(tài)分區(qū)載入ods_user_login中'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
3.將temp臨時(shí)表數(shù)據(jù)insert into 正式表中,之前set 屬性讓支持?jǐn)?shù)據(jù)壓縮筐咧。
將數(shù)據(jù)通過動(dòng)態(tài)分區(qū)載入ods_user_login中
set hive.exec.dynamic.partition=true; 【開啟動(dòng)態(tài)分區(qū)】
set hive.exec.dynamic.partition.mode=nostrict; 【動(dòng)態(tài)分區(qū)模式為非嚴(yán)格模式】
set hive.exec.max.dynamic.partitions.pernode=1000; 【最大動(dòng)態(tài)分區(qū)數(shù)量設(shè)置為1000】
# 設(shè)置輸出數(shù)據(jù)格式壓縮成為LZO
set hive.exec.compress.output=true;
set mapreduce.output.fileoutputformat.compress=true;
set mapred.output.compression.codec=com.hadoop.compression.lzo.LzopCodec;
#插入數(shù)據(jù)到目標(biāo)表里面去
insert overwrite table ods_user_login partition(part_date)
select plat_id,server_id,channel_id,user_id,role_id,role_name,client_ip,event_time,op_type,online_time,operating_system,operating_version,device_brand,device_type,from_unixtime(event_time,'yyyy-MM-dd') as part_date from tmp_ods_user_login;
#給lzo文件建立索引:便于以后多個(gè)mapTask來對(duì)文件進(jìn)行處理
hadoop jar /kkb/install/hadoop-2.6.0-cdh5.14.2/share/hadoop/common/hadoop-lzo-0.4.20.jar com.hadoop.compression.lzo.DistributedLzoIndexer /user/hive/warehouse/game_center.db/ods_user_login/
4.問題:注意鸯旁,給lzo的文件建立了index索引之后,查詢tmp_ods_user_login與ods_user_login表會(huì)發(fā)現(xiàn)量蕊,這兩個(gè)表當(dāng)中的數(shù)據(jù)總量count(1)不一樣铺罢,因?yàn)閷ndex索引文件也計(jì)算到總文件數(shù)當(dāng)中去了,解決方法
1:刪除index文件即可
2:設(shè)置屬性
set hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
select count(1) from ods_user_login 表中只計(jì)算了沒有l(wèi)zo索引的數(shù)據(jù)残炮,只包含真實(shí)數(shù)據(jù)記錄數(shù)