Hive中ORC和TEXTFILE插入數據的方式


最近在工作的時候踩坑了,在Hive進行數據加工的時候按照同事寫好的建表語句來造數往里插數據。

同事在建表的時候使用的stored AS orc;一般在工程中都是使用這種方式,有高壓縮比其性能更好。

在本次需求中需要保留一部分數據來進行測試(這個項目是很久之前做的了,之前讓優化,優化完畢之后讓自己造數)。

因為添加了幾個字段,造數的時候需要在結果表中也增加幾個字段,使用的是txt文件,這就導致會報錯Wrong file format. Please check the file's format。

之前還困惑以為是添加字段時候沒有搞好或者怎么樣,后來反應過來看了下源碼其表的建表使用的是orc。在操作的時候將orc改為textfile(stored AS textfile)就可以正常通過了,虛驚一場。

下面是textfile和ORC的區別:

引用自其它博客:

Hive文件存儲格式(TEXTFILE 、ORC、PARQUET三者的對比)
綜述:
HIve的文件存儲格式有四種:TEXTFILE 、SEQUENCEFILE、ORC、PARQUET,前面兩種是行式存儲,后面兩種是列式存儲;所謂的存儲格式就是在Hive建表的時候指定的將表中的數據按照什么樣子的存儲方式,如果指定了A方式,那么在向表中插入數據的時候,將會使用該方式向HDFS中添加相應的數據類型。

如果為textfile的文件格式,直接load就OK,不需要走MapReduce;如果是其他的類型就需要走MapReduce了,因為其他的類型都涉及到了文件的壓縮,這需要借助MapReduce的壓縮方式來實現。

總結:
比對三種主流的文件存儲格式TEXTFILE 、ORC、PARQUET
壓縮比:ORC >  Parquet >  textFile(textfile沒有進行壓縮)
查詢速度:三者幾乎一致
案例證明:
1,textfile,創建表,存儲數據格式為TEXTFILE
create table log_text (
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
) row format delimited fields terminated by '\t'
stored as textfile ; 
 
向表中加載數據 
load data local inpath '/opt/module/datas/log.data' into table log_text ;
 
查看表中數據大小 
這個過程不會走MapReduce,而是直接將文件上傳到了HDFS,在HDFS上文件的名字還叫log.data
dfs -du -h /user/hive/warehouse/db_hive.db/log_text;
18.1 M  /user/hive/warehouse/db_hive.db/log_text/log.data
 
2,ORC,創建表,存儲數據格式為ORC
create table log_orc(
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
) row format delimited fields terminated by '\t'
stored as orc ;
 
向表中加載數據
insert into table log_orc select * from log_text ;
 
查看表中數據大小
這個過程要走MapReduce,而且文件是按照列式存儲的,還會對文件進行壓縮,Orc默認使用的壓縮方式是
zlib因此會更加節省空間,hadfs上是新的文件名,
 
hive (db_hive)> dfs -du -h /user/hive/warehouse/db_hive.db/log_orc;
2.8 M  /user/hive/warehouse/db_hive.db/log_orc/000000_0
 
3,Parquet,創建表,存儲數據格式為parquet
create table log_parquet(
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
) row format delimited fields terminated by '\t'
stored as parquet ; 
 
向表中加載數據
insert into table log_parquet select * from log_text ;
 
查看表中數據大小這個過程要走MapReduce,而且文件是按照列式存儲的,因此會更加節省空間,
hadfs上是新的文件名,
hive (db_hive)> dfs -du -h /user/hive/warehouse/db_hive.db/log_parquet;
13.1 M  /user/hive/warehouse/db_hive.db/log_parquet/000000_0
 
存儲文件的壓縮比總結:
ORC >  Parquet >  textFile
 
 
select count(*) from log_text; 
select count(*) from log_orc; 
select count(*) from log_parquet;
 
存儲文件的查詢速度總結:查詢速度相近。
壓縮和存儲的結合:
在建表的時候,如果我們指定了列式存儲的方式,他會默認使用對於的壓縮方式將我們的數據進行壓縮,與此同時我們能夠自己定制在文件存儲的時候使用什么樣子的壓縮方式,例子如下:

1.創建一個非壓縮的的ORC存儲方式
create table log_orc_none(
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
)
row format delimited fields terminated by '\t'
stored as orc tblproperties ("orc.compress"="NONE");
插入數據
hive (default)> insert into table log_orc_none select * from log_text ;
 
2.創建一個SNAPPY壓縮的ORC存儲方式
create table log_orc_snappy(
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
)
row format delimited fields terminated by '\t'
stored as orc tblproperties ("orc.compress"="SNAPPY");
插入數據
hive (default)> insert into table log_orc_snappy select * from log_text ;
 
3.創建一個默認壓縮的ORC存儲方式
create table log_orc(
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
)
row format delimited fields terminated by '\t'
stored as orc ;
 
向表中加載數據
insert into table log_orc select * from log_text ;
 
對比三者的壓縮比:
 
hive (db_hive)> dfs -du -h /user/hive/warehouse/db_hive.db/log_orc_none;
18.1 M  /user/hive/warehouse/db_hive.db/log_orc_none/log.data
 
hive (db_hive)> dfs -du -h /user/hive/warehouse/db_hive.db/log_orc_snappy;
3.8 M  /user/hive/warehouse/db_hive.db/log_orc_snappy/000000_0
 
hive (db_hive)> dfs -du -h /user/hive/warehouse/db_hive.db/log_orc;
2.8 M  /user/hive/warehouse/db_hive.db/log_orc/000000_0
 
總結:
沒有壓縮的orc格式相當於textfile,默認的壓縮格式壓縮比最大,snappy對數據進行了壓縮
orc存儲文件默認采用ZLIB壓縮,ZLIB采用的是deflate壓縮算法。因此比snappy壓縮的小。
文件沒有壓縮的話,HDFS上顯示的是原來的文件名,如果壓縮的話,使用類似於000000_0的文件名
總結:
比對三種主流的文件存儲格式TEXTFILE 、ORC、PARQUET
 
壓縮比:ORC >  Parquet >  textFile(textfile沒有進行壓縮)
 
查詢速度:三者幾乎一致
 
HDFS上顯示的是原來的文件名,如果壓縮的話,使用類似於000000_0的文件名
 
————————————————
版權聲明:本文為CSDN博主「夜古誠」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Jerry_991/article/details/88924321


免責聲明!

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



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