Hive Partition Table

Source: Internet
Author: User
Tags select from where

1. Partition table

If there is a log file, each of these records contains a timestamp. We partition him based on the date, and the same day records are stored in the same partition.

    • To avoid generating too many small files, it is recommended that you partition only discrete fields.
    • Using partitions does not affect the execution of large-scale queries, and can still be queried across partitions.
    • Partition essence: Create the partition folder again under the Data table folder

2. Sub-partitions

On the basis of the date partition, you can also sub-partition each partition according to the country to speed up the query based on the geographic location.

The essence of the sub-partition: Create a sub-partition directory under the partition directory.

Partitions are defined with the partitioned by when the table is created, and you can use the ALTER TABLE statement to add or remove partitions after the table is created.

Create Table bigint   by (dt string,country string)  by ' \ t ';

Load data, the specified partition value is displayed:

Load ' /root/hive/file2 '  into Table logs partition (dt='2001-01-01', country=' GB ');

After the more data files are loaded into the logs table, the directory structure:

Log table: Two date partitions + two national partitions. The data files are stored in the underlying directory.

/User/Hive/Warehouse/Logs/Dt= .- on- on/Country=GB/file1/file2/Country=US/File3/Dt= .- on- Geneva/Country=GB/file4/Country=US/File5/File6

Use the Show partitions logs command to get those partitions in the logs table:

Dt=2001-01-01/country=gb

Dt=2001-01-01/country=us

Dt=2001-01-02/country=gb

Dt=2001-01-02/country=us

Displays the structure information of the table:Describe logs;

TS                      bigint                                 line                    string                                      dt                      string                                      Country                 String                                # Partition Information          # Col_ Name              data_type               comment                         dt                          string                                      Country                     
    • It is important to note that the column definition in the partitioned by clause is a formal column in the table, called the "Partition column" Partition column.
    • However, the data file does not contain the values for these columns because they originate from the directory name.
    • You can use partitioned columns in the normal way in a SELECT statement. Hive trims the input so that only the relevant partitions are scanned.
 Select from where country = ' GB ';

Only file1,file2,file4 will be scanned.

    • Also note that this query returns the value of the DT partition column. This value is the hive read from the directory name .

Hive Partition Table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.