1. 程式人生 > >hive使用技巧:把很多小檔案匯入一張表中、顯示在檔案中位置和行數等。

hive使用技巧:把很多小檔案匯入一張表中、顯示在檔案中位置和行數等。

1.使用MSCK命令匯入輸入到hive表

我們有時候會遇到很多小檔案需要匯入到一張hive表裡面,但是一個個匯入非常麻煩。

假設建立一個外部表,這個表在hdfs的order資料夾裡,但是這個資料夾現在是空的。所以用select * 是沒有資料的。

CREATE EXTERNAL TABLE order(
    order STRING
  , time  STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
LOCATION '/hive/order';

我們通過hdfs dfs -put 方式把資料匯入到hdfs的order 資料夾下。

然後在hive裡面輸入命令 msck repair table order;

現在再select * 就有資料了。通過這種方式,可以很快匯入資料到表格裡面,而不用一個個alter ...add partition來匯入。

2.使用describe formatted order_creates;可以查看錶具體資訊,包括位置,分隔符等。formatted不寫就看簡單點的。

3.關閉動態分割槽模式

 如果insert語句報錯的時候Dynamic partition strict mode requires at least one static partition column. To turn this off set hive.exec.dynamic.partition.mode=nonstrict

可以先進行如下設定在操作。set hive.exec.dynamic.partition.mode=nonstrict;

有關動態分割槽表模式


    hive.exec.dynamic.partition=false  #預設不允許動態分割槽表
    hive.exec.dynamic.partition.mode=strict #設定動態分割槽模式
    hive.exec.max.dynamic.partitions.pernode=100  #動態分割槽在每個map、reducer裡面建立數量
    hive.exec.max.dynamic.partitions=1000         #動態分割槽被建立總數
    hive.exec.max.created.files=100000 #所有mapper建立最大HDFS檔案數           
    hive.error.on.empty.partition=false  

4.檢視每行在檔案中的位置和行數

select INPUT__FILE__NAME,col1,col2, round(BLOCK__OFFSET__INSIDE__FILE / (length(col1) + length(col2) + 2) + 1) from  tablename; 

這裡的BLOCK_OFFSET_FILE表示在檔案中的位置,除以兩個列寬度加2(行首和行尾的\t鍵),最後加1表示第0行顯示成1.

5.修改檔案格式

ALTER TABLE order PARTITION (col1='2014-06') SET SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe';
ALTER TABLE order PARTITION (col1='2014-06') SET FILEFORMAT textfile;
ALTER TABLE order PARTITION (clo1='2014-07') SET FILEFORMAT INPUTFORMAT 'parquet.hive.DeprecatedParquetInputFormat' OUTPUTFORMAT 'parquet.hive.DeprecatedParquetOutputFormat';