栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 云计算 > 云平台

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

云平台 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

记一次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,
humanities_and_sciences  map,
comperhensive struct
)
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
-- 并指定别名为 first 注意不要加 引号!!!
-- Array类型通过下标取值
select student_name,intent_university[0] as first from student_exam_table; 

-- 6:查询学生成绩表student_exam_table 表中,学生的物理或者历史成绩
-- humanities_and_sciences  map
-- 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
-- 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类型    这个是没有学过的  里面的存储形式可以理解为 定义多个字段(可以是不同类型)  
--复杂类型名.字段名
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/897463.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号