Managing Databases and Tables in Hive CLI
Managing Databases
In addition to creating databases, Hive CLI also provides commands to manage existing databases. Here are some common database management operations:
Dropping a Database
To drop a database, you can use the DROP DATABASE
statement:
DROP DATABASE [IF EXISTS] database_name [CASCADE];
The CASCADE
option will also drop all the tables within the database.
Altering a Database
You can modify the properties of an existing database using the ALTER DATABASE
statement:
ALTER DATABASE database_name SET DBPROPERTIES (property_name=property_value, ...);
This allows you to update the custom properties of the database.
Managing Tables
Hive CLI also provides commands to create, manage, and interact with tables within a database.
Creating a Table
To create a new table, you can use the CREATE TABLE
statement:
CREATE TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]
[TBLPROPERTIES (property_name=property_value, ...)];
This statement allows you to define the table structure, including column names, data types, and other table-level properties.
Dropping a Table
To drop an existing table, you can use the DROP TABLE
statement:
DROP TABLE [IF EXISTS] table_name;
Altering a Table
Hive CLI also supports table-level alterations, such as adding/dropping columns, changing column data types, and modifying table properties:
ALTER TABLE table_name
ADD COLUMNS (col_name data_type [COMMENT col_comment], ...)
DROP COLUMNS (col_name, ...)
CHANGE COLUMN col_old_name col_new_name column_type
SET TBLPROPERTIES (property_name=property_value, ...)
By understanding how to manage databases and tables in Hive CLI, you can effectively organize and manipulate your data within the Hadoop ecosystem.