hive关于一个表的操作
Posted BigBrother@@U
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hive关于一个表的操作相关的知识,希望对你有一定的参考价值。
1,创建表格
create table stu(name string,hobby array<string>,scores map<string,double>)
row format delimited
fields terminated by ','
collection items terminated by '-'
MAP KEYS TERMINATED BY ':';
2,导入数据
数据:
zs,drink-eat,english:90-math:66
ls,drink-playgames-chat,english:60-math:66
w6,drink,english:80-math:66
w7,playgames,english:90-math:70
z9,drink-eat,english:90.4-math:66.5
x8,drink,english:80.3-math:66.7
load 命令导入数据到表中
load data local inpath '/root/data' into table stu;
3,练习
查询所有
select * from score
关系运算符
1、查询math成绩大于66的学生
select * from score where scores['math']>66
2、查询math成绩等于66的学生
select * from score where scores['math']=66
3、查询math成绩不等于90的学生
select * from score where scores['math']<>90;
4、like 查询类似的
w开头的数据 和s结尾的数据
select * from score where name like 'w%';
select * from score where name like '%s'
算术运算符
1、相加 语文+数学的成绩和
select scores['chinese']+scores['math']from score
数学函数
1、四舍五入
select round(scores['english']) from stu where name='z9';
2、floor 丢弃
select floor(scores['math']) from stu where name='x8';
3、ceil 进一
select ceil(scores['english']) from stu where name='x8';
4、max最大值
找到数学成绩最高是多少
select max(scores['math']) from stu;
5、sum
select sum(scores['math']) from stu;
6、avg平均
select avg(scores['math']) from stu;
7、列的方差
var_pop
select var_pop(scores['math']) from stu;
收集函数
1、size (数组 或者 map)返回集合个数
找出每个人的爱好有几个
select name,size(hobby) from stu;
字符串函数
1、length:返回长度
select name,length(name) from stu;
2、regexp_replace(string A, string B, string C)
字符串A中的B字符被C字符替代
姓名中的z被x代替:
select regexp_replace(name,'z','x') from stu;
3、trim去掉字符串两边的空格
trim(string A):去掉A两边的空格
4、split(string str, string pat):按照pat分割str返回一个数组
5、explode(array a):把一个数组打散为多行
其他
corr(col1, col2)
返回两列数值的相关系数
以上是关于hive关于一个表的操作的主要内容,如果未能解决你的问题,请参考以下文章