Hive for Beginners (3): Hive Data Types

Hive's atomic data types support implicit conversion, similar to Java's type conversion. If an expression uses INT, for example, a TINYINT is automatically converted to INT — but Hive never converts in the reverse direction.

Tutorial index: Big Data for Beginners: tutorial series

Primitive Types

Hive typeJava typeLengthExample
TINYINTbyte1-byte signed integer20
SMALINTshort2-byte signed integer20
INTint4-byte signed integer20
BIGINTlong8-byte signed integer20
BOOLEANbooleanBoolean type, true or falseTRUE FALSE
FLOATfloatSingle-precision floating point3.14159
DOUBLEdoubleDouble-precision floating point3.14159
STRINGstringCharacter sequence. Character set can be specified. Single or double quotes both work.‘now is the time’ “for all good men”
TIMESTAMPTime type
BINARYByte array

Collection Types

TypeSyntax and description
STRUCTSimilar to struct in C — elements are accessed with dot notation. For example, if a column’s type is STRUCT{first STRING, last STRING}, the first element is referenced as field.first.
MAPA set of key-value tuples accessed with array notation. For example, a column of type MAP holding 'first'->'John' and 'last'->'Doe' lets you read the last element via field['last'].
ARRAYA collection of variables sharing the same type and name. These variables are the array’s elements, numbered starting from zero. For an array value of ['John', 'Doe'], the second element is referenced as arrayName[1].

Type Conversion

Hive’s atomic types can be converted implicitly, similar to Java. If an expression uses INT, a TINYINT is automatically promoted to INT, but Hive will not convert in reverse: if an expression uses TINYINT, an INT will not be narrowed to TINYINT — it returns an error unless you use a CAST.

Implicit Conversion Rules

  1. Any integer type can be implicitly converted to a wider-range type: TINYINT to INT, INT to BIGINT.
  2. All integer types, FLOAT, and STRING can be implicitly converted to DOUBLE.
  3. TINYINT, SMALLINT and INT can all be converted to FLOAT.
  4. BOOLEAN cannot be converted to any other type.

Explicit Conversion with CAST

For example, CAST('1' AS INT) converts the string '1' into the integer 1. If the cast fails — say CAST('X' AS INT) — the expression returns NULL.