簡體   English   中英

Hive,如何按具有 null 值的列進行分區,將所有空值放在一個分區中

[英]Hive, how to partition by a colum with null values, putting all nulls in one partition

我使用的是 Hive,而 IDE 是色調。 我正在嘗試為我的分區鍵選擇不同的組合鍵。

我的原始表的定義如下:

CREATE External Table `my_hive_db`.`my_table`(
    `col_id` bigint,
    `result_section__col2` string,
    `result_section_col3` string ,
    `result_section_col4` string,
    `result_section_col5` string,
    `result_section_col6__label` string,
    `result_section_col7__label_id` bigint ,
    `result_section_text` string ,
    `result_section_unit` string,
    `result_section_col` string ,
    `result_section_title` string,
    `result_section_title_id` bigint,
    `col13` string,
    `timestamp` bigint,
    `date_day` string
    )
    PARTITIONED BY ( 
      `date_year` string, 
      `date_month` string)
    ROW FORMAT SERDE 
      'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe' 
    STORED AS INPUTFORMAT 
      'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat' 
    OUTPUTFORMAT 
      'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'
    LOCATION
      's3a://some/where/in/amazon/s3'; 

上面的代碼工作正常。 但是當我使用 date_day 作為分區鍵創建一個新表時,該表是空的,我需要運行 MSCK 修復表。 但是,我收到以下錯誤:

編譯語句時出錯:FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.ddl.DDLTask

在此處輸入圖像描述

當分區鍵為 date_year、date_month 時,MSCK 工作正常。

我收到錯誤的表的表定義如下:

CREATE External Table `my_hive_db`.`my_table`(
    `col_id` bigint,
    `result_section__col2` string,
    `result_section_col3` string ,
    `result_section_col4` string,
    `result_section_col5` string,
    `result_section_col6__label` string,
    `result_section_col7__label_id` bigint ,
    `result_section_text` string ,
    `result_section_unit` string,
    `result_section_col` string ,
    `result_section_title` string,
    `result_section_title_id` bigint,
    `col13` string,
    `timestamp` bigint,
    `date_year` string, 
    `date_month` string
  )
    PARTITIONED BY (
     `date_day` string)
    ROW FORMAT SERDE 
      'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe' 
    STORED AS INPUTFORMAT 
      'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat' 
    OUTPUTFORMAT 
      'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'
    LOCATION
      's3a://some/where/in/amazon/s3'; 

在此之后,以下查詢為空:

Select * From `my_hive_db`.`my_table` Limit 10;

因此,我運行了以下命令:

MSCK REPAIR TABLE `my_hive_db`.`my_table`;

我收到錯誤:編譯語句時出錯:FAILED:執行錯誤,從 org.apache.hadoop.Z8A4AC216FB230DATask3834DE641B3E5D0F7Z.dldl.ql 返回代碼 1。

我檢查了這個鏈接,因為這正是我得到的錯誤,但是通過使用提供的解決方案:

set hive.msck.path.validation=ignore;
MSCK REPAIR TABLE table_name;

我得到一個不同的錯誤:

處理語句時出錯:無法在運行時修改 hive.msck.path.validation。 它不在允許在運行時修改的參數列表中。

我認為我收到這些錯誤的原因是 date_day 有超過 2 億條記錄具有 null 值。

有 31 個不同的日期日期,而不是 null 值。 我想用 32 個分區對我的表進行分區,每個分區都有一個不同的 date_day 字段值,並且所有 null 值都進入不同的分區。 有沒有辦法這樣做(按具有 null 值的列分區)?

如果這可以通過spark實現,我也願意使用它。

這是通過重新創建表來更改分區鍵的更大問題的一部分,如在回答我的另一個問題的鏈接中提到的那樣

謝謝您的幫助。

您似乎不明白 Hive 的分區是如何工作的。 Hive 將數據存儲到 HDFS(或 S3,或其他一些分布式文件夾)上的文件中。 如果您創建一個名為my_schema.my_table的非分區 parquet 表,您將在分布式存儲中看到存儲在文件夾中的文件

hive/warehouse/my_schema.db/my_table/part_00001.parquet
hive/warehouse/my_schema.db/my_table/part_00002.parquet
...

如果您創建一個按列p_col分區的表,文件將如下所示

hive/warehouse/my_schema.db/my_table/p_col=value1/part_00001.parquet
hive/warehouse/my_schema.db/my_table/p_col=value1/part_00002.parquet
...
hive/warehouse/my_schema.db/my_table/p_col=value2/part_00001.parquet
hive/warehouse/my_schema.db/my_table/p_col=value2/part_00002.parquet
...

命令MSCK repair table允許您在創建外部表時自動重新加載分區。

假設您在 s3 上有如下所示的文件夾:

hive/warehouse/my_schema.db/my_table/p_col=value1/part_00001.parquet
hive/warehouse/my_schema.db/my_table/p_col=value2/part_00001.parquet
hive/warehouse/my_schema.db/my_table/p_col=value3/part_00001.parquet

您創建一個外部表

CREATE External Table my_schema.my_table(
   ... some columns ...
)
PARTITIONED BY (p_col STRING)

該表將被創建但為空,因為 Hive 尚未檢測到分區。 您運行MSCK REPAIR TABLE my_schema.my_table ,Hive 將識別出您的分區p_col與 s3 ( /p_col=value1/ ) 上的分區方案匹配。

根據我從您的其他問題中了解到的情況,您正試圖通過執行更改表的分區方案

CREATE External Table my_schema.my_table(
   ... some columns ...
)
PARTITIONED BY (p_another_col STRING)

並且您收到一條錯誤消息,因為p_another_col與 s3 中使用的列p_col不匹配。 這個錯誤是完全正常的,因為你所做的沒有意義。

另一個問題的答案所述,您需要使用不同的分區方案創建第一個表的副本。

你應該嘗試這樣的事情:

CREATE External Table my_hive_db.my_table_2(
    `col_id` bigint,
    `result_section__col2` string,
    `result_section_col3` string ,
    `result_section_col4` string,
    `result_section_col5` string,
    `result_section_col6__label` string,
    `result_section_col7__label_id` bigint ,
    `result_section_text` string ,
    `result_section_unit` string,
    `result_section_col` string ,
    `result_section_title` string,
    `result_section_title_id` bigint,
    `col13` string,
    `timestamp` bigint,
    `date_year` string, 
    `date_month` string
)
PARTITIONED BY (`date_day` string)

然后用動態分區填充你的新表

INSERT OVERWRITE TABLE my_hive_db.my_table_2 PARTITION(date_day)
SELECT 
  col_id,
  result_section__col2,
  result_section_col3,
  result_section_col4,
  result_section_col5,
  result_section_col6__label,
  result_section_col7__label_id,
  result_section_text,
  result_section_unit,
  result_section_col,
  result_section_title,
  result_section_title_id,
  col13,
  timestamp,
  date_year,
  date_month,
  date_day
FROM my_hive_db.my_table_1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM