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

编程入门 行业动态 更新时间:2024-10-08 06:28:02

记一次HIve<a href=https://www.elefans.com/category/jswz/34/1771149.html style=作业(数据表创建时加载数据,各复杂类型取值)"/>

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

数据表创建时加载数据,各复杂类型取值

  • 源数据
  • 操作步骤
  • 总结
    • 定义格式符时我不熟悉,下面我在打一遍,定义的顺序不能变
    • 各类型取值

源数据

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类型    这个是没有学过的  里面的存储形式可以理解为 定义多个字段(可以是不同类型)  
--复杂类型名.字段名

更多推荐

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

本文发布于:2024-03-08 00:02:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1719305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   数据表   加载   类型   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!