记一次HIve作业(数据表创建时加载数据,各复杂类型取值)

本文详细介绍了如何在Hive中创建外部表`student_exam_table`,包括数据加载、表结构定义以及各种复杂类型如Array、Map、Struct的取值方法。操作步骤涵盖从创建文本文件到查询学生考试成绩的完整流程,解析了字段分隔符的使用规则,并给出了查询不同成绩类型的SQL示例。
摘要由CSDN通过智能技术生成

源数据

Mandy,Peking University-Wuhan University-Nankai University,Chemistry:90-Physics:98-Biology:83,126-135-140
Jerome,Tsinghua University-Fudan University-Nanjing University,History:89-Politics:92-Geography:87,130-116-128
Delia,Nanjing University-Wuhan University-Nankai University,Chemistry:87-Physics:95-Biology:73,102-123-112
Ben,Tianjin Universit-Peking University-Fudan University,Chemistry:92-Physics:88-Biology:79,98-142-106
Carter,Tsinghua University-Fudan University-Tianjin Universit,History:90-Politics:91-Geography:80,109-111-140
Vivian,Fudan University-Nanjing University-Nankai University,Chemistry:83-Physics:86-Biology:90,120-140-132

操作步骤

-- 1:在虚拟机 /opt/software 下创建student_exam.txt  并添加内容
-- 2:将 student_exam.txt 文件上传到/hive_data/data/student_exam/    下
-- 3:根据数据的结构 创建考试成绩表 student_exam_table
-- student_name 学生姓名  
-- intent_university  意向大学
-- humanities_and_sciences 文/理成绩
-- comperhensive   综合成绩
create external table student_exam_table(
student_name   string,
intent_university Array<string>,
humanities_and_sciences  map<string,float>,
comperhensive struct<chinese:float,maths:float,english:float>
)
row format delimited
--下面 指定分隔符的顺序不能变
--字段之间的分隔符
fields terminated by ','
--复杂类型分隔符   (数据之间的分隔符)
collection items terminated by '-'
-- MAP 类型分隔符
map keys terminated by ':'
--行分隔符
lines terminated by '\n'
--指定我们加载的数据
location '/hive_data/data/student_exam/';

-- 4:查看表记录
SELECT  *  FROM  student_exam_table ;

--   操作
--5: 查询学生成绩表student_exam_table 表中所有学生的第一意向大学  》intent_university Array<string>
-- 并指定别名为 first 注意不要加 引号!!!
-- Array类型通过下标取值
select student_name,intent_university[0] as first from student_exam_table; 

-- 6:查询学生成绩表student_exam_table 表中,学生的物理或者历史成绩
-- humanities_and_sciences  map<string,float>
-- map 类型 取值是通过 key   
--格式: 集合名["key名"]   注意要加引号!!!!!
select humanities_and_sciences["Physics"] as Physics,humanities_and_sciences["History"] as History from student_exam_table;

--7:查询学生考试表student_exam_table中,所有学生的数学成绩
--comperhensive struct<chinese:float,maths:float,english:float>
-- struct 是复杂类型   取值是通过名.类型名  >得到相应的
select student_name, comperhensive.maths as math from student_exam_table; 

总结

定义格式符时我不熟悉,下面我在打一遍,定义的顺序不能变

----------------------------------------------------------
row format delimited 
--1	字段之间的分隔符
fields terminated by ','

--2	多数据之间的分隔符  也就是数据之间
collection items terminated by '-'
--3	MAP 类型分隔符
map keys terminated by ':'
--4	行分隔符
lines terminated by '\n';

各类型取值

--1:Array类型   可以理解为 list类型 也就是列表 我们的数据通过下标访问   0,1,2,....
--数组名[下标]
--2:Map 类型     可以理解为 map类型  也就是key,value 键值对    数据通过 对应key访问
--map["key"]   主要要加引号
--3:struct类型    这个是没有学过的  里面的存储形式可以理解为 定义多个字段(可以是不同类型)  
--复杂类型名.字段名
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

·惊鸿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值