Sunday, 26 November 2017

Word Count in Pig

After the word count in Hive, we will proceed to word count in Pig. For all queries in this post, we will use the Cloudera sandbox, Cloudera QuickStart VM 5.12.

We will use the same file that we used in the earlier word count exercise. The path to the file is /user/hive/warehouse/text/blue_carbuncle.txt

Navigate to the Pig Editor as shown below:






















The first step is the below command:

data = LOAD '/user/hive/warehouse/text/blue_carbuncle.txt' as (text:CHARARRAY);

The above command loads the data in the file into a field called text that is of type CHARARRAY. Once the data is loaded, we can run below command to describe the schema.

DESCRIBE data;

The results returned are shown below on line 17:







The next line is shown below:

words = FOREACH data GENERATE (TOKENIZE(text)) AS word;

FOREACH applies that the operation that follows on every element of input, data in our case.

GENERATE generates the records or the output from the input on the left.

TOKENIZE splits a string input into a bag of words based on word separators, space in our case.

The output at the end of this operation is shown below from line 93:











We can see that based on the space the words are separated. But, we need to put the words on different rows so that that can counted like we did in case of HIVE. So, we use the word FLATTEN as shown below:

words = FOREACH data GENERATE FLATTEN(TOKENIZE(text)) AS word;

The output at the end of this operation is shown below from line 93:











The next task is to group the words using below command:

grpd  = GROUP words BY word;

The output at the end of this operation is shown below from line 96:










Lastly, we need to add the count as shown below:

cntd  = FOREACH grpd GENERATE group, COUNT(words);

The output at the end of this operation is shown below from line 96:














The overall code is:

data = LOAD '/user/hive/warehouse/text/blue_carbuncle.txt' as (text:CHARARRAY);

words = FOREACH data GENERATE FLATTEN(TOKENIZE(text)) AS word;

grpd  = GROUP words BY word;

cntd  = FOREACH grpd GENERATE group, COUNT(words);

DUMP cntd;


The DUMP command outputs the result.

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.