【hive】字符串操作,截取想要的字符串

本文通过实例详细介绍了如何在Hive中使用split和substr函数截取字符串,提取年份并进行统计分析。从单条数据测试到全表操作,再到统计每年电影数量,展示了Hive在数据处理上的应用。同时,文章还涉及正则表达式、临时表的创建以及各种聚合函数的使用,如regexp_extract、explode等,用于完成更复杂的数据分析任务,如电影类型和评分的统计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

你好呀!这里是小易同学的博客,一名大二在校生。
写博客是为了记录自己的学习过程,同时也希望能帮助到需要帮助的人。
如果我的博客可以帮助到你,不妨给我一个关注🥰


前言

对字符串截取,这里主要有两种方法:

一是用split截取字符串

二是用substr截取字符串


提示:以下是本篇文章正文内容

一、目标

将hive中movie表的年份数字取出来,并统计每一年有多少部电影

二、截取字符串,取出年份数字

1.split

注:在分割时加“\\”是因为()是特殊字符,需要加\\才能让hive识别

 (1)因为数据太多,先用一条数据来测试,也就是movieId=1这条数据

select split(title,"\\(") from yiqianbin_movie where movieId=1;

select split(title,"\\(")[1] from yiqianbin_movie where movieId=1;

select split(split(title,"\\(")[1],"\\)")[0] from yiqianbin_movie where movieId=1;

(2)取出了1995,那么就可以直接去掉后面where的条件,查出全部的年份

select split(split(title,"\\(")[1],"\\)")[0] from yiqianbin_movie;

2.substr

注:这里加  where title<>"title"  是为了去掉表头“title”

三、统计每个数字出现了多少次

实例:创建临时表,一步完成计数

select year,count(1) from
(select regexp_extract(title, "(\\d{4})",1)as year from yiqianbin_movie where title regexp '\\d{4}') temp
group by year;


补充

杂七杂八

select * from yiqianbin_movie where title not regexp '\\d{4}';

create table movie_year as select *,substr(title,-5,4) as year from yiqianbin_movie;

create table movies_year as select *,regexp_extract(title, "(\\d{4})",1)as year from yiqianbin_movie where title regexp '\\d{4}';

select substr(title,-5,4)from yiqianbin_movie where title regexp '\\d{4}';

select year,count(1) from movies_year group by year;

查出括号中的四个数字

select regexp_extract(title,"\\((\\d{4})\\)") from yiqianbin_movie;

查出哪一年出的电影最多,即年份出现最多的数字对应的电影名
select year,count(1) as year1 from movies_year group by year order by year1 desc limit 1;

将多个电影类型分开
先分开
select explode(split(genres,'\\|')) as type from yiqianbin_movie;   
之后将分开的导入一个新表
create table movies_type as select explode(split(genres,'\\|')) as type from yiqianbin_movie;
将类型的多少按降序排下来
select type,count(1) as count from movies_type group by type order by count desc;
 临时表(一步完成)
select type,count(1) as count from (select explode(split(genres,'\\|')) as type from yiqianbin_movie)temp group by type order by count desc;

查出评分次数最高的电影
select movieId,count(1) as count from yiqianbin_ratings group by movieId order by count desc limit 1;
select * from(select movieId,count(1) as count from yiqianbin_ratings group by movieId order by count desc limit 1)t left join yiqianbin_movie m on t.movieId = m.movieId;

什么类型的电影评分最高      第四行是一个视图查询
1.先连接查询,每部电影的评分+分类
2.分类  行专列
3.按类型分组统计                                   
select type,avg(rating) as avgrating from
(select t1.rating,type.col as type from(select r.rating,m.genres from yiqianbin_ratings r
 left join yiqianbin_movie m on r.movieId = m.movieId)t1
lateral view explode(split(t1.genres,'\\|')) type)t2
group by type order by avgrating desc
limit 10;

按年份统计电影数量
create table movies_year_result(year string,num string);
insert into movies_year_result
select year,count(1) from
(select substr(title,-5,4) as year from yiqianbin_movie where title regexp '\\d{4}')t group by year;

什么类型电影最多
create table genres_top10_result(type string,num string);
insert into genres_top10_result
select type,count(1) as total from
(select explode(split(genres,'\\|')) as type from yiqianbin_movie)t group by type
order by total desc limit 10;

评分人气最高电影top10
create table hot_top10_result(title string,rating string);
insert into hot_top10_result
select m.title,t.total from (
select movieId,count(1) as total
from yiqianbin_ratings group by movieId
order by total desc limit 10)t
left join yiqianbin_movie m on t.movieId = m.movieId;

高频词top10
create table tag_top10_result(tag string,num string);
insert into tag_top10_result
select tag,count(1) as total from
(select explode(split(tag,'\\s')) as tag from yiqianbin_tags)t
group by tag
order by total desc limit 10;

评分最高电影top10
create table rating_top10_result(title string,rating string);
insert into rating_top10_result
select m.title,t.avgrating from(
select movieId,avg(rating) as avgrating
from yiqianbin_ratings
group by movieId
order by avgrating desc limit 10)t
left join yiqianbin_movie m on t.movieId = m.movieId;


 


 

### HiveSQL 字符串截取函数及用法 #### substr 和 substring 函数 `substr` 或 `substring` 是用于从给定字符串中提取子字符串的函数。这两个函数功能相同,在使用上可以根据个人习惯选择。 - **语法** - `substr(string A, int start)` 或者 `substr(string A, int start, int length)` - 参数解释: - `A`: 被操作的目标字符串。 - `start`: 子字符串起始位置,支持正负索引。正数表示从左边开始计算的位置;负数则代表从右边算起。 - `length`(可选): 提取出多少字符,默认直到字符串结束为止。 - **实例展示** ```sql SELECT substr('hello world', 1, 5); -- 结果: hello SELECT substr('hello world', -5); -- 结果: world ``` 上述命令分别实现了从前向后选取前五个字符以及从后往前选取最后五个字符的效果[^3]。 #### split 函数配合 array 访问元素方式实现复杂场景下的字符串切割 当面对更复杂的字符串处理需求时,比如按照特定分隔符拆分并获取其中某一部分的内容,则可以考虑采用 `split()` 方法先将整个字符串转换成数组形式再通过下标访问目标片段: - **语法**: `split(string str, string pattern)` - **参数描述**: - `str`: 需要被分割的大字符串; - `pattern`: 定义用来区分各部分的小字符串模式(通常是某种特殊符号),如逗号 `,`. - **组合应用案例** 假设有一个由多个单词组成的列表 `"apple,banana,cherry"` ,现在想要单独拿到第二个水果名称 "banana": ```sql SELECT split('apple,banana,cherry', ',')[1]; -- 输出结果 banana ``` 这里利用了 Hive 的特性可以直接对数组进行索引访问来简化代码逻辑[^2]. #### locate 函数辅助定位特定字符首次出现的位置 如果目的是找到某个子串第一次出现在源字符串中的确切位点以便后续进一步加工的话,那么就可以借助于 `locate()` 来完成这项工作: - **定义**: `locate(substring, string [, position])` - **说明**: - 如果指定了第三个参数position,则会从此处开始搜索而不是默认从头找起; - 返回值为所查到的第一个匹配项所在偏移量加一(即基于人类计数而非计算机科学里的零基地址体系);如果没有发现任何符合条件的结果就给出0作为回应. - **实际运用举例** 为了示范如何结合其他工具共同解决问题,下面这条语句展示了怎样确定字母'o'在单词"hello"里最早显现的地方: ```sql SELECT locate('o', 'hello'); -- 结果应该是 5 因为 o 在第五个位置. ``` 此方法适用于那些需要知道具体坐标的场合,例如准备做精准替换或是裁剪等精细化编辑任务之前[^1].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小易同学go

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

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

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

打赏作者

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

抵扣说明:

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

余额充值