注意事项
- 蓝色字体是建表语法的关键字,用户指定某些功能。
- [ ] 中括号的语法表示可选。
- | 表示使用的时候,左右语法二选一。
- 建表语句中的语法顺序要和语法树中顺序保持一致
Hive数据类型指的是表中列的字段类型
注意事项
- HIive SQL中,数据类型英文字母大小写不敏感;
- 除SQL数据类型外,还支持Java数据类型,比如字符串string;
- 复杂数据类型的使用通常需要和分隔符指定语法配合使用;
- 如果定义的数据类型和文件不一致,Hive会尝试隐式转换,但不保证成功。
隐式转换
显示转换
目标要求:
create database if not exists kox01;
use kox01;
create table t_archer(id int comment "ID",name string comment "英雄名称",hp_max int comment "最大生命",mp_max int comment "最大法力",attack_max int comment "最高物攻",defense_max int comment "最大物防",attack_range string comment "攻击范围",role_main string comment "主要定位",role_assist string comment "次要定位"
) comment "王者荣耀射手信息"
row format delimited
fields terminated by "\t";
验证是否创建成功
mkdir hivedata
cd hivedata/
hadoop fs -put archer.txt /user/hive/warehouse/kox01.db/t_archer
验证查询是否成功
目标要求:
实现过程及结果
create table t_hot_hero_skin_price(id int,name string,win_rate int,skin_price map -- 复杂类型
) row format delimited
fields terminated by ',' -- 指定字段之间分隔符
collection items terminated by '-' -- 指定集合元素之间的分隔符
map keys terminated by ':'; -- 指定map元素kv之间的分隔符
验证是否创建成功
hadoop fs -put hot_hero_skin_price.txt /user/hive/warehouse/kox01.db/t_hot_hero_skin_price
验证查询是否成功
目标任务:
create table t_team_ace_player(id int,team_name string,ace_player_name string
); --没有指定row format语句 此时采用的是默认的\001作为字段的分割符
验证是否创建成功
hadoop fs -put team_ace_player.txt /user/hive/warehouse/kox01.db/t_team_ace_player
验证查询是否成功
任务目标:
hadoop fs -mkdir /data
hadoop fs -put team_ace_player.txt /data
create table t_team_ace_player_location(id int,team_name string,ace_player_name string
) location '/data'; -- 使用loacation关键字指代指定本张表数据在hdfs上的存储路径
验证查询是否成功