Hive Data Types: How to Create and Drop Databases in Hive

Data types in Hive

Data types are very important elements in Hive query language and data modeling. For defining the table column types, we must have to know about the data types and its usage.

The following gives brief overview of some data types present in Hive:

  • Numeric Types
  • String Types
  • Date/Time Types
  • Complex Types

Hive Numeric Data Types

Type Memory allocation
TINY INT Its 1-byte signed integer (-128 to 127)
SMALL INT 2-byte signed integer (-32768 to 32767)
INT 4 –byte signed integer ( -2,147,484,648 to 2,147,484,647)
BIG INT 8 byte signed integer
FLOAT 4 – byte single precision floating point number
DOUBLE 8- byte double precision floating point number
DECIMAL We can define precision and scale in this Type

Hive String Data Types

Type Length
CHAR 255
VARCHAR 1 to 65355
STRING We can define length here(No Limit)

Hive Date/Time Data Types

Type Usage
Timestamp Supports traditional Unix timestamp with optional nanosecond precision
Date
  • It’s in YYYY-MM-DD format.
  • The range of values supported for the Date type is be 0000-01-01 to 9999-12-31, dependent onsupport by the primitive Java Date type

Hive Complex Data Types

Type Usage
Arrays ARRAY<data_type>
Negative values and non-constant expressions not allowed
Maps MAP<primitive_type, data_type>
Negative values and non-constant expressions not allowed
Structs STRUCT<col_name :datat_type, ….. >
Union UNIONTYPE<data_type, datat_type, ……>

How to Create and Drop Databases in Hive

Following are the steps on how to create and drop databases in Hive.

Step 1: Create Database in Hive

For creating a database in Hive shell, we have to use the command as shown in the syntax below:-

Syntax:

Create database <DatabaseName>

Example: -Create database “guru99”

Create Database in Hive

From the above screen shot, we are doing two things

  • Creating database “guru99” in Hive
  • Displaying existing databases using “show” command
  • In the same screen, Database “guru99” name is displayed at the end when we execute the show command. Which means Database “guru99” is successfully created.

Step 2: Drop Database in Hive

For Dropping database in Hive shell, we have to use the “drop” command as shown in the syntax below:-

Syntax:

Drop database <DatabaseName>

Example: -Drop database guru99

Drop Database in Hive

In the above screenshot, we are doing two things

  • We are dropping database ‘guru99’ from Hive
  • Cross checking the same with “show” command
  • In the same screen, after checking databases with show command, database”guru99″ does not appear inside Hive.
  • So we can confirm now that database “guru99” is dropped