creating partition in external table in hive

删除回忆录丶 提交于 2019-12-03 00:23:19
pradeep

1. Set below property

set hive.exec.dynamic.partition=true

set hive.exec.dynamic.partition.mode=nonstrict

2. Create External partitioned table

create external table1 ( name string, age int, height int) location 'path/to/dataFile/in/HDFS';

3. Insert data to partitioned table from source table.

Basically , the process is same. its just that you create external partitioned table and provide HDFS path to table under which it will create and store partition.

Hope this helps.

Yes, you have to tell Hive explicitly what is your partition field.

Consider you have a following HDFS directory on which you want to create a external table.

/path/to/dataFile/

Let's say this directory already have data stored(partitioned) department wise as follows:

/path/to/dataFile/dept1
/path/to/dataFile/dept2
/path/to/dataFile/dept3

Each of these directories have bunch of files where each file contains actual comma separated data for fields say name,age,height.

e.g.
    /path/to/dataFile/dept1/file1.txt
    /path/to/dataFile/dept1/file2.txt

Now let's create external table on this:

Step 1. Create external table:

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/';

Step 2. Add partitions:

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';

Done, run select query once to verify if data loaded successfully.

Follow the below steps:

  1. Create a temporary table/Source table

    create table source_table(name string,age int,height int) row format delimited by ',';
    

    Use your delimiter as in the file instead of ',';

  2. Load data into the source table

    load data local inpath 'path/to/dataFile/in/HDFS';
    
  3. Create external table with partition

    create external table external_dynamic_partitions(name string,height int) 
    partitioned by (age int) 
    location 'path/to/dataFile/in/HDFS';
    
  4. Enable dynamic partition mode to nonstrict

    set hive.exec.dynamic.partition.mode=nonstrict
    
  5. Load data to external table with partitions from source file

    insert into table external_dynamic partition(age) 
    select * from source_table;
    

That's it. You can check the partitions information using

show partitions external_dynamic;

You can even check if it is an external table or not using

describe formatted external_dynamic;

External table is a type of table in Hive where the data is not moved to the hive warehouse. That means even if U delete the table, the data still persists and you will always get the latest data, which is not the case with Managed table.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!