学习大数据DAY40 基于 hive 的数据处理

devtools/2024/10/22 14:31:39/

目录

Hive 复合数据定义方法

Hive 复合数据查询方法

hive%20%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0-toc" style="margin-left:40px;">hive 内置函数

上机练习


Hive 复合数据定义方法

Hive 复合数据查询方法

hive%20%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0">hive 内置函数

-- 查看系统自带的函数
show functions;
-- 显示自带的函数的用法
desc function upper;
-- 详细显示自带的函数的用法
desc function extended upper;

上机练习

1 导入 user 数据 id name sex age address date lev s_info m_info a_info
2 使用三种复合数据类型 struct map array 存储 info 数据
python 清洗数据:
with open(r"D:\智云大数据\数据源
\user\user_info.txt",'r',encoding="utf-8") as f:
LineRead=f.readlines()
# print(LineRead)
for i in LineRead:
list1=[]
list_i=i.split('\t')
# print(list_i)
# 把字典的值复制加到每行的末尾
for j in eval(list_i[7]).values():
list1.append(j)
str1='|'.join(list1)
list_i[7]=str1
list_i.append(str1)
# print(list_i)
list_i[8]='|'.join(list_i[8].split(',')).strip('\n') #
把第九列转化成可以导入 struct 的形式
str_i=','.join(list_i) # 每行每个数据用逗号隔开
# print(str_i)
with open(r"D:\智云大数据
\user_info_disposal_new.txt",'a',encoding='utf-8') as h:
h.writelines(str_i+"\n")
h.close()
f.close()
hive 建表和导入数据:
drop table if exists user_info;
create table if not exists user_info
(
id int,
name string,
sex string,
age tinyint,
address string,
date_info string,
lev tinyint,
a_info array<string>,
m_info map<string,string>,
s_info
struct<systemtype:string,education:string,marriage_status:string,phon
ebrand:string>
)
row format delimited
fields terminated by ','
collection items terminated by '|'
MAP keys terminated by ':'
lines terminated by '\n'
load data local inpath '/root/user_info_disposal_new.txt'
into table user_info;
结果:(双击 object 能看见值)
3 指标计算
3.1 按月统计各个地区男女生人数
select date_format(date_info,'YYYY-MM'),address,sex,count(1) from
user_info
group by date_format(date_info,'YYYY-MM'),address,sex3.2 统计各地区的不同手机型号使用人数,并按照老中青(35 以下青年 男 65 岁
以下女 55 岁以下中年 男 65 岁以上女 55 岁以上老年)年龄 划分
地区 安卓使用人数{老 中 青} ios 使用人数{老 中 青}
with SystemtypeAndAge as
(
select
case when m_info["systemtype"]='android' then 'android'
when m_info["systemtype"]='ios' then 'ios'end as systemtype,
case when age<35 then '青年'
when sex='male' and age<65 or sex='female' and age<55 then '中年'
else '老年'
end as AgeBracket
from user_info
)
select systemtype,AgeBracket,count(1) from SystemtypeAndAge
group by systemtype,AgeBracket

3.2 统计各地区的不同手机型号使用人数,并按照老中青(35 以下青年 男 65 岁
以下女 55 岁以下中年 男 65 岁以上女 55 岁以上老年)年龄 划分
地区 安卓使用人数{老 中 青} ios 使用人数{老 中 青}
with SystemtypeAndAge as
(
select
case when m_info["systemtype"]='android' then 'android'
when m_info["systemtype"]='ios' then 'ios'end as systemtype,
case when age<35 then '青年'
when sex='male' and age<65 or sex='female' and age<55 then '中年'
else '老年'
end as AgeBracket
from user_info
)
select systemtype,AgeBracket,count(1) from SystemtypeAndAge
group by systemtype,AgeBracket

3.3 统计不同地区,不同学历,使用的手机品牌(去重)collect_list
地区 学历 手机品牌列表
select user_info.address,education
,collect_list(distinct m_info["phonebrand"])
from user_info
left join
(
select address,
case m_info["education"] when "bachelor" then 'bachelor'
when "doctor" then 'doctor'when "master" then 'master'
end as education
from user_info
) EducationInAddress on user_info.address=EducationInAddress.address
group by user_info.address,education

3.4 统计不同等级,各个手机品牌的使用人数(需要行转列)
等级 phonebrand_list
{iphone6:5 iphone7:5 .....mi:5 .....iphoneXS:2}
这题我不会,老师沉迷黑吗喽作业视频讲解都还没发......
第三阶段太吃自学能力了,hive语法基本要去网上找。

http://www.ppmy.cn/devtools/99473.html

相关文章

分享一个基于python的租房数据分析与可视化系统Hadoop大数据源码(源码、调试、LW、开题、PPT)

&#x1f495;&#x1f495;作者&#xff1a;计算机源码社 &#x1f495;&#x1f495;个人简介&#xff1a;本人 八年开发经验&#xff0c;擅长Java、Python、PHP、.NET、Node.js、Android、微信小程序、爬虫、大数据、机器学习等&#xff0c;大家有这一块的问题可以一起交流&…

通过Android模拟蓝牙并实现自动瞄准--kmbox的代替方案

在目前成熟的目标识别AI技术下&#xff0c;通过AI实现FPS射击游戏中的自动瞄准已经不是什么难题。然而&#xff0c;如何精准地模拟人类的鼠标操作却是一个核心问题。现有的主流技术包括通过罗技鼠标的驱动技术来控制鼠标移动&#xff0c;以及通过硬件完全模拟鼠标的KMBOX系列设…

SQL— DML语句学习【后端 10】

数据库操作-DML 详解 在数据库管理系统中&#xff0c;DML&#xff08;Data Manipulation Language&#xff0c;数据操作语言&#xff09;扮演着至关重要的角色&#xff0c;它负责对数据库中的数据进行增、删、改操作。掌握DML操作对于数据库的日常维护和管理至关重要。本文将详…

关于Arrays.asList返回List无法新增和删除?

这个是在写项目的时候发现的&#xff0c;然后就分析了一下源码&#xff0c;得其内部原理 复现代码示例&#xff1a; public class ArraysAsList {public static void main(String[] args) {Integer[] array {1, 2, 3, 4, 5};List<Integer> list Arrays.asList(array);…

海运整箱成本与拼箱成本对比 | 国际贸易服务平台 | 箱讯科技

整箱和拼箱 在集装箱运输业务中&#xff0c;我们把一个集装箱、一个出口人、一个收货人、一个目的港&#xff0c;满足这“四个一”条件的货物叫做整箱货&#xff0c;而把一个集装箱、出口人、收货人和目的港这三项之中只要有一项是在两个或两个以上的出口运输货物&#xff0c;就…

linux(arm)移植 macchanger

在ARM Linux平台上移植macchanger工具主要涉及从源代码编译该工具&#xff0c;‌并确保它在目标ARM设备上正确运行。‌macchanger是一个用于操纵网络接口MAC地址的小工具&#xff0c;‌常用于增强网络隐私和安全性。‌ 移植步骤 1.‌准备开发环境‌&#xff1a;‌ 确保你有一…

009 批量删除

文章目录 前端后端 https://element.eleme.cn/#/zh-CN/component/button 前端 <el-button type"danger" click"batchDelete">批量删除</el-button><el-treeref"menuTree"batchDelete() {console.log("批量删除菜单..."…

第九周:机器学习笔记

第九周机器学习周报 摘要Abstract机器学习——Spatial Transformer1.1 How to transform an image/feature map?&#xff08;怎么做&#xff09;1.2 Interpolation&#xff08;插值&#xff09;1.3 spatial Transformer的应用 Pytorch学习1. 线性层2. 其他层的介绍3. 搭建小实…