📜  HiveQL-函数

📅  最后修改于: 2020-12-03 03:52:36             🧑  作者: Mango

HiveQL-功能

Hive提供了各种内置函数来执行数学和聚合类型运算。在这里,我们将在下表的记录上执行此类功能:

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中的数学函数示例

  • 让我们看一个获取每个员工薪水平方根的示例。
hive> select Id, Name, sqrt(Salary) from employee_data ;  

Hive中的汇总函数

在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中的聚合函数示例

  • 让我们看一个获取员工最高薪水的示例。
hive> select max(Salary) from employee_data;



  • 让我们看一个获取员工最低工资的示例。
hive> select min(Salary) from employee_data;



Hive中的其他内置函数

以下是配置单元中其他一些常用的内置函数:-

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.

Hive中其他内置函数的示例

  • 让我们看一个示例,以大写形式获取每个员工的姓名。
select Id, upper(Name) from employee_data;

  • 让我们看一个示例,以小写形式获取每个员工的姓名。
select Id, lower(Name) from employee_data;