一孟抗、日志行為數(shù)據(jù)
1谍肤、日志數(shù)據(jù)在flume中指定了數(shù)據(jù)放在hdfs上的壓縮格式
a1.sinks.k1.hdfs.fileType = CompressedStream
a1.sinks.k1.hdfs.codeC = lzop
2然痊、創(chuàng)建lzo表,導(dǎo)入數(shù)據(jù)
CREATE EXTERNAL TABLE ods_log (`line` string)
PARTITIONED BY (`dt` string) -- 按照時(shí)間創(chuàng)建分區(qū)
STORED AS -- 指定存儲(chǔ)方式农尖,讀數(shù)據(jù)采用LzoTextInputFormat其垄;
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION '/warehouse/gmall/ods/ods_log' -- 指定數(shù)據(jù)在hdfs上的存儲(chǔ)位置
;
load data inpath '/origin_data/gmall/log/topic_log/2020-08-20'
into table ods_log partition(dt='2020-08-20')--導(dǎo)入數(shù)據(jù);
3、把數(shù)據(jù)從hdfs遷移到hive外部表分區(qū)中/warehouse/ods/ods_log/dt=$do_date
在遷移過(guò)程中指定了運(yùn)行隊(duì)列hive卤橄,另外添加lzo索引绿满,為后續(xù)讀取時(shí)切分使用坷牛。
$hadoop jar /opt/module/hadoop-3.1.3/share/hadoop/common/hadoop-lzo-0.4.20.jar
com.hadoop.compression.lzo.DistributedLzoIndexer
-Dmapreduce.job.queuename=hive
/warehouse/gmall/ods/ods_log/dt=$do_date
4侦锯、dwd如何使用lzo壓縮并帶有l(wèi)zo索引的表數(shù)據(jù)
(1)兩種方式录粱,分別查詢數(shù)據(jù)有多少行
hive (gmall)> select * from ods_log;2955 row(s)
hive (gmall)> select count(*) from ods_log;2959 row(s)
(2)兩次查詢結(jié)果不一致欲诺。
原因是select * from ods_log這種查詢方式是不執(zhí)行MR操作的展父,默認(rèn)采用的是ods_log建表語(yǔ)句中指定的DeprecatedLzoTextInputFormat吟温,能夠識(shí)別lzo.index為索引文件荆针。
select count(*) from ods_log這種查詢方式是執(zhí)行MR操作的碑宴,而hive默認(rèn)采用的是CombineHiveInputFormat殖属,不能識(shí)別lzo.index為索引文件姐叁,將索引文件當(dāng)做普通文件處理。更嚴(yán)重的是洗显,這會(huì)導(dǎo)致LZO文件無(wú)法切片外潜。
hive (gmall)> set hive.input.format;
hive.input.format=org.apache.hadoop.hive.ql.io.CombineHiveInputFormat
解決辦法:修改CombineHiveInputFormat為HiveInputFormat
(3)再次測(cè)試
hive (gmall)>
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
hive (gmall)> select * from ods_log;
Time taken: 0.706 seconds, Fetched: 2955 row(s)
hive (gmall)> select count(*) from ods_log;2955 row(s)
dwd層建表后采用下面parquet存儲(chǔ)格式。
PARTITIONED BY (dt string)
stored as parquet
LOCATION '/warehouse/gmall/dwd/dwd_error_log'
TBLPROPERTIES('parquet.compression'='lzo');
說(shuō)明:數(shù)據(jù)采用parquet存儲(chǔ)方式挠唆,是可以支持切片的处窥,不需要再對(duì)數(shù)據(jù)創(chuàng)建索引。
如果單純的text方式存儲(chǔ)數(shù)據(jù)玄组,需要采用支持切片的滔驾,lzop壓縮方式并創(chuàng)建索引谒麦。
二、業(yè)務(wù)操作數(shù)據(jù)
1哆致、使用sqoop導(dǎo)入hdfs時(shí)绕德,采用lzo壓縮,并建lzo索引
import_data(){
$sqoop import \
--connect jdbc:mysql://hadoop102:3306/gmall \
--username root \
--password 000000 \
--target-dir /origin_data/gmall/db/$1/$do_date \
--delete-target-dir \
--query "$2 and \$CONDITIONS" \
--num-mappers 1 \
--fields-terminated-by '\t' \
--compress \
--compression-codec lzop \
--null-string '\\N' \
--null-non-string '\\N'
hadoop jar /opt/module/hadoop-3.1.3/share/hadoop/common/hadoop-lzo-0.4.20.jar
com.hadoop.compression.lzo.DistributedLzoIndexer
/origin_data/gmall/db/$1/$do_date
2摊阀、業(yè)務(wù)數(shù)據(jù)ods層數(shù)據(jù)采用lzo壓縮,并指定數(shù)據(jù)輸入輸出格式
STORED AS -- 指定存儲(chǔ)方式迁匠,讀數(shù)據(jù)采用LzoTextInputFormat;輸出數(shù)據(jù)采用TextOutputFormat
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_order_info/' -- 指定數(shù)據(jù)在hdfs上的存儲(chǔ)位置
;
3驹溃、業(yè)務(wù)數(shù)據(jù)dwd層采用lzo壓縮,parquet存儲(chǔ)
--設(shè)置HiveInputFormat,不能使用默認(rèn)CombineHiveInputFormat
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
--存儲(chǔ)表的方式改變成parquet
stored as parquet
location '/warehouse/gmall/dwd/dwd_dim_sku_info/'
tblproperties ("parquet.compression"="lzo");
三延曙、dwd-dws-dwt采用的都是parquet存儲(chǔ)豌鹤,lzo壓縮。
四枝缔、ads層數(shù)據(jù)量比較小布疙,可以不采取壓縮方式。
使用dws/dwt層的數(shù)據(jù)時(shí)不需要設(shè)置HiveInputFormat愿卸,
采用默認(rèn)的CombineHiveInputFormat就可以識(shí)別parquet(本身支持切分)灵临,。