hive复杂结构之array,map,struct(hive array map)
wxin55 2024-11-06 12:43 10 浏览 0 评论
hive提供了复合数据类型array,map,struct,下面来依次介绍
一.array介绍
语法:array<int> 例如:[1,2,3]
array<string> 例如 ['a','b','c']
描述:数组是一组具有相同类型和名称的变量的集合。这些变量成为数组的元素,每个数组元素都有一个编号,编号从0开始。例如,数组['Jack','David'],那么第二个元素可以用数组名[1]进行引用。
创建数组
create table array_test
(
name string
,dept_id array<int>
,status array<string>
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY ','
;
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' 设置字段间的分隔符为tab
COLLECTION ITEMS TERMINATED BY ',' 设置字段中每个元素间的分隔符为逗号,数组中必须使用该设置
准备数据:array_test.txt
zs 1,2,3,4 ok,return,reject,fail
ls 11,22,33,41 ok,reject,fail
wl 100
加载数据
LOAD DATA LOCAL INPATH '/Users/admin/Documents/bigdata/test_file/array_test.txt' OVERWRITE INTO TABLE array_test;
查询数据
hive (test)> select * from array_test;
array_test.name array_test.dept_id array_test.status
zs [1,2,3,4] ["ok","return","reject","fail"]
ls [11,22,33,41] ["ok","reject","fail"]
wl [100] NULL
查询数组字段的长度:size(array)
hive (test)> select name,size(dept_id),size(status) from array_test;
name _c1 _c2
zs 4 4
ls 4 3
wl 1 -1
注意:由上面的结果可看出当数组为null时,查出的长度为-1
查询数组字段中某一位置的元素:array[index]
hive (test)> select name,dept_id[0],status[2] from array_test;
name _c1 _c2
zs 1 reject
ls 11 fail
wl 100 NULL
查询数组中是否包含某元素:array_contains(数组名,值)
array_contains方法返回一个Boolean对象,可以配合if函数使用,例如 if(array_contains(cloumn,'A'),'包含A','不包含A')
hive (test)> select name,array_contains(status,'ok') from array_test;
name _c1
zs true
ls true
wl false
hive (test)> select name,if(array_contains(status,'ok'),'yes','no') from array_test;
name _c1
zs yes
ls yes
wl no
查询status中包含'ok'的数据
hive (test)> select * from array_test where array_contains(status,'ok');
array_test.name array_test.dept_id array_test.status
zs [1,2,3,4] ["ok","return","reject","fail"]
ls [11,22,33,41] ["ok","reject","fail"]
此处注意,如果不使用该函数,而直接使用 “值 in/not in 数组”,会报错,会报类型不匹配的错误
hive (test)> select * from array_test where status in ('ok');
FAILED: SemanticException Line 0:-1 Wrong arguments ''ok'': The arguments for IN should be the same type! Types are: {array<string> IN (string)}
查询数组中的每一个元素:explode()
注意:explode()函数只是生成了一个数据的展示方式,无法在表中产生一个新的数据列,即select name,explode(dept_id) from array_test; 会报错的
col
1
2
3
4
11
22
33
41
100
hive (test)> select name,explode(dept_id) from array_test;
FAILED: SemanticException [Error 10081]: UDTF's are not supported outside the SELECT clause, nor nested in expressions
要想展示其他列,可以使用下面的lateral view explode(array)来实现
一行转多行(行转列):lateral view explode(array)
lateral view 会将explode生成的结果放到一个虚拟表中,然后这个虚拟表会和输入行即每个name进行join,来达到数据聚合的目的。
hive (test)> select name ,a_dept_id from array_test
> lateral view explode(dept_id) a as a_dept_id -- 要进行聚合的虚拟表,lateral view explode(字段) 虚拟表名 as 虚拟表字段
> ;
name a_dept_id
zs 1
zs 2
zs 3
zs 4
ls 11
ls 22
ls 33
ls 41
wl 100
split()将字符串转成字符串数组
语法: split(string str, string pat)
返回值: array
说明: 按照pat字符串分割str,会返回分割后的字符串数组
hive (test)> select * from split_test;
OK
split_test.id split_test.info
1 a,b,c
2 d,e,f
3
4 NULL
将split_test表中的info字段转成数组
hive (test)> select id,split(info,',') as array_info, size(split(info,',')) as array_info_size from split_test;
id array_info array_info_size
1 ["a","b","c"] 3
2 ["d","e","f"] 3
3 [""] 1
4 NULL -1
二.map介绍
语法:
map(k1,v1,k2,v2,...)
描述:
map是一组键-值对元组集合,map类型的数据可以通过'列名['key']的方式访问
创建map
create table map_test
(
id int
,course map<string,int>
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY ','
MAP KEYS TERMINATED BY ':'
;
MAP KEYS TERMINATED BY :key value分隔符
准备数据: map_test.txt
1 chinese:80,math:90,english:100
2 chinese:95,math:70
3
加载数据
LOAD DATA LOCAL INPATH '/Users/admin/Documents/bigdata/test_file/map_test.txt' OVERWRITE INTO TABLE map_test;
查询数据
hive (test)> select * from map_test;
map_test.id map_test.course
1 {"chinese":80,"math":90,"english":100}
2 {"chinese":95,"math":70}
3 {}
查询map的长度:size(map)
hive (test)> select id,size(course) from map_test;
id _c1
1 3
2 2
3 0
查询map字段中的某个元素:map['parame']
hive (test)> select id,course['math'] as math from map_test;
OK
id math
1 90
2 70
3 NULL
查询map中所有的key, 返回值类型为 array:map_keys(map)
hive (test)> select id,map_keys(course) as key from map_test;
id key
1 ["chinese","math","english"]
2 ["chinese","math"]
3 []
查询map中所有的value,返回值类型为array:map_values(map)
hive (test)> select id,map_values(course) as value from map_test;
id value
1 [80,90,100]
2 [95,70]
3 []
判断map中是否包含某个key值:array_contains(map_keys(map))
hive (test)> select * from map_test where array_contains(map_keys(course),'math');
map_test.id map_test.course
1 {"chinese":80,"math":90,"english":100}
2 {"chinese":95,"math":70}
一行转多行(行转列):lateral view explode(map)
hive (test)> select id,subject,score from map_test
> lateral view explode(course) a as subject,score;
id subject score
1 chinese 80
1 math 90
1 english 100
2 chinese 95
2 math 70
三.struct介绍
语法:
struct(val1, val2, val3, ...) ,struct内部的元素类型可以为array,map,struct复杂结构
描述:
和C语言中的struct或者对象类似,都可以通过"点"符号访问元素内容。例如,如果某个列的数据类型为struct{first string,last string},那么第一个元素可以通过字段名.first来引用
创建struct表
create table struct_test
(
id int
,info struct<name:string,age:int>
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY ','
MAP KEYS TERMINATED BY ':'
;
准备数据:
LOAD DATA LOCAL INPATH '/Users/admin/Documents/bigdata/test_file/struct_test.txt' OVERWRITE INTO TABLE struct_test;
查询数据:
hive (test)> select * from struct_test;;
struct_test.id struct_test.info
1 {"name":"zs","age":18}
2 {"name":"ls","age":22}
3 {"name":"ww","age":30}
struct 无size()函数
hive (test)> select id,size(info) from struct_test;
FAILED: SemanticException [Error 10016]: Line 1:15 Argument type mismatch 'info': "map" or "list" is expected at function SIZE, but "struct<name:string,age:int>" is found
查询struct中的元素:struct.元素名称
hive (test)> select id,info.name,info.age from struct_test;
id name age
1 zs 18
2 ls 22
3 ww 30
相关推荐
- ES6中 Promise的使用场景?(es6promise用法例子)
-
一、介绍Promise,译为承诺,是异步编程的一种解决方案,比传统的解决方案(回调函数)更加合理和更加强大在以往我们如果处理多层异步操作,我们往往会像下面那样编写我们的代码doSomething(f...
- JavaScript 对 Promise 并发的处理方法
-
Promise对象代表一个未来的值,它有三种状态:pending待定,这是Promise的初始状态,它可能成功,也可能失败,前途未卜fulfilled已完成,这是一种成功的状态,此时可以获取...
- Promise的九大方法(promise的实例方法)
-
1、promise.resolv静态方法Promise.resolve(value)可以认为是newPromise方法的语法糖,比如Promise.resolve(42)可以认为是以下代码的语...
- 360前端一面~面试题解析(360前端开发面试题)
-
1.组件库按需加载怎么做的,具体打包配了什么-按需加载实现:借助打包工具(如Webpack的require.context或ES模块动态导入),在使用组件时才引入对应的代码。例如在V...
- 前端面试-Promise 的 finally 怎么实现的?如何在工作中使用?
-
Promise的finally方法是一个非常有用的工具,它无论Promise是成功(fulfilled)还是失败(rejected)都会执行,且不改变Promise的最终结果。它的实现原...
- 最简单手写Promise,30行代码理解Promise核心原理和发布订阅模式
-
看了全网手写Promise的,大部分对于新手还是比较难理解的,其中几个比较难的点:状态还未改变时通过发布订阅模式去收集事件实例化的时候通过调用构造函数里传出来的方法去修改类里面的状态,这个叫Re...
- 前端分享-Promise可以中途取消啦(promise可以取消吗)
-
传统Promise就像一台需要手动组装的设备,每次使用都要重新接线。而Promise.withResolvers的出现,相当于给开发者发了一个智能遥控器,可以随时随地控制异步操作。它解决了三大...
- 手写 Promise(手写输入法 中文)
-
前言都2020年了,Promise大家肯定都在用了,但是估计很多人对其原理还是一知半解,今天就让我们一起实现一个符合PromiseA+规范的Promise。附PromiseA+规范地址...
- 什么是 Promise.allSettled()!新手老手都要会?
-
Promise.allSettled()方法返回一个在所有给定的promise都已经fulfilled或rejected后的promise,并带有一个对象数组,每个对象表示对应的pr...
- 前端面试-关于Promise解析与高频面试题示范
-
Promise是啥,直接上图:Promise就是处理异步函数的API,它可以包裹一个异步函数,在异步函数完成时抛出完成状态,让代码结束远古时无限回掉的窘境。配合async/await语法糖,可...
- 宇宙厂:为什么前端离不开 Promise.withResolvers() ?
-
大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发。1.为什么需要Promise.with...
- Promise 新增了一个超实用的 API!
-
在JavaScript的世界里,Promise一直是处理异步操作的神器。而现在,随着ES2025的发布,Promise又迎来了一个超实用的新成员——Promise.try()!这个新方法简...
- 一次搞懂 Promise 异步处理(promise 异步顺序执行)
-
PromisePromise就像这个词的表面意识一样,表示一种承诺、许诺,会在后面给出一个结果,成功或者失败。现在已经成为了主流的异步编程的操作方式,写进了标准里面。状态Promise有且仅有...
- Promise 核心机制详解(promise机制的实现原理)
-
一、Promise的核心状态机Promise本质上是一个状态机,其行为由内部状态严格管控。每个Promise实例在创建时处于Pending(等待)状态,此时异步操作尚未完成。当异步操作成功...
- javascript——Promise(js实现promise)
-
1.PromiseES6开始支持,Promise对象用于一个异步操作的最终完成(包括成功和失败)及结果值的表示。简单说就是处理异步请求的。之所以叫Promise,就是我承诺,如果成功则怎么处理,失败怎...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- ES6中 Promise的使用场景?(es6promise用法例子)
- JavaScript 对 Promise 并发的处理方法
- Promise的九大方法(promise的实例方法)
- 360前端一面~面试题解析(360前端开发面试题)
- 前端面试-Promise 的 finally 怎么实现的?如何在工作中使用?
- 最简单手写Promise,30行代码理解Promise核心原理和发布订阅模式
- 前端分享-Promise可以中途取消啦(promise可以取消吗)
- 手写 Promise(手写输入法 中文)
- 什么是 Promise.allSettled()!新手老手都要会?
- 前端面试-关于Promise解析与高频面试题示范
- 标签列表
-
- hive行转列函数 (63)
- sourcemap文件是什么 (54)
- display none 隐藏后怎么显示 (56)
- 共享锁和排他锁的区别 (51)
- httpservletrequest 获取参数 (64)
- jstl包 (64)
- qsharedmemory (50)
- watch computed (53)
- java中switch (68)
- date.now (55)
- git-bash (56)
- 盒子垂直居中 (68)
- npm是什么命令 (62)
- python中+=代表什么 (70)
- fsimage (51)
- nginx break (61)
- mysql分区表的优缺点 (53)
- centos7切换到图形界面 (55)
- 前端深拷贝 (62)
- kmp模式匹配算法 (57)
- jsjson字符串转json对象 (53)
- jdbc connection (61)
- javascript字符串转换为数字 (54)
- mybatis 使用 (73)
- 安装mysql数据库 (55)