A Comprehensive Guide to Apache Hive DDL Commands

Introduction

Apache Hive is a popular data warehousing tool built on top of the Apache Hadoop framework. Hive provides an SQL-like query language called HiveQL for querying and analyzing large datasets stored in Hadoop. One of the key aspects of working with Hive is understanding how to use DDL (Data Definition Language) commands to create, modify, and delete databases, tables, and other Hive objects.

In this article, we‘ll take an in-depth look at the most important Hive DDL commands, including syntax, examples, best practices. Whether you‘re new to Hive or an experienced user, this guide will help you master Hive DDL and use it effectively in your big data projects. Let‘s get started!

What are Hive DDL Commands?

DDL stands for Data Definition Language. Just like in traditional relational databases, DDL commands in Hive are used to define, modify, and delete the structure of databases and tables. This includes creating new databases/tables, dropping existing ones, altering table schemas, and more.

Hive DDL commands can be categorized into two main types:

  1. Database DDL commands – Used for creating, altering, and dropping databases in Hive
  2. Table DDL commands – Used for creating, altering, and dropping tables within a Hive database

It‘s important to understand the syntax and usage of these key DDL operations as they form the foundation for working with data in Hive.

Database DDL Commands

Let‘s start by looking at the DDL commands for working with Hive databases:

1. Create Database

The CREATE DATABASE command is used to create a new Hive database. Here is the basic syntax:

CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
  [COMMENT database_comment]
  [LOCATION hdfs_path]
  [WITH DBPROPERTIES (property_name=property_value, ...)];
  • DATABASE and SCHEMA are synonymous and can be used interchangeably
  • IF NOT EXISTS is an optional clause. When included, Hive will silently ignore the command if a database with the same name already exists.
  • COMMENT allows you to add a comment describing the database
  • LOCATION lets you specify a custom directory in HDFS where the database files will be stored. If omitted, Hive will use a default location.
  • WITH DBPROPERTIES is used to set key-value property pairs for the database.

Here‘s an example showing how to create a new database called my_db with a comment and custom location:

CREATE DATABASE IF NOT EXISTS my_db 
  COMMENT ‘My custom database‘
  LOCATION ‘/user/hive/warehouse/my_db‘
  WITH DBPROPERTIES (‘created_by‘=‘Joe Smith‘, ‘created_on‘=‘2023-04-24‘);  

2. Drop Database

The DROP DATABASE command deletes an existing Hive database and all objects (tables, views, etc.) within it. The basic syntax is:

DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE];
  • IF EXISTS silently ignores the command if the specified database doesn‘t exist
  • RESTRICT is the default behavior and will only drop an empty database. If the database contains any tables/objects, the command will fail.
  • CASCADE drops the database along with all tables and objects

For example, to drop a database named my_old_db and all of its tables:

DROP DATABASE IF EXISTS my_old_db CASCADE;

Use caution with CASCADE mode as the data will be permanently deleted!

3. Alter Database

The ALTER DATABASE command changes the properties or location of a Hive database.

ALTER (DATABASE|SCHEMA) database_name 
  SET LOCATION hdfs_path;
ALTER (DATABASE|SCHEMA) database_name 
  SET DBPROPERTIES (property_name=property_value, ...);

Here‘s an example of changing location and setting a new property:

ALTER DATABASE my_db 
  SET LOCATION ‘/new/hdfs/location‘;
ALTER DATABASE my_db 
  SET DBPROPERTIES (‘max_size‘=‘2TB‘);  

4. Use Database

The USE command sets the current database for your Hive session. Any subsequent HiveQL commands will run against this database.

USE database_name;

For example: USE my_db;

5. Describe Database

The DESCRIBE DATABASE command shows the properties and metadata for a Hive database.

DESCRIBE (DATABASE|SCHEMA) [EXTENDED] database_name;

EXTENDED displays additional properties like owner and default location.

6. Show Databases

The SHOW DATABASES command lists all databases in your Hive environment.

SHOW (DATABASES|SCHEMAS) [LIKE regex_pattern];  

You can optionally provide a regular expression with LIKE to filter the results.

Table DDL Commands

Now let‘s dive into the DDL commands used for working with Hive tables.

1. Create Table

The CREATE TABLE command defines a new table in Hive. Here is a simplified version of the syntax:

CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name    
  [(col_name data_type [column_constraint] [COMMENT col_comment], ... ]
  [COMMENT table_comment]  
  [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
  [CLUSTERED BY (col_name, ...) INTO num_buckets BUCKETS]
  [STORED BY ‘storage.handler.class.name‘ [WITH SERDEPROPERTIES (...)]]
  [LOCATION hdfs_path]
  [TBLPROPERTIES (property_name=property_value, ...)]

There are many optional clauses, but the key components are:

  • Column definitions
  • Table comment
  • Partitioning/bucketing
  • Storage format and SerDe
  • HDFS location
  • Table properties

Here is an example:

CREATE EXTERNAL TABLE IF NOT EXISTS my_db.page_views
( 
  view_time TIMESTAMP, 
  url STRING,
  user_id BIGINT, 
  referrer STRING
)
PARTITIONED BY (view_date STRING)
STORED AS PARQUET
LOCATION ‘/data/page_views‘
TBLPROPERTIES (‘notes‘ = ‘Web traffic data‘);

This creates an external partitioned table stored in Parquet format to hold page view events.

2. Drop Table

The DROP TABLE command deletes a table and all of its data:

DROP TABLE [IF EXISTS] table_name [PURGE]; 

The optional PURGE clause completely removes data from the filesystem rather than moving it to the .Trash folder.

3. Truncate Table

The TRUNCATE TABLE command removes all rows from a table without deleting the table itself.

TRUNCATE TABLE table_name;

Be careful as this permanently deletes data and cannot be undone!

4. Alter Table

The ALTER TABLE command modifies the schema or properties of an existing table. Some common use cases:

Renaming a table:

ALTER TABLE table_name RENAME TO new_table_name;

Adding columns:

ALTER TABLE table_name ADD COLUMNS (col_name data_type,...);  

Replacing columns:

ALTER TABLE table_name REPLACE COLUMNS (col_name data_type,...);

Changing table properties:

ALTER TABLE table_name SET TBLPROPERTIES (property_name = property_value, ... );

There are many other ALTER TABLE operations for partitioning, storage formats, constraints and more. Refer to the Apache Hive documentation for full details.

5. Describe Table

The DESCRIBE TABLE command shows the schema (column names, types, comments) for a table.

DESCRIBE [EXTENDED|FORMATTED] table_name;
  • DESCRIBE table_name shows just the column metadata
  • EXTENDED adds additional metadata like table type, storage format, and SerDe
  • FORMATTED displays in a neatly formatted style

6. Show Tables

The SHOW TABLES command lists all tables in the current database.

SHOW TABLES [IN database_name] [‘regex_pattern‘];

You can specify another database with IN and filter results using a regular expression. For example:

SHOW TABLES IN my_db ‘page*‘;

This would list all tables starting with "page" in the my_db database.

Best Practices for Hive DDL

To make the most of Hive DDL, follow these tips and best practices:

  1. Use external tables for data that may be used outside of Hive. This preserves the underlying data when a table is dropped.

  2. Partition large tables on commonly filtered columns (e.g. date, region) to improve query performance.

  3. Use appropriate storage formats like ORC or Parquet to reduce I/O and storage costs.

  4. Take advantage of Hive‘s built-in SerDes for working with data in JSON, CSV, Avro, and other common formats.

  5. Use Hive‘s ACCESS TO and GRANT/REVOKE commands to secure databases and tables with role-based permissions.

  6. Take care when running destructive commands like DROP TABLE with PURGE or TRUNCATE TABLE, as the data deleted cannot be recovered.

  7. Use the SHOW and DESCRIBE commands liberally to inspect your databases and tables.

Conclusion

In this guide, we covered all of the essential Hive DDL commands for working with databases and tables, including syntax, examples, and best practices. Hive DDL forms the foundation for data engineering and analytics on the Hadoop platform by providing a powerful SQL-like interface to define and manipulate data structures at massive scale.

To learn more about Hive and Hadoop, consult the official Apache Hive and Hadoop documentation, as well as resources from leading Hadoop vendors and the open source community. Hive DDL is an essential skill for any big data practitioner.

I hope this guide has been helpful in furthering your understanding of Hive DDL. Leave a comment below with any questions or tips from your own experience working with Hive!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts