Search This Blog

Showing posts with label Apache Pig. Show all posts
Showing posts with label Apache Pig. Show all posts

Checking versions of Hadoop eco system tools


We need to know versions of hadoop technologies while we are trouble shooting any Hadoop issues. This article talks how to check versions of  Hadoop ecosystem technologies.

1) Hadoop version

Hadoop version can be found using below command.

hadoop version




2) Hive version

Hive version can be found using below command.

hive version



3) Pig version

Pig version can be found using below command.

pig version




4) Sqoop version

Sqoop version can be found using below command.

sqoop version



5) Tez version 

Tez version can be found using below rpm command.

rpm -qa|grep -i tez



6) Zookeeper version

Zookeeper version can be found using below rpm command.

rpm -qa|grep -i zookeeper




7) Hortonworks Data Platform  (HDP) version 

HDP version can be found using below command.

hdp-select versions



8) Knox version

Apache Knox version can be found using below rpm command.

rpm -qa|grep -i knox



9) Ranger version

Apache Ranger version can be found using below rpm command.

rpm -qa|grep -i ranger

We can also check version file for ranger version.



10)Checking all  versions from Ambari Web UI

Goto ---> Admin ---> Service accounts and versions



Let me know if you want to know particular Hadoop technology version.


HDFS Commands examples

This article cover simple and useful HDFS commands examples. Apache Hadoop is data processing tool at web scale. Hadoop contains three modules :

HDFS                          ---   Data storage system

MAPREDUCE           ---    Data processing framework

YARN                         ---    Resource management system for Hadoop


We will discuss some commands to learn how to interact with Hadoop distributed File System (HDFS). All hdfs file system commands start with hdfs dfs.  Most of the hadoop distributions  (CDH, HDP) come with standard hdfs user.

1)

Change the current user  root to HDFS user. Mostly hdfs user will be password less user.









2).



Create a file on the local file system using cat command.




3).


Create a directory in HDFS using mkdir command.





4).


Upload local file helloworld to HDFS directory helloworld using put command.



5).


Check file is loaded into hdfs directory helloworld using ls command.







6).


Read hdfs file content using cat command.







7).


Rename HDFS file helloworld to helloworldfile using mv command.









8)

Copy helloworldfile to another hdfs directory using cp command.












9)

Check the size of a file in HDFS using du command.





10).


Check replication factor of a file in HDFS using ls command. Rounded number is the replication factor of the file.



11).

Change the replication factor of a file in HDFS using setrep command.The example below changes replication factor from 3 to 2.







Difference between Apache Hive and Apache Pig

MapReduce follows key-value programming model. It has two core stages Map and Reduce.
Both Map and Reduce have key-value as an input and key-value as an output. To write Map Reduce applications ,we need to know one programming language like Java.
These MapReduce applications will have a Map program , a reduce program and a driver program to run map and reduce programs.We need to create a jar containing these programs to process the data.

This Mapreduce has lengthy development time and may not be suitable for situations like adhoc querying. That is one of the reasons there are so many abstractions available for Mapreduce.
For example Cascading, Apache Crunch, Apache Hive and Apache Pig etc...All of these hide key-value complexity for developer. We will now discuss differences between Apache Hive and Apache Pig.



Apache Hive       VS   Apache Pig






Types of Data they support


Apache Hive :  

Hive is a scalable data warehouse on top of Apache Hadoop. As data is available in tables it only supports structured data . processing semi structured data is difficult and processing unstructured data is very very difficult.

Apache Pig :

Pig is a platform for processing large data sets. Its query language is called Pig latin. Pig latin can process structured ,semi structured and unstructured data.



Programming model


Apache Hive :  Hive query language is declarative programming language. It is not easy to build complex business logic.

Apache Pig : Pig Latin is an imperative programming language , You can easily write complex business logic.


Integration


Apache Hive :   Hive has a component called HCatalog that provides cross platform schema.
It also has Rest API called WebHCatalog. So You can integrate any tool with Apache Hive.
Already Teradata, Aster Data got integrated with apache Hive. Even Pig can process data using WebHCatalog.

Apache Pig : It does not have any such feature. Because it is processing platform not a storage platform.



Debugging


Apache Hive : We can debug hive queries but not that easy.

Apache Pig : Pig Latin is a data flow language It is designed keeping debugging feature in mind.

So We can easily debug Pig Latin scripts.


Learning


Both can be easily learned . Hive is almost same as SQL. Pig Latin also looks like SQL .

One can easily learn hive and start writing queries to process data.

Industry Adoption


Apache Hive : It is more widely used in the industry than Apache Pig. 


Adhoc Querying


Both can be used for adhoc querying Hive is more suitable than Pig if it is structured data.


Complex Business logic


If you have to develop applications that have so much business complexity. It is better to use Apache Pig rather than using Hive.

Pig is widely used in research applications than Hive for the same reason.

Let me know if you want to compare these two for any other use-case.







Error Categories in Apache Pig


When you are working on Apache Pig you might see some error codes with some error description.
I would like to discuss those error codes categories so that It becomes easy to progress  for error resolution.

Apache Pig categorizes error codes into four groups . They are INPUT,BUG,USER ENVIRONMENT and REMOTE ENVIRONMENT.


If error code is greater than or equals to 100 and less than or equals to 1999 ,They fall in INPUT group.

For Example :

Error code 1000 is thrown if Pig Latin script input is not parse-able.

and also Error Code 1005 is thrown when we are trying to describe relation which does not have  a n input schema.

If error codes is greater than or equals to 2000 and less than or equals to 2999. They fall under BUG group. all of these are run time errors .

For example Error Code 2009 is thrown when copy operation is failed .

If error codes is greater than or equals to 3000 and less than or equals to 4999. They fall under USER ENVIRONMENT group.

For example : Error code 4002 is thrown when program fails to read data from a file. because there is a problem in User environment.

If error codes is greater than or equals to 5000 and less than or equals to 6999. They fall under REMOTE ENVIRONMENT group.

For example error code 6002 is thrown when out of memory occurs on cluster.



Word Count in Pig Latin

In this Post, we learn how to write word count program using Pig Latin.

Assume we have data in the file like below.

This is a hadoop post
hadoop is a bigdata technology

and we want to generate output for count of each word like below
 

(a,2)
(is,2)
(This,1)
(class,1)
(hadoop,2)
(bigdata,1)
(technology,1)


Now we will see in steps how to generate the same using Pig latin.

 

1.Load the data from HDFS


Use Load statement to load the data into a relation .
As keyword used to declare column names, as we dont have any columns, we declared only one column named line.
 
input = LOAD '/path/to/file/' AS(line:Chararray);

2. Convert the Sentence into words.


The data we have is in sentences. So we have to convert that data into words using
TOKENIZE Function.


(TOKENIZE(line));
(or)
If we have any delimeter like space we can specify as
(TOKENIZE(line,' '));


Output will be like this:


({(This),(is),(a),(hadoop),(class)})
({(hadoop),(is),(a),(bigdata),(technology)})


but we have to convert it into multiple rows like below


(This)
(is)
(a)
(hadoop)
(class)
(hadoop)
(is)
(a)
(bigdata)
(technology)

 
3.Convert Column into Rows

 
I mean we have to convert every line of data into multiple rows ,for this we have function called
FLATTEN in pig.


Using FLATTEN function the bag is converted into tuple, means the array of strings
converted into multiple rows.


Words = FOREACH input GENERATE FLATTEN(TOKENIZE(line,' ')) AS word;


Then the ouput is like below


(This)
(is)
(a)
(hadoop)
(class)
(hadoop)
(is)
(a)
(bigdata)
(technology)


3. Apply GROUP BY


We have to count each word occurance, for that we have to group all the words.


Grouped = GROUP words BY word;



4. Generate word count 
 
wordcount = FOREACH Grouped GENERATE group, COUNT(words);






We can print the word count on console using Dump.


DUMP wordcount;


Output will be like below.
 

(a,2)
(is,2)
(This,1)
(class,1)
(hadoop,2)
(bigdata,1)
(technology,1)


Below is the complete program for the same.

input = LOAD '/path/to/file/' AS(line:Chararray);
Words = FOREACH input GENERATE FLATTEN(TOKENIZE(line,' ')) AS word;
Grouped = GROUP words BY word;
wordcount = FOREACH Grouped GENERATE group, COUNT(words); 

You may check same word count using Hive .

Writing Macro in Pig Latin

We can develop more reusable scripts in Pig Latin Using Macros also.Macro is a kind of function written in Pig Latin.

We will learn macro in this Post.

We will take sample emp data like below.

eno,ename,sal,dno
10,Balu,10000,15
15,Bala,20000,25
30,Sai,30000,15
40,Nirupam,40000,35

using above data I would like to have employee data who belong to department number 15.

First we will write Piglatin code without using Macro.


1. Example without Macro

Write below code in a file called filterwitoutmacro.pig

emp = load '/data/employee'using PigStorage(',') as (eno,ename,sal,dno);
empdno15 =filter emp by $3==15;
dump empdno15;

run pig latin code from file.

pig -f /path/to/filterwitoutmacro.pig

Now we will create a macro for filter logic

2. Same example with macro

Define is the keyword used to create a macro ,It will also have returns statement.

return relation/varibale declared within macro should be the last variable/relation within macro code.

2.1 Create a macro

DEFINE myfilter(relvar,colvar) returns x{
$x = filter $relvar by $colvar==15;
};

Above macro takes two values as input,one is relation variable (relvar) and second is column variable (colvar)

macro checks if colvar equals to 15 or not.

2.2  Usage of  macro

we can use myfilter macro like below.

emp = load '/data/employee'using PigStorage(',') as (eno,ename,sal,dno);
empdno15 =myfilter( emp,dno);
dump empdno15;

we can write macro creation code and macro usage code in same file ,can run file with -f option.

pig -f /path/to/myfilterwithembeddedmacro.pig



3. same example with external macro.

macro code even be in a separate file ,so that we can use it in different pig latin scripts.

to use external macro file in pig latin code we use IMPORT statement.

3.1 write  above macro in separate file called myfilter.macro

--myfilter.macro
DEFINE myfilter(relvar,colvar) returns x{
$x = filter $relvar by $colvar==15;
}

3.2 Import macro file in another pig latin script file.


IMPORT '/path/to/myfilter.macro'
emp = load '/data/employee'using PigStorage(',') as (eno,ename,sal,dno);
empdno15 =myfilter( emp,dno);
dump empdno15;


and we run pig latin script file using -f option.

pig -f /path/to/myfilterwithexternalmacro.pig


So what is the use of macro ?

we can use macro as many times as we wish and on different inputs also.

for example ,with respect to above example ,If I want to check employee details who has 15 as their employee number.

then we can write pig latin code like below.



IMPORT '/path/to/myfilter.macro'
emp = load '/data/employee'using PigStorage(',') as (eno,ename,sal,dno);
eno15 =myfilter( emp,eno);
dump eno15;


So That concludes ,we can write highly reusable scripts in Pig latin using macros.

also visit pig official documentation on macro.

upto some extent , we can write reusable scripts in Pig latin using parameter substitution also.




Parameter substitution in Pig

Earlier I have discussed about writing reusable scripts using Apache Hive, now we see how to achieve same functionality using Pig Latin.
Pig Latin has an option called param, using this we can write dynamic scripts .

Assume ,we have a file called numbers with below data.
12
23
34
12
56
34
57
12
If we want to list numbers equal to 12 ,then we write pig latin code like below.


Numbers = load ‘/data/numbers’ as (number:int);

specificNumber = filter numbers by number==12;

Dump specificNumber;


Usually we write above code in a file .let us assume we have written it in a file called numbers.pig

And we write code from file using


Pig –f /path/to/numbers.pig


Later if we want to see only numbers equals to 34, then we change second line to


specificNumber = filter numbers by number==34;


and we re-run the code using same command.
But Its not a good practice to touch the code in production ,so we can make this script dynamic by using –param option of Piglatin.
Whatever values we want to decide at the time of running we make them dynamic .now we want to decide number to be filtered at the time running job,we can write second line like below.


specificNumber = filter numbers by number==$dynanumber


and we run code like below.


Pig –param dynanumber=12  –f numbers.pig


Assume we even want to take path at the time of running script, now we write code like below


Numbers = load ‘$path’ as (number:int);

specificNumber = filter numbers by number==’$ dynanumber';

Dump specificNumber;


And run like below


Pig –param path=/data/path –param dynanumber =34 –f numbers.pig


If you feel this code is missing readability, we can specify all these dynamic values in a file like below
##Dyna.params (file name)


Path = /data/numbers

dynanumber = 34


Then you can run script with param-file option like below.


Pig –param-file dyna.params –f numbers.pig


This type of feature is not available in apache hive.

So what are the benefits we gain using this feature.

1.       We can avoid hard coding in pig scripts
2.       Of course, we make scripts more reusable and dynamic.
3.       We can have better productivity using reusable scripts.

Happy  Hadooping friends.






Datasets for practicing hadoop


To practise Hadoop you can use below ways to generate the big data (GB),So that you can get the real feel/power of the Hadoop.


1.clearbits.net


From clearbits.net, you can get quarterly full data set of stack exchange so that you can use it while you are practising the hadoop . it contains around 10 GB data.

2.grouplens.org

      grouplens.org collected different rating data sets ,you can use it for practicing the hadoop.

If you have Hadoop installed on your machine,you can use the following two ways to generate data.

3.hadoop jar /usr/lib/hadoop-0.20-mapreduce/hadoop-examples.jar randomwriter /random-data


  generates 10 GB data per node under folder /random-data in HDFS.
                 

4.hadoop jar /usr/lib/hadoop-0.20-mapreduce/hadoop-examples.jar randomtextwriter /random-text-data

 generates 10 GB textual data per node under folder /random-text-data in HDFS.

path of hadoop-examples.jar may change as per your hadoop installation.

5. Amazon provides so many data sets ,you can use them.

6. Check answers of the same question on stackoverflow

7.From University of Waikato ,many data sets available for practicing machine learning.

8.See answers for the similar question on Quora.

If you know any free data sets ,please share in comments