Monday, 20 November 2017

Analytic Functions in Hive - V

In the following post, we will take a look at Regular Expressions as is dealt by Hive. We will follow the post that we wrote before. For all our work, we will use the Mapr sandbox, MapR-Sandbox-For-Hadoop-5.2.0.


A Regular Expression is a special text used as a search pattern. In other words, a Regular Expression describes a pattern that can be used for searching text in strings. They are specified as a combination of Metacharacters and Literals. These are described below:

1) Metacharacters: These are operators like *,?,[], etc, that are used to create the algorithm to be used in the search process

2) Literals: These are the actual characters or content that are searched for

The syntax followed by Hive is described here.

Hive has the following functions to support Regular Expressions: RLIKE, REGEXP, REGEXP_EXTRACT, and REGEXP_REPLACE.

In the first query, we will use RLIKE as seen in the example below:

Use of '.':

select first_name, last_name from employees where rlike (last_name, 'A....n');

The results returned are shown below:











The '.' metacharacter matches any character. So, since there are 4 of those, the query matches any word that starts with A and ends with n and has 4 characters in between.

Use of '+':


The second query makes use of REGEXP. REGEXP is synonymous with RLIKE. 

select first_name, last_name from employees where regexp(last_name, '(rr)+');

The results returned are shown below: 











'+' matches one or more of the preceding subexpression. 

Use of '?':

select first_name, last_name from employees where rlike(last_name, '(at?a)');

The results returned are shown below:  

 










'?' matches zero or one of the preceding subexpression. De Haan is selected because there is no t in between in aa. Pataballa is selected because there is one occurrence of t between aa.

Use of '*':

select first_name, last_name from employees where rlike (last_name, 'al*a');


The results returned are shown below: 













'*' matches zero or more of the preceding subexpression.

Use of '{}':

select first_name, last_name from employees where regexp_like (last_name, 'l{2}');

The results returned are shown below:



















This query returns last_name containing l character two times.

select first_name, last_name from employees where regexp(last_name, 'a{2,}');

The results returned are shown below:









This query returns all employees having last_names having 'a' two times or more.

select first_name, last_name from employees where rlike(last_name, 'h{1,2}');

The results returned are shown below:

















The last query returns all employees having last_names having at least one h but not more than two h.

Use of '[]':

select first_name, last_name from employees where rlike(last_name, '[zx]');

The results returned are shown below:












The last query returns first_name, last_name from all employees having last_name containing x, or z.


Use of '[ ]':

select first_name, last_name from employees where rlike(last_name, '[ ]');

The results returned are shown below:










The last query returns first_name, last_name from all employees having last_name containing a space.

The same result can be obtained by below query:

select first_name, last_name from employees where rlike(last_name, '\\s');

Use of '\1':

'\1' is called back reference as it will call the first search criteria that has been called before. In this case, it is a vowel. So, a repeat is sought.

select last_name from employees where rlike(last_name, '([aeiou])\\1');

The results returned are shown below:
















The last query returns last_name containing two consecutive characters mentioned in [].

Next, we look at REGEXP_EXTRACT that helps with extracting parts from a field that meet certain criteria as shown in below example:

select phone_number, regexp_extract(phone_number, '590.423.(.*)',0) from employees order by regexp_extract(phone_number, '590.423.(.*)',0) desc limit 7;

The results returned are shown below:











The first parameter is the field itself, phone_number. The second argument is the pattern that provides the search expression. The third argument has 0 and so results that match entire search criteria are returned. If the search criteria is not met, nulls are returned. If the third argument is set to 1, then, only results that pertain to the group, (.*) in our case is returned back. This is shown in following query:

select phone_number, regexp_extract(phone_number, '590.423.(.*)',1) from employees order by regexp_extract(phone_number, '590.423.(.*)',1) desc limit 7;

The results returned are shown below:











The last function, REGEXP_REPLACE, can be used to replace characters that satisfy certain pattern matches as shown below:

select last_name, phone_number, regexp_replace(phone_number, '([/.])', ' ') as modified_phone_number from employees where rlike(last_name, 'K');
 
The results returned are shown below:










REGEXP_REPLACE function replaces '.' character in phone_number with a space character.

With the query on REGEXP_REPLACE, we conclude the topic of Regular Expressions in Hive.

Sunday, 19 November 2017

Analytic Functions in Hive - IV

In the following post, we will take a look at a new set of analytic functions continuing in the series. We will follow the post that we wrote before. For all our work, we will use the Mapr sandbox, MapR-Sandbox-For-Hadoop-5.2.0.

We will look at LAG and LEAD functions. We will use the results of the below query to understand these two functions:

select * from employees where employee_id <= 109 order by salary desc;

The results returned are shown below:  











The two functions are dependent on the order of the rows. If the order of the rows are changed, then, the values returned by these functions also will change as we will see in the queries shortly. LAG and LEAD functions return values from previous of following rows. The offset determines the row that is selected from which the value will be fetched. If no offset is specified, then, value from the immediate previous or immediate following row depending on whether LAG or LEAD function is used, respectively. If no value is available because previous row or next row is non-existent, then NULL, is returned. To avoid NULL values being returned, we can specify default value so that a meaningful result is returned.

As an example, let us run below query:

select employee_id, last_name, hire_date, department_id, salary, lag(salary) over (order by salary desc) as PREVIOUS_SALARY from employees where employee_id <= 109;

The results returned are shown below:  



The second row and further rows correctly carry the salary from the previous row. Two things are to be noted. The first row has NULL for previous salary as there there is no previous row from which salary can be fetched. NULL value is returned as we have not set a default value to LAG function.  Below query corrects both the issues where 0 is returned instead of NULL:

select employee_id, last_name, hire_date, department_id, salary, lag(salary,1,0) over (order by salary desc) as PREVIOUS_SALARY from employees where employee_id <= 109;

The results returned are shown below:











If, however, instead of 1 as offset, we use 0 with no default for NULL value as shown below, the PREVIOUS SALARY column shows the current salary. This is only to be expected.

select employee_id, last_name, hire_date, department_id, salary, lag(salary,0) over (order by salary desc) as PREVIOUS_SALARY from employees where employee_id <= 109;

The results returned are shown below: 












 The next query shows a offset value of 2 using LEAD function but with a different sorting on order.

select employee_id, last_name, hire_date, department_id, salary, lead(salary,2,0) over (order by salary) as FOLLOWING_SALARY from employees where employee_id <= 109;


The results returned are shown below:












 Finally, we introduce the PARTITION keyword in the clause following OVER as shown below:

select employee_id, last_name, hire_date, department_id, salary, lag(salary) over (partition by department_id order by salary desc) as PREVIOUS_SALARY from employees where employee_id <= 109;

The results returned are shown below:










The results are neatly partitioned by department_id.

Thursday, 16 November 2017

Analytic Functions in Hive - III

We will now take a look at more analytic functions continuing in the series. We will follow the post that we wrote before. For all our work, we will use the Mapr sandbox, MapR-Sandbox-For-Hadoop-5.2.0.

RANK as the name suggests ranks any measure in a query using sequential numbers starting from 1. If in the ranking process, a tie is encountered, then, the same rank is repeated for all values that tie and the next rank in sequence is used after skipping as many rank values as was repeated in the tie . Let us see that by means of an example. For starters, let us use the same hr.employees table used in the previous posts. We are interested only in the results of the below query:

select employee_id, last_name, department_id, salary from employees where employee_id <= 109 order by salary desc;

The results returned are shown below:  













The query returns ten records. Let us now run the first query using RANK shown below:

select employee_id, last_name, department_id, salary, rank() over (order by salary desc) as SALARY_RANK from employees where employee_id <= 109;

The results returned are shown below:  













The results are as written above: a tie is seen at 17000, so there is a repeat of ranks for 17000 and at the next value of 12008, rank value 3 is skipped for one repeat of 17000 and a rank of 4 is returned. Same is true for the salary value 9000 where rank of 6 is skipped.

DENSE_RANK is similar to RANK except that in case a of tie, the rank values are not skipped. Let us replace RANK with DENSE_RANK in above query to see this contrast.

select employee_id, last_name, department_id, salary, dense_rank() over (order by salary desc) as SALARY_DENSE_RANK from employees where employee_id <= 109;

The results returned are shown below: 













Results show that DENSE_RANK does not skip any values at all and ranks are repeated at ties. 

Let us add the partition keyword and see the results. The query using partition is show below:

select department_id, last_name, salary, rank() over (partition by department_id order by salary desc) as SALARY_RANK from employees where employee_id <= 109;

The results returned are shown below:













The results show that the RANK function is applied department wise. The same can be tried using DENSE_RANK as shown below:

select department_id, last_name, salary, dense_rank() over (partition by department_id order by salary desc)  as SALARY_DENSE_RANK from employees where employee_id <= 109;

The results returned are shown below:












The results are clearly in line with our understanding of DENSE_RANK as is seen in the ranking of Lorentz.

We will conclude this post with a query for 5 salaries as shown below:

select employee_id, last_name, department_id, salary from employees where employee_id <= 109 order by salary desc limit 5; 

The results returned are shown below: