Open In App

Hive Primitive Datatypes

Improve
Improve
Like Article
Like
Save
Share
Report

Hive is a data warehousing tool that was built on top of Hadoop. Hive acts as an interface for the Hadoop ecosystem. It is a framework that is used to store the data using HDFS(Hadoop distributed File system) and process the data using Map Reduce. So, the data present in Hadoop is taken by hive and performs analytical activities. In this, we will discuss the hive primitive data types in detail with the help of an example for better understanding. Also, we will cover need of hive data types and then emphasize on primitive data types such as Numeric data types, Date time data types, complex data types, etc. Let’s discuss it one by one. 

Need of Hive Data types :
To perform analytical activities needs some data types and data formats to process and retrieve the data.

  • By using Hive we can perform Data analysis.
  • Hive Provides HQL (Hive Query Language) which is similar to SQL.
  • We can create Hive Tables by defining table columns with a data type.

Classification of data types :
Hive data types can be classified into two parts.

Primitive Data Types :
Primitive Data Types also divide into 3 types which are as follows.

Type-1 :
Numeric Data Type –
These data types are used to define the columns with integer variables.

Data type Size
TINYINT –  1 byte signed integer -128 to 127
SMALLINT – ‘2 byte signed integer -32, 768 to 32, 767
INT  – 4 byte signed integer –2,147,483,648 to 2,147,483,647 
BIGINT  – 8 byte signed integer’ 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
FLOAT – ‘Single precision floating point Single Precision
DOUBLE – Double precision floating point Double Precision
DECIMAL  – Precise decimal type based on Java BigDecimal Object Big Decimal

Example query –

TINYINT Demo :
In this hive data type, range is from -128 to 127. Let’s understand with the help of an example.

create table geeksportal.geekdata(college_id tinyint);

SMALLINT Demo :
In this hive data type, size is 2 byte and range is from -32768 to 32767. Let’s understand with the help of an example.

create table geeksportal.geekdata(college_id smallint);

INT Demo :
In this hive data type, size is 4 byte and range is from –2,147,483,648 to 2,147,483,647. Let’s understand with the help of an example.

create table geeksportal.geekdata(college_id  int);

BIGINT Demo :
In this hive data type, size is 8 byte and range is from 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Let’s understand with the help of an example.

create table geeksportal.geekdata(phonenumber  bigint);

FLOAT Demo :
In this hive data type, size is Single Precision floating point and range is Single Precision. Let’s understand with the help of an example.

create table geeksportal.geekdata(percentage float);

DOUBLE Demo :
In this hive data type, size is Double precision floating point and range is Double precision. Let’s understand with the help of an example.

create table geeksportal.geekdata( percentage double );

DECIMAL Demo :
In this hive data type, size is  Precise decimal type based on Java BigDecimal Object and range is Big Decimal. Let’s understand with the help of an example.

create table geeksportal.geekdata( percentage decimal);

Type-2 :
Date/Time Data Type –
Here, we will discuss the Date/Time data  types in Hive as follows.

TIMESTAMP - 'YYYY-MM-DD HH:MM:SS.fffffffff   = 9 decimal place precision

TIMESTAMP Demo :
In this hive data type, you will see the demonstration of TIMESTAMP data type as follows.

create table geeksportal.geekdata( time timestamp);

String Data Type :
In this hive data type, you will understand Character String data type data type with the help of an example as follows.

create table geeksportal.geekdata( name string);

Type-3 :
Complex data types –
Here, we will discuss the Complex data types in Hive as follows.

Array :
An array is a  collection of fields all of the same data type indexed by an integer.

Syntax :

ARRAY<TINYINT>

MAP :
The map is a  Collection of Key, Value Pairs where the Key is a Primitive Type and the Value can be anything. The chosen data types for the keys and values must remain the same per map.

Syntax :

MAP<STRING,INT>

STRUCT :
It is a nested complex data structure.

Syntax :

STRUCT<first : SMALLINT, second : FLOAT, third : STRING>

UNION :
It is a Complex Data Type that can hold One of its Possible Data Types at Once.

Syntax :

UNIONTYPE<INT,FLOAT,STRING>

Example :
To demonstrate the data types with the help of an example for better understanding. Consider a text file (Geek.txt)  that includes subject and marks records as follows.

7058,cse^1,1|2|3,A|50000,3,true
7059,cse^2,1|2,B|40000,good,true

Code/Query :

Creating a table t1:
create table t1(id int,class map<string,int>,sections array<int>,hostel
struct<grade:string,fee:double>,rating uniontype<int,string>,exist boolean)
row format delimited
fields terminated by ','
collection items terminated by '|'
map keys terminated by '^'
lines terminated by '\n'
stored as textfile; 

Last Updated : 26 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads