[英]creating partition in external table in hive
我已經成功地在 hive 的內部表中創建並添加了動態分區。 即通過使用以下步驟:
1-創建了一個源表
2-從本地加載數據到源表
3- 創建另一個帶有分區的表 - partition_table
4- 將數據從源表插入到此表中,從而動態創建所有分區
我的問題是,如何在外部表中執行此操作? 我閱讀了很多關於此的文章,但我很困惑,我是否必須指定現有分區的路徑才能為外部表創建分區?
示例: 步驟 1:
create external table1 ( name string, age int, height int)
location 'path/to/dataFile/in/HDFS';
第2步:
alter table table1 add partition(age)
location 'path/to/already/existing/partition'
我不確定如何在外部表中進行分區。 有人可以通過逐步描述相同的內容來提供幫助嗎?
提前致謝!
是的,您必須明確告訴 Hive 您的分區字段是什么。
假設您有以下 HDFS 目錄,要在其上創建外部表。
/path/to/dataFile/
假設這個目錄已經有數據存儲(分區)部門明智如下:
/path/to/dataFile/dept1
/path/to/dataFile/dept2
/path/to/dataFile/dept3
這些目錄中的每一個都有一堆文件,其中每個文件都包含實際逗號分隔的字段數據,例如名稱、年齡、高度。
e.g.
/path/to/dataFile/dept1/file1.txt
/path/to/dataFile/dept1/file2.txt
現在讓我們在此創建外部表:
步驟 1. 創建外部表:
CREATE EXTERNAL TABLE testdb.table1(name string, age int, height int)
PARTITIONED BY (dept string)
ROW FORMAT DELIMITED
STORED AS TEXTFILE
LOCATION '/path/to/dataFile/';
步驟 2. 添加分區:
ALTER TABLE testdb.table1 ADD PARTITION (dept='dept1') LOCATION '/path/to/dataFile/dept1';
ALTER TABLE testdb.table1 ADD PARTITION (dept='dept2') LOCATION '/path/to/dataFile/dept2';
ALTER TABLE testdb.table1 ADD PARTITION (dept='dept3') LOCATION '/path/to/dataFile/dept3';
完成,運行一次選擇查詢以驗證數據是否成功加載。
1.設置以下屬性
set hive.exec.dynamic.partition=true
set hive.exec.dynamic.partition.mode=nonstrict
2. 創建外部分區表
create external table1 ( name string, age int, height int) location 'path/to/dataFile/in/HDFS';
3. 從源表向分區表插入數據。
基本上,過程是一樣的。 它只是您創建外部分區表並提供表的 HDFS 路徑,它將在該路徑下創建和存儲分區。
希望這可以幫助。
正確的做法。
創建表並提及它已分區。
創建由 (age int) 分區的外部 table1 ( name string, age int, height int) 存儲為 ****(您的格式) 位置 'path/to/dataFile/in/HDFS';
現在您必須刷新 hive Metastore 中的分區。
msck修復表table1
這將負責將所有分區加載到 hive 元存儲中。
您可以在過程中的任何時候使用 msck 修復表來更新 Metastore。
請按照以下步驟操作:
創建臨時表/源表
create table source_table(name string,age int,height int) row format delimited by ',';
使用文件中的分隔符而不是 ',';
加載數據到源表
load data local inpath 'path/to/dataFile/in/HDFS';
創建帶分區的外部表
create external table external_dynamic_partitions(name string,height int) partitioned by (age int) location 'path/to/dataFile/in/HDFS';
啟用動態分區模式為非嚴格
set hive.exec.dynamic.partition.mode=nonstrict
使用源文件中的分區將數據加載到外部表
insert into table external_dynamic partition(age) select * from source_table;
就是這樣。 您可以使用檢查分區信息
show partitions external_dynamic;
您甚至可以檢查它是否是外部表或未使用
describe formatted external_dynamic;
外部表是 Hive 中的一種表,其中數據不會移動到 Hive 倉庫。 這意味着即使您刪除了表,數據仍然存在並且您將始終獲得最新的數據,而托管表則不是這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.