1. 程式人生 > >Hive(21):Hive資料型別

Hive(21):Hive資料型別

1.Hive資料型別

  數值型:tinyint、smallint、int、bigint
  字元型:varchar、char、string
  時間型:date、timestamp
  其他型別:boolean
  複雜型別:arrays(下標是從0開始)、map(key,value)、structs
     userlist map(int,string)    userlist['1']取出value
     user structs<name:string,age:int>  user.name  user.age

2.例項

(1)建立表

CREATE TABLE if not exists `jihetable`(                        
  `id` string,
  `area` array<string>,
  `province` map<int,string>,
  `city` struct<name:string,city:string,type:int>)
ROW FORMAT DELIMITED
  FIELDS TERMINATED BY '\t'
  COLLECTION ITEMS TERMINATED BY ','
  MAP KEYS TERMINATED BY ':'
  LINES TERMINATED BY '\n'
STORED AS textfile;

(2)資料

    A    華東,華南,華北    1:北京,2:天津,2:天津    南京,A23,2
    B    華東,西南,華北    3:北京,2:天津,2:天津    福州,A23,2
    C    華東,華南,中原    5:寧夏,2:河南,2:天津    上海,A1,2

(3)載入資料load

load data local inpath '/opt/datas/jihetest' into table jihetable;

(4)檢視

select * from jihetable;

結果:
+---------------+-------------------+---------------------+-------------------------------------------+--+
| jihetable.id  |  jihetable.area   | jihetable.province  |              jihetable.city               |
+---------------+-------------------+---------------------+-------------------------------------------+--+
| A             | ["華東,華南,華北"]      | {}                  | {"name":"南京,A23","city":"3","type":null}  |
| B             | ["華東,語文,屬性"]      | {}                  | {"name":"上海,A23","city":"3","type":null}  |
| A             | ["華東","華南","華北"]  | {1:"北京",2:"天津"}     | {"name":"南京","city":"A23","type":2}       |
| B             | ["華東","西南","華北"]  | {3:"北京",2:"天津"}     | {"name":"福州","city":"A23","type":2}       |
| C             | ["華東","華南","中原"]  | {5:"寧夏",2:"河南"}     | {"name":"上海","city":"A1","type":2}        |
+---------------+-------------------+---------------------+-------------------------------------------+--+