Spark sql 函数使用

!
! expr - Logical not.



%
expr1 % expr2 - Returns the remainder after expr1/expr2.

Examples:

> SELECT 2 % 1.8;
 0.2
> SELECT MOD(2, 1.8);
 0.2


&
expr1 & expr2 - Returns the result of bitwise AND of expr1 and expr2.

Examples:

> SELECT 3 & 5;
 1


*
expr1 * expr2 - Returns expr1*expr2.

Examples:

> SELECT 2 * 3;
 6


+
expr1 + expr2 - Returns expr1+expr2.

Examples:

> SELECT 1 + 2;
 3


-
expr1 - expr2 - Returns expr1-expr2.

Examples:

> SELECT 2 - 1;
 1


/
expr1 / expr2 - Returns expr1/expr2. It always performs floating point division.

Examples:

> SELECT 3 / 2;
 1.5
> SELECT 2L / 2L;
 1.0


<
expr1 < expr2 - Returns true if expr1 is less than expr2.

Arguments:

expr1, expr2 - the two expressions must be same type or can be casted to a common type, and must be a type that can be ordered. For example, map type is not orderable, so it is not supported. For complex types such array/struct, the data types of fields must be orderable.
Examples:

> SELECT 1 < 2;
 true
> SELECT 1.1 < '1';
 false
> SELECT to_date('2009-07-30 04:17:52') < to_date('2009-07-30 04:17:52');
 false
> SELECT to_date('2009-07-30 04:17:52') < to_date('2009-08-01 04:17:52');
 true
> SELECT 1 < NULL;
 NULL


<=
expr1 <= expr2 - Returns true if expr1 is less than or equal to expr2.

Arguments:

expr1, expr2 - the two expressions must be same type or can be casted to a common type, and must be a type that can be ordered. For example, map type is not orderable, so it is not supported. For complex types such array/struct, the data types of fields must be orderable.
Examples:

> SELECT 2 <= 2;
 true
> SELECT 1.0 <= '1';
 true
> SELECT to_date('2009-07-30 04:17:52') <= to_date('2009-07-30 04:17:52');
 true
> SELECT to_date('2009-07-30 04:17:52') <= to_date('2009-08-01 04:17:52');
 true
> SELECT 1 <= NULL;
 NULL


<=>
expr1 <=> expr2 - Returns same result as the EQUAL(=) operator for non-null operands, but returns true if both are null, false if one of the them is null.

Arguments:

expr1, expr2 - the two expressions must be same type or can be casted to a common type, and must be a type that can be used in equality comparison. Map type is not supported. For complex types such array/struct, the data types of fields must be orderable.
Examples:

> SELECT 2 <=> 2;
 true
> SELECT 1 <=> '1';
 true
> SELECT true <=> NULL;
 false
> SELECT NULL <=> NULL;
 true


=
expr1 = expr2 - Returns true if expr1 equals expr2, or false otherwise.

Arguments:

expr1, expr2 - the two expressions must be same type or can be casted to a common type, and must be a type that can be used in equality comparison. Map type is not supported. For complex types such array/struct, the data types of fields must be orderable.
Examples:

> SELECT 2 = 2;
 true
> SELECT 1 = '1';
 true
> SELECT true = NULL;
 NULL
> SELECT NULL = NULL;
 NULL


==
expr1 == expr2 - Returns true if expr1 equals expr2, or false otherwise.

Arguments:

expr1, expr2 - the two expressions must be same type or can be casted to a common type, and must be a type that can be used in equality comparison. Map type is not supported. For complex types such array/struct, the data types of fields must be orderable.
Examples:

> SELECT 2 == 2;
 true
> SELECT 1 == '1';
 true
> SELECT true == NULL;
 NULL
> SELECT NULL == NULL;
 NULL


>
expr1 > expr2 - Returns true if expr1 is greater than expr2.

Arguments:

expr1, expr2 - the two expressions must be same type or can be casted to a common type, and must be a type that can be ordered. For example, map type is not orderable, so it is not supported. For complex types such array/struct, the data types of fields must be orderable.
Examples:

> SELECT 2 > 1;
 true
> SELECT 2 > '1.1';
 true
> SELECT to_date('2009-07-30 04:17:52') > to_date('2009-07-30 04:17:52');
 false
> SELECT to_date('2009-07-30 04:17:52') > to_date('2009-08-01 04:17:52');
 false
> SELECT 1 > NULL;
 NULL


>=
expr1 >= expr2 - Returns true if expr1 is greater than or equal to expr2.

Arguments:

expr1, expr2 - the two expressions must be same type or can be casted to a common type, and must be a type that can be ordered. For example, map type is not orderable, so it is not supported. For complex types such array/struct, the data types of fields must be orderable.
Examples:

> SELECT 2 >= 1;
 true
> SELECT 2.0 >= '2.1';
 false
> SELECT to_date('2009-07-30 04:17:52') >= to_date('2009-07-30 04:17:52');
 true
> SELECT to_date('2009-07-30 04:17:52') >= to_date('2009-08-01 04:17:52');
 false
> SELECT 1 >= NULL;
 NULL


^
expr1 ^ expr2 - Returns the result of bitwise exclusive OR of expr1 and expr2.

Examples:

> SELECT 3 ^ 5;
 2


abs
abs(expr) - Returns the absolute value of the numeric value.

Examples:

> SELECT abs(-1);
 1


acos
acos(expr) - Returns the inverse cosine (a.k.a. arccosine) of expr if -1<=expr<=1 or NaN otherwise.

Examples:

> SELECT acos(1);
 0.0
> SELECT acos(2);
 NaN


add_months
add_months(start_date, num_months) - Returns the date that is num_months after start_date.

Examples:

> SELECT add_months('2016-08-31', 1);
 2016-09-30
Since: 1.5.0



and
expr1 and expr2 - Logical AND.



approx_count_distinct
approx_count_distinct(expr[, relativeSD]) - Returns the estimated cardinality by HyperLogLog++. relativeSD defines the maximum estimation error allowed.



approx_percentile
approx_percentile(col, percentage [, accuracy]) - Returns the approximate percentile value of numeric column col at the given percentage. The value of percentage must be between 0.0 and 1.0. The accuracy parameter (default: 10000) is a positive numeric literal which controls approximation accuracy at the cost of memory. Higher value of accuracy yields better accuracy, 1.0/accuracy is the relative error of the approximation. When percentage is an array, each value of the percentage array must be between 0.0 and 1.0. In this case, returns the approximate percentile array of column col at the given percentage array.

Examples:

> SELECT approx_percentile(10.0, array(0.5, 0.4, 0.1), 100);
 [10.0,10.0,10.0]
> SELECT approx_percentile(10.0, 0.5, 100);
 10.0


array
array(expr, ...) - Returns an array with the given elements.

Examples:

> SELECT array(1, 2, 3);
 [1,2,3]


array_contains
array_contains(array, value) - Returns true if the array contains the value.

Examples:

> SELECT array_contains(array(1, 2, 3), 2);
 true


ascii
ascii(str) - Returns the numeric value of the first character of str.

Examples:

> SELECT ascii('222');
 50
> SELECT ascii(2);
 50


asin
asin(expr) - Returns the inverse sine (a.k.a. arcsine) the arc sin of expr if -1<=expr<=1 or NaN otherwise.

Examples:

> SELECT asin(0);
 0.0
> SELECT asin(2);
 NaN


assert_true
assert_true(expr) - Throws an exception if expr is not true.

Examples:

> SELECT assert_true(0 < 1);
 NULL


atan
atan(expr) - Returns the inverse tangent (a.k.a. arctangent).

Examples:

> SELECT atan(0);
 0.0


atan2
atan2(expr1, expr2) - Returns the angle in radians between the positive x-axis of a plane and the point given by the coordinates (expr1, expr2).

Examples:

> SELECT atan2(0, 0);
 0.0


avg
avg(expr) - Returns the mean calculated from values of a group.



base64
base64(bin) - Converts the argument from a binary bin to a base 64 string.

Examples:

> SELECT base64('Spark SQL');
 U3BhcmsgU1FM


bigint
bigint(expr) - Casts the value expr to the target data type bigint.



bin
bin(expr) - Returns the string representation of the long value expr represented in binary.

Examples:

> SELECT bin(13);
 1101
> SELECT bin(-13);
 1111111111111111111111111111111111111111111111111111111111110011
> SELECT bin(13.3);
 1101


binary
binary(expr) - Casts the value expr to the target data type binary.



bit_length
bit_length(expr) - Returns the bit length of string data or number of bits of binary data.

Examples:

> SELECT bit_length('Spark SQL');
 72


boolean
boolean(expr) - Casts the value expr to the target data type boolean.



bround
bround(expr, d) - Returns expr rounded to d decimal places using HALF_EVEN rounding mode.

Examples:

> SELECT bround(2.5, 0);
 2.0


cast
cast(expr AS type) - Casts the value expr to the target data type type.

Examples:

> SELECT cast('10' as int);
 10


cbrt
cbrt(expr) - Returns the cube root of expr.

Examples:

> SELECT cbrt(27.0);
 3.0


ceil
ceil(expr) - Returns the smallest integer not smaller than expr.

Examples:

> SELECT ceil(-0.1);
 0
> SELECT ceil(5);
 5


ceiling
ceiling(expr) - Returns the smallest integer not smaller than expr.

Examples:

> SELECT ceiling(-0.1);
 0
> SELECT ceiling(5);
 5


char
char(expr) - Returns the ASCII character having the binary equivalent to expr. If n is larger than 256 the result is equivalent to chr(n % 256)

Examples:

> SELECT char(65);
 A


char_length
char_length(expr) - Returns the character length of string data or number of bytes of binary data. The length of string data includes the trailing spaces. The length of binary data includes binary zeros.

Examples:

> SELECT char_length('Spark SQL ');
 10
> SELECT CHAR_LENGTH('Spark SQL ');
 10
> SELECT CHARACTER_LENGTH('Spark SQL ');
 10


character_length
character_length(expr) - Returns the character length of string data or number of bytes of binary data. The length of string data includes the trailing spaces. The length of binary data includes binary zeros.

Examples:

> SELECT character_length('Spark SQL ');
 10
> SELECT CHAR_LENGTH('Spark SQL ');
 10
> SELECT CHARACTER_LENGTH('Spark SQL ');
 10


chr
chr(expr) - Returns the ASCII character having the binary equivalent to expr. If n is larger than 256 the result is equivalent to chr(n % 256)

Examples:

> SELECT chr(65);
 A


coalesce
coalesce(expr1, expr2, ...) - Returns the first non-null argument if exists. Otherwise, null.

Examples:

> SELECT coalesce(NULL, 1, NULL);
 1


collect_list
collect_list(expr) - Collects and returns a list of non-unique elements.



collect_set
collect_set(expr) - Collects and returns a set of unique elements.



concat
concat(str1, str2, ..., strN) - Returns the concatenation of str1, str2, ..., strN.

Examples:

> SELECT concat('Spark', 'SQL');
 SparkSQL


concat_ws
concat_ws(sep, [str | array(str)]+) - Returns the concatenation of the strings separated by sep.

Examples:

> SELECT concat_ws(' ', 'Spark', 'SQL');
  Spark SQL


conv
conv(num, from_base, to_base) - Convert num from from_base to to_base.

Examples:

> SELECT conv('100', 2, 10);
 4
> SELECT conv(-10, 16, -10);
 -16


corr
corr(expr1, expr2) - Returns Pearson coefficient of correlation between a set of number pairs.



cos
cos(expr) - Returns the cosine of expr.

Examples:

> SELECT cos(0);
 1.0


cosh
cosh(expr) - Returns the hyperbolic cosine of expr.

Examples:

> SELECT cosh(0);
 1.0


cot
cot(expr) - Returns the cotangent of expr.

Examples:

> SELECT cot(1);
 0.6420926159343306


count
count(*) - Returns the total number of retrieved rows, including rows containing null.

count(expr) - Returns the number of rows for which the supplied expression is non-null.

count(DISTINCT expr[, expr...]) - Returns the number of rows for which the supplied expression(s) are unique and non-null.



count_min_sketch
count_min_sketch(col, eps, confidence, seed) - Returns a count-min sketch of a column with the given esp, confidence and seed. The result is an array of bytes, which can be deserialized to a CountMinSketch before usage. Count-min sketch is a probabilistic data structure used for cardinality estimation using sub-linear space.



covar_pop
covar_pop(expr1, expr2) - Returns the population covariance of a set of number pairs.



covar_samp
covar_samp(expr1, expr2) - Returns the sample covariance of a set of number pairs.



crc32
crc32(expr) - Returns a cyclic redundancy check value of the expr as a bigint.

Examples:

> SELECT crc32('Spark');
 1557323817


cube


cume_dist
cume_dist() - Computes the position of a value relative to all values in the partition.



current_database
current_database() - Returns the current database.

Examples:

> SELECT current_database();
 default


current_date
current_date() - Returns the current date at the start of query evaluation.

Since: 1.5.0



current_timestamp
current_timestamp() - Returns the current timestamp at the start of query evaluation.

Since: 1.5.0



date
date(expr) - Casts the value expr to the target data type date.



date_add
date_add(start_date, num_days) - Returns the date that is num_days after start_date.

Examples:

> SELECT date_add('2016-07-30', 1);
 2016-07-31
Since: 1.5.0



date_format
date_format(timestamp, fmt) - Converts timestamp to a value of string in the format specified by the date format fmt.

Examples:

> SELECT date_format('2016-04-08', 'y');
 2016
Since: 1.5.0



date_sub
date_sub(start_date, num_days) - Returns the date that is num_days before start_date.

Examples:

> SELECT date_sub('2016-07-30', 1);
 2016-07-29
Since: 1.5.0



date_trunc
date_trunc(fmt, ts) - Returns timestamp ts truncated to the unit specified by the format model fmt. fmt should be one of ["YEAR", "YYYY", "YY", "MON", "MONTH", "MM", "DAY", "DD", "HOUR", "MINUTE", "SECOND", "WEEK", "QUARTER"]

Examples:

> SELECT date_trunc('2015-03-05T09:32:05.359', 'YEAR');
 2015-01-01T00:00:00
> SELECT date_trunc('2015-03-05T09:32:05.359', 'MM');
 2015-03-01T00:00:00
> SELECT date_trunc('2015-03-05T09:32:05.359', 'DD');
 2015-03-05T00:00:00
> SELECT date_trunc('2015-03-05T09:32:05.359', 'HOUR');
 2015-03-05T09:00:00
Since: 2.3.0



datediff
datediff(endDate, startDate) - Returns the number of days from startDate to endDate.

Examples:

> SELECT datediff('2009-07-31', '2009-07-30');
 1

> SELECT datediff('2009-07-30', '2009-07-31');
 -1
Since: 1.5.0



day
day(date) - Returns the day of month of the date/timestamp.

Examples:

> SELECT day('2009-07-30');
 30
Since: 1.5.0



dayofmonth
dayofmonth(date) - Returns the day of month of the date/timestamp.

Examples:

> SELECT dayofmonth('2009-07-30');
 30
Since: 1.5.0



dayofweek
dayofweek(date) - Returns the day of the week for date/timestamp (1 = Sunday, 2 = Monday, ..., 7 = Saturday).

Examples:

> SELECT dayofweek('2009-07-30');
 5
Since: 2.3.0



dayofyear
dayofyear(date) - Returns the day of year of the date/timestamp.

Examples:

> SELECT dayofyear('2016-04-09');
 100
Since: 1.5.0



decimal
decimal(expr) - Casts the value expr to the target data type decimal.



decode
decode(bin, charset) - Decodes the first argument using the second argument character set.

Examples:

> SELECT decode(encode('abc', 'utf-8'), 'utf-8');
 abc


degrees
degrees(expr) - Converts radians to degrees.

Examples:

> SELECT degrees(3.141592653589793);
 180.0


dense_rank
dense_rank() - Computes the rank of a value in a group of values. The result is one plus the previously assigned rank value. Unlike the function rank, dense_rank will not produce gaps in the ranking sequence.



double
double(expr) - Casts the value expr to the target data type double.



e
e() - Returns Euler's number, e.

Examples:

> SELECT e();
 2.718281828459045


elt
elt(n, input1, input2, ...) - Returns the n-th input, e.g., returns input2 when n is 2.

Examples:

> SELECT elt(1, 'scala', 'java');
 scala


encode
encode(str, charset) - Encodes the first argument using the second argument character set.

Examples:

> SELECT encode('abc', 'utf-8');
 abc

spark sql 函数详见
https://spark.apache.org/docs/2.3.0/api/sql/#ltrim

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,530评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,403评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,120评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,770评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,758评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,649评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,021评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,675评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,931评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,751评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,410评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,004评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,969评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,042评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,493评论 2 343

推荐阅读更多精彩内容