creating partition in external table in hive

后端 未结 4 1275

I have successfully created and added Dynamic partitions in an Internal table in hive. i.e. by using following steps:

1-created a sourc

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-03 11:56

    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.

提交回复
热议问题