Hive数据类型及示例

Hive的数据类型用来说明Hive表的cloumn/field的类型,可以大致分为两类:

  • Primitive Data Types
  • Complex Data Types

Primitive Data Types可以进一步分为四类:

  • Numeric Types
  • String Types
  • Date/Time Types
  • Miscellaneous Types

这些数据类型和占用空间大小与Java/SQL primitive相似。

Numeric Data Types

整型包括tinyint、smallint、int和bigint,等价于Java的byte、short、int和long primitive types;

浮点型包括float、double和decimal,等价于Java的float、double,SQL的decimal类型。

decimal(5,2)表示一共有5位,其中2位是小数。下面的表格是所有数值类型的范围及示例:

Primitive - Numeric Data Types
Type Size Range Examples
tinyint 1 byte signed integer -128 to 127 100
smallint 2 byte signed integer -32,768 to 32,767 100,1000
int 4 byte signed integer -2,147,483,648 to 2,147,483,647 100,1000.50000
bigint 8 byte signed integer -9.2*10^18 to 9.2*10^18 100,1000*10^10
float 4 byte single precision float 1.4*e^-45 to 3.4*e^38 1500.00
double 8 byte double precision float 4.94*e^-324 to 1.79*e^308 750000.00
decimal 17 bytes precision upto 38 digits -10^38+1 to 10^38-1 decimal(5,2)

在Hive里面,整型数值默认当做int处理,除非超出了int值的范围。如果需要当做tinyint or smallint or bigint处理,需要在值后面分别添加后缀Y,S or L。

例如:100Y -> TINYINT, 100S -> SMALLINT, 100L -> BIGINT

String Data Types

从0.14版本开始,Hive支持3种字符类型,见如下表格:

Primitive - String Data Types
Type Description Examples
string sequence of characters.either single quotes(') or double quotes ('') can be used to enclose characters 'string'
varchar max length is specified in braces.similar to SQL's varchar.max length allowed is 65355 bytes 'str'
char similar to SQL's char with fixed length. i.e values shorter than the specified length are padded with spaces 's'

char vs varchar

  • char是固定长度的,不足用空格补齐;
  • varchar是变长的,但是需要指定最大长度(例如:name VARCHAR(64))。若长度不足,不会用空格补齐,不占用剩余空间;
  • char的最大长度是255,varchar的最大长度可到65335字节;
  • varchar会进行空间/存储(space/storage)优化,但是char不会;
  • 如果一个字符长度超过varchar指定的长度,会被自动截取。

Date/Time Data Types

猜你喜欢

转载自www.cnblogs.com/ylhzhi/p/9504679.html