Saturday, 25 November 2017

Word Count in Hive

In this post, we will take a look at word count in Hive. This post is the culmination of the posts that we have written earlier on Analytic Functions in Hive. For all queries in this post, we will use the Cloudera sandbox, Cloudera QuickStart VM 5.12.

We begin by creating an external table that will have the text data on which we wish to do a word count. The table will have just one column of data type, STRING. The command to create the table is shown below:

create external table text (line string);

The screenshot is shown below:












Once the table is created, we can import data into this table. We will import a .txt file that has the first few paragraphs of the short story, The Adventure of the Blue Carbuncle, by Sir Arthur Conan Doyle.

To port data into this external table, right click on table name on left as shown below:
















In the next screen, click on Import Data as shown below:












In the dialog box, click Upload a file as shown below:
























Select the file once uploaded and click on Submit button:















Click on Sample tab to see the data loaded as shown below:




















Navigate back to Hive Editor as shown below:

























Run the below query to see the position of the word on each line and the word itself:

select posexplode(split(line, '\\s')) as (position, word) from text; 

The results returned are shown below:
























Running below query returns the words in rows:

select explode(split(line,'\\s')) as word from text; 

The results returned are shown below:

























Adding a count as shown below returns the word count:

select word, count(*) from(select explode(split(line,'\\s')) as word from text) z group by word;

The results returned are shown below:



















Counting only those words that begin with a capital letter:

select word, count(*) from(select explode(split(line,'\\s')) as word from text) z where regexp(word, '^[A-Z]') group by word; 

The results returned are shown below:
















The last query returns the top three words that begin with a capital letter occurring in the text:

select word, count(*) as word_count from(select explode(split(line,'\\s')) as word from text) z where regexp(word, '^[A-Z]') group by word order by word_count desc limit 3;

The results returned are shown below:



Tuesday, 21 November 2017

Analytic Functions in Hive - VI

In the following post, we will take a look at a case study similar to word count in Hive. For all our work, we will use the Mapr sandbox, MapR-Sandbox-For-Hadoop-5.2.0.

Before we proceed to  the case study, let us take a look at EXPLODE and POSEXPLODE.

We will run below query to understand EXPLODE:

select explode(array('Hello','Big','Data'));

The results returned are shown below:














As the name suggests, EXPLODE decomposes the elements in an array into elements in multiple rows. POSEXPLODE is the same as EXPLODE but returns another column with positions as well starting from 0.

select posexplode(array('Hello','Big','Data')); 

The results returned are shown below:











For the case study, the data is described by the query below:

select last_name, phone_number from employees where rlike(last_name, 'K');

The results returned are shown below: 













The goal is to count the number of elements in the phone numbers after they are split into different elements using '.' as a delimiter. As an example, '515' should have a count of 3 while '615' should have a count of 1 only.

As a first step, we remove the double quotes in the numbers with below query:

select regexp_replace(phone_number, '([/"])', '') as part_number from employees where rlike(last_name, 'K');

The results returned are shown below: 













We then split the result into an array having different phone number parts based on delimiter "." as shown below:

select split(regexp_replace(phone_number, '([/"])', ''),'\\.') as part_number from employees where rlike(last_name, 'K');

The results returned are shown below: 













We then use EXPLODE to move the different parts of the array to multiple rows as shown below: 

select explode(split(regexp_replace(phone_number, '([/"])', ''),'\\.')) as part_number from employees where rlike(last_name, 'K');

The results returned are shown below:


























Now, the part numbers are into different rows. We just need to do a group by to complete the part number count as shown below:

select part_number, count(*) as count_number from (select explode(split(regexp_replace(phone_number, '([/"])', ''),'\\.')) as part_number from employees where rlike(last_name, 'K')) w group by part_number;

The results returned are shown below: 















This shows the count of the different part numbers. To get the total number of part numbers, use below query:

select sum(count_number) from (select part_number, count(*) as count_number from (select explode(split(regexp_replace(phone_number, '([/"])', ''),'\\.')) as part_number from employees where rlike(last_name, 'K')) w group by part_number) y;

The results returned are shown below:











This concludes the Analytic Functions series on Hive.

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.