- 参数说明
创建表的时候可以指定每行数据的格式,如果使用的是复合数据类型,还需要指定复合数据类型中的元素分割符
ROW FORMAT DELIMITED
[FIELDS TERMINATED BY char [ESCAPED BY char]]
[COLLECTION ITEMS TERMINATED BY char]
[MAP KEYS TERMINATED BY char]
[LINES TERMINATED BY char]
其中这里
FIELDS TERMINATED BY char 指定每一行记录中字段的分割符
COLLECTION ITEMS TERMINATED BY char 指定复合类型中多元素的分割符
MAP KEYS TERMINATED BY char 指定map集合类型中每一个key/value之间的分隔符
LINES TERMINATED BY char 指定每行记录的换行符,一般有默认 就是\n
- Array类型
- array中的数据为相同类型,例如:array A中元素['a','b','c'],则A[1]的值为'b'
- 建表语句
create table t_array(
id string,
name string,
locations array<string>
) row format delimated fields terminated by ' '
collection items terminated by ',';
- 准备数据文件t_array.txt(使用空格分隔符)
1 zhangsan beijing,shanghai
2 lisi shanghai,tianjin
load data local inpath '/bigdata/logs/t_array.txt' into table t_array;
select id,name,locations[0],locations[1] from t_array;
- Map类型
- map类型中存储key/value类型的数据,后期可以通过["指定key名称"]访问
- 建表语句
create table t_map(
id string,
info map<string,string>
) row format delimited fields terminated by ' '
collection items terminated by '#'
map keys terminated by ':';
1 name:zhangsan#age:30
2 name:lisi#age:40
load data local inpath '/bigdata/logs/t_map.txt' into table t_map;
select id info['name'],info['age'] from t_map;
- Struct类型
- 可以存储不同类型的数据,eg:c的类型为struct{a int;b int},我们可以通过c.a来访问域a
- 建表语句
create table t_struct(
id string,
info struct<name:string,age:int,address:String>
) row format delimited fields terminated by ' '
collection items terminated by ':';
- 准备数据t_struct.txt(字段以空格分割)
1 zhangsan:30:beijing
2 lisi:40:shanghai
load data local inpath '/bigdata/logs/t_struct.txt' into table t_struct;
select id,info.name,info.age,info.address from t_struct;