Truncating tables

In this recipe, you will learn how to truncate a table in Hive.

Getting ready

The TRUNCATE command removes all rows from the table as well as from the partition, but keeps the table structure as it is. Truncating a table in Hive is indirectly removing the files from the HDFS as a table in Hive is just a way of reading the data from the HDFS in the table or structural format. The general format of using the Truncate table command is as follows:

TRUNCATE TABLE table_name [PARTITION partition_spec];

Where:

partition_spec:

(partition_column = partition_col_value, partition_column = partition_col_value, ...)

How to do it…

Follow these steps to truncate a table in Hive:

The preceding command truncates the table named Sales:

Truncate table ...

Get Apache Hive Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.