📅  最后修改于: 2020-12-03 03:52:36             🧑  作者: Mango
Hive提供了各种内置函数来执行数学和聚合类型运算。在这里,我们将在下表的记录上执行此类功能:
让我们创建一个表,并使用以下步骤将数据加载到其中:-
hive> use hql;
hive> create table employee_data (Id int, Name string , Salary float)
row format delimited
fields terminated by ',' ;
hive> load data local inpath '/home/codegyani/hive/emp_details' into table employee_data;
hive> select * from employee_data;
现在,我们将通过相应的示例讨论数学,集合函数和其他内置函数。
蜂巢中常用的数学函数是:-
Return type | Functions | Description |
---|---|---|
BIGINT | round(num) | It returns the BIGINT for the rounded value of DOUBLE num. |
BIGINT | floor(num) | It returns the largest BIGINT that is less than or equal to num. |
BIGINT | ceil(num), ceiling(DOUBLE num) | It returns the smallest BIGINT that is greater than or equal to num. |
DOUBLE | exp(num) | It returns exponential of num. |
DOUBLE | ln(num) | It returns the natural logarithm of num. |
DOUBLE | log10(num) | It returns the base-10 logarithm of num. |
DOUBLE | sqrt(num) | It returns the square root of num. |
DOUBLE | abs(num) | It returns the absolute value of num. |
DOUBLE | sin(d) | It returns the sin of num, in radians. |
DOUBLE | asin(d) | It returns the arcsin of num, in radians. |
DOUBLE | cos(d) | It returns the cosine of num, in radians. |
DOUBLE | acos(d) | It returns the arccosine of num, in radians. |
DOUBLE | tan(d) | It returns the tangent of num, in radians. |
DOUBLE | atan(d) | It returns the arctangent of num, in radians. |
hive> select Id, Name, sqrt(Salary) from employee_data ;
在Hive中,聚合函数返回一个由许多行计算得出的值。我们来看一些常用的聚合函数:-
Return Type | Operator | Description |
---|---|---|
BIGINT | count(*) | It returns the count of the number of rows present in the file. |
DOUBLE | sum(col) | It returns the sum of values. |
DOUBLE | sum(DISTINCT col) | It returns the sum of distinct values. |
DOUBLE | avg(col) | It returns the average of values. |
DOUBLE | avg(DISTINCT col) | It returns the average of distinct values. |
DOUBLE | min(col) | It compares the values and returns the minimum one form it. |
DOUBLE | max(col) | It compares the values and returns the maximum one form it. |
hive> select max(Salary) from employee_data;
hive> select min(Salary) from employee_data;
以下是配置单元中其他一些常用的内置函数:-
Return Type | Operator | Description |
---|---|---|
INT | length(str) | It returns the length of the string. |
STRING | reverse(str) | It returns the string in reverse order. |
STRING | concat(str1, str2, …) | It returns the concatenation of two or more strings. |
STRING | substr(str, start_index) | It returns the substring from the string based on the provided starting index. |
STRING | substr(str, int start, int length) | It returns the substring from the string based on the provided starting index and length. |
STRING | upper(str) | It returns the string in uppercase. |
STRING | lower(str) | It returns the string in lowercase. |
STRING | trim(str) | It returns the string by removing whitespaces from both the ends. |
STRING | ltrim(str) | It returns the string by removing whitespaces from left-hand side. |
TRING | rtrim(str) | It returns the string by removing whitespaces from right-hand side. |
select Id, upper(Name) from employee_data;
select Id, lower(Name) from employee_data;