我是靠谱客的博主 秀丽大神,最近开发中收集的这篇文章主要介绍hive学习笔记之七:内置函数,mybatis防止sql注入原理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

locate

log

log10

log2

lower

lpad

ltrim

map

map_keys

map_values

matchpath

max

min

minute

month

months_between

named_struct

negative

next_day

ngrams

noop

noopstreaming

noopwithmap

noopwithmapstreaming

not

ntile

nvl

or

parse_url

parse_url_tuple

percent_rank

percentile

percentile_approx

pi

pmod

posexplode

positive

pow

power

printf

radians

rand

rank

reflect

reflect2

regexp

regexp_extract

regexp_replace

repeat

reverse

rlike

round

row_number

rpad

rtrim

second

sentences

shiftleft

shiftright

shiftrightunsigned

sign

sin

size

sort_array

soundex

space

split

sqrt

stack

std

stddev

stddev_pop

stddev_samp

str_to_map

struct

substr

substring

sum

tan

to_date

to_unix_timestamp

to_utc_timestamp

translate

trim

trunc

ucase

unbase64

unhex

unix_timestamp

upper

var_pop

var_samp

variance

weekofyear

when

windowingtablefunction

xpath

xpath_boolean

xpath_double

xpath_float

xpath_int

xpath_long

xpath_number

xpath_short

xpath_string

year

|

~

Time taken: 0.003 seconds, Fetched: 216 row(s)

【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取
  1. 以lower函数为例,执行命令describe function lower;即可查看lower函数的说明:

hive> describe function lower;

OK

lower(str) - Returns str with all characters changed to lowercase

Time taken: 0.005 seconds, Fetched: 1 row(s)

  • 接下来从计算函数开始,体验常用函数;

  • 先执行以下命令,使查询结果中带有字段名:

set hive.cli.print.header=true;

[](

)计算函数

  1. 加法+:

hive> select name, age, age+1 as add_value from student;

OK

name age add_value

tom 11 12

jerry 12 13

mike 13 14

john 14 15

mary 15 16

Time taken: 0.098 seconds, Fetched: 5 row(s)

  1. 减法(-)、乘法(*)、除法(/)的使用与加法类似,不再赘述了;

  2. 四舍五入round:

hive> select round(1.1), round(1.6);

OK

_c0 _c1

1.0 2.0

Time taken: 0.028 seconds, Fetched: 1 row(s)

  1. 向上取整ceil:

hive> select ceil(1.1);

OK

_c0

2

Time taken: 0.024 seconds, Fetched: 1 row(s)

  1. 向下取整floor:

hive> select floor(1.1);

OK

_c0

1

Time taken: 0.024 seconds, Fetched: 1 row(s)

  1. 平方pow,例如pow(2,3)表示2的三次方,等于8:

hive> select pow(2,3);

OK

_c0

8.0

Time taken: 0.027 seconds, Fetched: 1 row(s)

  1. 取模pmod:

hive> select pmod(10,3);

OK

_c0

1

Time taken: 0.059 seconds, Fetched: 1 row(s)

[](

)字符函数

  1. 转小写lower,转大写upper:

hive> select lower(name), upper(name) from student;

OK

_c0 _c1

tom TOM

jerry JERRY

mike MIKE

john JOHN

mary MARY

Time taken: 0.051 seconds, Fetched: 5 row(s)

  1. 字符串长度length:

hive> select name, length(name) from student;

OK

tom 3

jerry 5

mike 4

john 4

mary 4

Time taken: 0.322 seconds, Fetched: 5 row(s)

  1. 字符串拼接concat:

hive> select concat(“prefix_”, name) from student;

OK

prefix_tom

prefix_jerry

prefix_mike

prefix_john

prefix_mary

Time taken: 0.106 seconds, Fetched: 5 row(s)

  1. 子串substr,substr(xxx,2)表示从第二位开始到右边所有,substr(xxx,2,3)表示从第二位开始取三个字符:

hive> select substr(“0123456”,2);

OK

123456

Time taken: 0.067 seconds, Fetched: 1 row(s)

hive> select substr(“0123456”,2,3);

OK

123

Time taken: 0.08 seconds, Fetched: 1 row(s)

  1. 去掉前后空格trim:

hive> select trim(" 123 ");

OK

123

Time taken: 0.065 seconds, Fetched: 1 row(s)

[](

)json处理(get_json_object)

为了使用json处理的函数,先准备一些数据:

  1. 先创建表t15,只有一个字段用于保存字符串:

create table t15(json_raw string)

row format delimited;

  1. 创建t15.txt文件,内容如下:

{“name”:“tom”,“age”:“10”}

{“name”:“jerry”,“age”:“11”}

  1. 加载数据到t15表:

load data

local inpath ‘/home/hadoop/temp/202010/25/015.txt’

into table t15;

  1. 使用get_json_object函数,解析json_raw字段,分别取出指定name和age属性:

select

get_json_object(json_raw, “$.name”),

get_json_object(json_raw, “$.age”)

from t15;

得到结果:

hive> select

get_json_object(json_raw, “$.name”),

get_json_object(json_raw, “$.age”)

from t15;

OK

tom 10

jerry 11

Time taken: 0.081 seconds, Fetched: 2 row(s)

[](

)日期

  1. 获取当前日期current_date:

hive> select current_date();

OK

2020-11-02

Time taken: 0.052 seconds, Fetched: 1 row(s)

  1. 获取当前时间戳current_timestamp:

hive> select current_timestamp();

OK

2020-11-02 10:07:58.967

Time taken: 0.049 seconds, Fetched: 1 row(s)

  1. 获取年份year、月份month、日期day:

hive> select year(current_date()), month(current_date()), day(current_date());

OK

2020 11 2

Time taken: 0.054 seconds, Fetched: 1 row(s)

  1. 另外,year和current_timestamp也能搭配使用:

hive> select year(current_timestamp()), month(current_timestamp()), day(current_timestamp());

OK

2020 11 2

Time taken: 0.042 seconds, Fetched: 1 row(s)

  1. 返回日期部分to_date:

hive> select to_date(current_timestamp());

OK

2020-11-02

Time taken: 0.051 seconds, Fetched: 1 row(s)

[](

)条件函数

  • 条件函数的作用和java中的switch类似,语法是case X when XX then XXX else XXXX end;

  • 示例如下,作用是判断name字段,如果等于tom就返回tom_case,如果等于jerry就返回jerry_case,其他情况都返回other_case:

select name,

case name when ‘tom’ then ‘tom_case’

when ‘jerry’ then ‘jerry_case’

else ‘other_case’

end

from student;

结果如下:

hive> select name,

case name when ‘tom’ then ‘tom_case’

      when 'jerry' then 'jerry_case'
      else 'other_case'

end

from student;

OK

tom tom_case

jerry jerry_case

mike other_case

john other_case

mary other_case

Time taken: 0.08 seconds, Fetched: 5 row(s)

[](

)聚合函数

  1. 返回行数count:

select count(*) from student;

触发MR,结果如下:

最后

以上就是秀丽大神为你收集整理的hive学习笔记之七:内置函数,mybatis防止sql注入原理的全部内容,希望文章能够帮你解决hive学习笔记之七:内置函数,mybatis防止sql注入原理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部