Let's continue our explanation of Hive partitioning, focusing on how partitions are added, data is loaded into them, and how they physically manifest in HDFS.
1. Adding a Partition (ALTER TABLE ... ADD PARTITION)
When you define a table as PARTITIONED BY, Hive knows the potential for partitions, but it doesn't create the physical subdirectories for them until you explicitly tell it to, or when you load data in a way that automatically creates them (though explicit addition is often recommended for better control).
ALTER TABLE part ADD PARTITION (P_BRAND='GoldenBolts');
ALTER TABLE part: This specifies that we are modifying the part table.
ADD PARTITION: This clause instructs Hive to add a new partition.
(P_BRAND='GoldenBolts'): This defines the specific partition being added. It means we are adding a partition where the value of the P_BRAND partition column is 'GoldenBolts'.
What happens behind the scenes:
Metastore Update: Hive updates its Metastore to record the existence of a new partition for part table with P_BRAND='GoldenBolts'.
HDFS Directory Creation: Hive creates the corresponding physical directory in HDFS for this partition. Based on the table's location and partition definition, this would be something like /user/hive/warehouse/part/p_brand=GoldenBolts/. At this point, the directory is empty.
Verifying Partitions (SHOW PARTITIONS)
show partitions part;
This command is used to list all the partitions that have been registered for a given table in the Hive Metastore.
OK
p_brand=GoldenBolts
Time taken: 0.072 seconds, Fetched: 1 row(s)
The output confirms that a partition named p_brand=GoldenBolts now exists for the part table. This indicates that Hive is aware of this partition and its corresponding HDFS path has been created.
To inspect the current partition structure of a table, ensuring that partitions were added correctly and to see what data segments are available for querying.
2. Loading Data into a Partition (LOAD DATA LOCAL INPATH ... INTO TABLE ... PARTITION)
Once a partition is defined (either explicitly or implicitly), you can load data specifically into that partition. This ensures that the data is placed in the correct HDFS subdirectory, allowing for efficient partition pruning during queries.
LOAD DATA LOCAL INPATH '/local/home/janusz/HIVE-EXAMPLES/TPCHR/part.txt' OVERWRITE INTO TABLE part PARTITION (P_BRAND='GoldenBolts');
This command loads data from a local file directly into the previously created partition of the part table.
LOAD DATA LOCAL INPATH '/local/home/janusz/HIVE-EXAMPLES/TPCHR/part.txt':
LOCAL INPATH: Specifies that the source file is part.txt and it's located on the local file system at the given absolute path.
Crucial assumption: The part.txt file is expected to contain data only for the P_BRAND='GoldenBolts' partition, and not include the P_BRAND column itself within its data rows, as P_BRAND is a partition column. The schema of part.txt should match P_PARTKEY, P_NAME, P_TYPE, P_SIZE, P_COMMENT.
OVERWRITE: This is an important keyword.
If the partition P_BRAND='GoldenBolts' already contains data, OVERWRITE will delete all existing data files within that specific partition's HDFS directory before copying the new part.txt data.
If OVERWRITE is omitted, LOAD DATA will append the new data to the existing data within the partition.
INTO TABLE part: Specifies the target table is part.
PARTITION (P_BRAND='GoldenBolts'): This is the key part for loading into a partition. It explicitly tells Hive which partition (identified by P_BRAND='GoldenBolts') the data from part.txt should be loaded into. Hive will place the data into the HDFS directory associated with this specific partition.