Tuesday, 27 September 2016

Do we need JAVA to learn HADOOP?

When Doug Cutting, the creator of Hadoop, named his new framework after his son’s toy elephant, little did he know that it would take the open source software world by storm. Today, we can also presume that Doug did not wish to create an elephantine misconception about Java being required to master Hadoop. True, Hadoop is built on Java. But do you need Java to learn Hadoop? This blog answers the question for you.

Do you need Java to learn Hadoop?

Two important Hadoop components endorse the fact that you can work with Hadoop without having functional knowledge of Java – Pig and Hive.
Pig is a high-level data flow language and execution framework for parallel computation, while Hive is a data warehouse infrastructure that provides data summarization and ad- hoc querying. Pig is widely used by researchers and programmers while Hive is a favourite with data analysts.
10 lines of Pig = 200 lines of Java. Check out this blog for a Pig demo.
In order to navigate through Pig and Hive, you only need to learn Pig Latin and Hive Query Language (HQL), both of which need only an SQL base. Pig Latin is very similar to SQL, while HQL can best be described as a much faster and more tolerant avatar of SQL. These languages are easy to learn, and more than 80% of Hadoop projects revolve around them.
Careers in Hadoop
Hadoop has become the poster boy of Big Data. With its ability to store huge amounts of data – both structured and unstructured – on the cloud, with lesser capital investment, Hadoop is on top of every CIO’s to-do list, today. This had led to a burgeoning growth in career opportunities around Hadoop.
In order to explore job roles related to Hadoop without having Java as a prerequisite, you need to just orient yourself to two critical aspects of Hadoop; Storage and Processing. For a job around Hadoop storage, you can learn how Hadoop cluster functions, and how Hadoop makes its data secure and stable. For this, knowing the various nuances of the Hadoop Distributed File System (HDFS) and HBase, Hadoop’s distributed database, will help tremendously.
If you choose to work on the processing side of Hadoop, you have Pig and Hive at your disposal, that automatically convert your code in the backend to work with the Java-based MapReduce cluster programming model.
So, without running MapReduce, you can still control the entire life cycle of your project. As long as you master Pig and Hive, along with HDFS and HBase, Java can take a backseat.

Rare requirements for Java coding
However, Java coding is needed if you wish to add user-defined functions to Pig, Hive and other tools. This is required only if you wish to create custom input/output formats. We are happy to inform that this requirement is a rarity.
Another rare scenario where basic Java coding might be necessary is for debugging. In the rare event of a Hadoop program crashing, you might need to debug the program using Java. It’s a fair guess how insignificant a debugging role is going to be, in your career.

JSON vs XML

In Web Application Development, we use different types of formats for accessing a data over the web. Today, we will compare two different formats JSON (JavaScript Object Notation) and XML (Extensible Markup Language). Both have their particular usage for to & fro data passing.

JSON (JavaScript Object Notation) – A model of efficiency

  • A Lightweight text-based open designed for human – readable data interchange manner.
  • It is an alternative to XML that is more efficient because it is not a markup language that requires open and close tags.
  • It is extended from JavaScript.
  • Example :
XML (Extensible Markup Language) – A Data stuffed
  • An Open Standard for describing data defined by the www (world wide web).
  • It defines mark-up language that allows the developer to certain HTML tags type codes.
  • Example:

Difference between XML vs JSON

FeaturesJSONXML
Full FormJavaScript Object NotationExtensible Markup Language
ExtendExtended from JavaScriptExtended from SGML(Standard Generalized Markup Language)
SpeedFaster in ExecutionSlow compared to JSON in Execution
Arrays UsageUses Structured Data Excepting ArraysUses Structured Data Including Arrays
ApplicationFor WebService, JSON is better.For Configuration, XML is better
Example
CommentsNot Supports Comments Supports Comments
NameSpaceNo support for Namespaces Support Namespaces
APIsFacebook Graph API, Google Maps API, Twitter API, Pinterest API,Reddit APIAmazon Products Advertising API
PerformanceGood Compared to XMLNot as good when Compared to JSON
PurposeStructured Data InterchangeDocument Markup
Database SupportMongoDB, CouchDB, eXistDB, BaseX, Oracle Database, PostgreSQLMySQL, IBM DB2, Mocrosoft SQL Server, BaseX, MarkLogics
Light WeightLighter than XMLLess Light compared to JSON
SchemaFor Description and DataType and Structure ValidationFor DataType, Structure Validation, It also makes possible to create new DataTypes
Easy to ManipulateYesNo
SecurityMore SecureLess Secure
Choose wisely between JSON & XML. There are some good reasons for using JSON over XML & vice versa. JSON is a better data exchange format. XML is a better document exchange format. Use the right one for the right job

Regards
Anand Pandey

Apache Pig Use Case – Electrical Bulb Testing

In this blog, we will work on a use case involving electric bulbs and work with the date and time concepts in Pig.
In this instance, Pig is used in the local mode to load the local data. We can use Pig in HDFS mode as per our convenience.
In the research center of bulb manufacturing companies, the longevity of bulbs is tested by subjecting them to adverse conditions.
The dataset used in this case is a sample from the light bulb production house where bulbs are tested at random intervals of time. The first column is StartDate which is the date and time when the testing of the bulb started and the second column is EndDate which is the date when the testing ended.
StartDate           EndDate
30-Jun-2018 23:42    04-Jul-2018 15:10
30-Jun-2018 23:37
30-Jun-2018 23:13    30-Jun-2019 23:34m
A few rows may be empty which indicates that data is not available, maybe because of various reasons. But as a developer we need not worry about missing data. With the help of Data Filtering, we can remove the unnecessary data.
Loading Data into the Pig environment
Since Pig uses default as tab(\t) delimited data, it’s not mandatory to state USING PigStorage(‘\t’)  in the code while loading, nevertheless it is good to write it. You have to use this parameter depending on the dataset.
Since we have data inside Pig, the first step is to filter data in the column we are working on.
Here we remove all the rows with null data.
In this step, it is mandatory to filter all the data in EndTime containing – symbol.
We have to convert the data loaded in Pig into datetime format in order to work with it.
Here, we use two predefined functions:
ToDate()
MinutesBetween()
The first one converts the character array to datetime readable structure which can be interpreted by Pig and the second one takes the difference between two DateTime parameters provided.
The ToDate function can be used in different formats of year, month and date. Some examples are as follows:
YYYY-MM-DD
DD/MM/YYYY
DD-YY-MM
Depending on the appropriate structure in the dataset provided, we can choose the format.
After simple filtering and conversion of character array data to datetime format, we have now determined the difference in terms of minutes for every bulb which was in ON state during testing.

We can see the results with dump command.
Result in minutes is displayed:
Once we achieve this, we can perform analysis on the result, for example, to find the maximum time a bulb can stay ON or minimum time and so on..
Shown below is the result for the average time the bulbs were ON during the testing phase.
Dump Avg_ALL;
This way we can perform analysis on the filtered result and get the results with help of Pig in a matter of minutes from a large set of data.
For dataset and code for practice, click  —  https://drive.google.com/open?id=0B2nmxAJLHEE8Tnp3SmEyLUhnMkE
HAPPY LEARNING !
ANAND PANDEY

TroubleShoot Hive and Pig Errors

In this blog we will learn how to solve common errors which may occur while executing hive and pig commands.
We recommend readers to go through our previous blog on Troubleshooting errors in MapReduce before moving ahead.
A beginner will normally have doubts about errors while executing hive and Pig commands in the initial stages, He might not be aware of the type of common errors that may occur and what type of solutions should be used to solve that error.
So, let us know the different types of error which may occur while executing hive and Pig commands and the solution which must be used to solve that error.
Troubleshooting Hive errors
1. Another instance of Derby may have already booted the database :
This type of error will be thrown if the user tries to access more than one instance of Derby database at a time.
Normally, Hive in embedded mode has a limitation of one active user at a time.
As we can see from the above image we have started hive in more than one terminal.
So once the user tries to use hive in more than one terminal an error will be thrown stating that an Another instance of Derby may have already booted the database.
Embedded Apache Derby is used as the default Hive metastore in the Hive configuration. This configuration is called embedded metastore and is good for the sake of development and unit testing, but won’t scale to a production environment as only a single user can connect to the derby database at any instant of time. Starting second instance of the Hive driver will throw a error message.
The solution for the above error can is the user should use one instance of the Derby database at a time.
2. Invalid or No files matching path :
This type of error will be thrown if the user doesn’t specify s the correct path of the file while loading file data into the table.
We can see from the above image an error as been thrown stating “No files matching”. Because the actual file is in hdfs path, we can refer to below image where the file employee.txt is present in hdfs root directory.
We can use cat command to see the contents of employee.txt file.
To solve the above error the user should specify the correct path of the file where it is saved.
We know that the employee.txt file is present in hdfs root path. Let us load the employee.txtfile into the table emp_details, without using local command (local refers to local file sytem path) in the load file statement.
We can see from the image employee.txt file contents successfully loaded in the tableemp_deatils.
Now we can use select * command to see the contents of the table emp_details.
Thus, from the above execution steps we have learnt the type of error and the solution for the error if we get error as Invalid or No files matching path file.

Trouble shooting common pig errors :
1. Input path does not exist – Hdfs : The first thing whenever a programmer wishes to execute any query in pig shell is, the programmer should decide to in which mode he/she would like to process that query. If the programmer tries to load and dump a relation file which is not present in the specified path then an error will be thrown, stating “input path does not exist”.
When we are in Pig Mapreduce mode the Mapreduce frame work allows user to perform querys on the files which are present only in the Hdfs path.
In this below example, We are purposefully trying to load a file from Local file system path and try to dump it in Pig Mapreduce mode.
We can see in the above image we are loading the file emp.txt which is present in Local file system path into the relation A.
We can see in the above image a error is thrown while Dumping the relation A.
The mapreduce framework considers that the assigned path is already exist in hdfs. i.e, it considers the path /home/acadgild/Desktop/emp.txt is already exist in hdfs. When the relation Ais dumped, since the path and file is not available in the hdfs directory the error Input path does not exist will be thrown.
So, The solution for the above error is to specify the correct path of the input file while loading.
Since, we are in Pig Mapreduce mode we should first save the input file emp.txt into the hdfs path and execute the query.
We can see in the below image the emp.txt file now, is present in the hdfs root path.
We can now load the file and use Dump command to see the contents of the file.
We can see in the above image, no exception is thrown after after Dumping the relation A.
Thus, we can see from the above execution steps we have learnt the type of error and the solution for the error if we get error as input file path does not exist.
2. Pig by default loads field files delimited by tab :
When loading an input file into the pig, if the user doesn’t explicitly specify s the field delimited separator of the file, then by default pig considers the input loaded file fields are separated by tab.
When that relation is dumped, “null values are printed” instead of the actual field values.
In the below example we have considered two input files where the file em.txt is separated bytab and the file emp.txt is separated by comma(,).
Let us load the em.txt file without explicitly declaring the using pigstorage( ) field delimited command.
Let us Dump the relation A to see the result.
Since the file em.txt fields were separated by tab(\t) there is no need of using, using pigstorage ( ) field delimited command, therefore relation A successfully stores all the records without any exception.
Now let us load the emp.txt file where the fields are separated by comma(,).
We can see in the above image we are loading emp.txt file without using pigstorage ( ), fields delimited command. Hence, pig considers the loaded file is separated by tab and stores null values in the relation B.
Let us Dump the relation B to see the result.
We can see in the above image we have dumped the relation B, but null values were returned instead of actual values.
The solution for the above error is to use using pigstorage (‘field separator’) command while loading the input file in the pig.
Let us load the file emp.txt using command using pigstorage(‘field separator’) and dump the relation B to see the result.
We can see from the above image actual values are stored instead of null values in the relation B
Thus, we can see from the above execution steps we have learnt when to use “using pigstorage(field separator)” command if the input file fields are separated apart from character tab.
3. Undefined alias :
This type of error will be thrown if a relation is being used which does not exist in the pig query command.
In the above diagram we can see that there is no relation with the name a . Thus, an error is thrown stating “Undefined alias: a”
The solution for the above exception is to use an existing relation in the query.
We can see from the above image, no exception is thrown after using the existing relation A in the query.
Now by using Dump command we see the contents of the relation B.
Thus, we can see from the above execution steps we have learnt the type of error and the solution for the error “Undefined alias relation” name.
4. Projected field does not exist :
This type of error will be thrown if the field name when the specified field does not exist in the schema.
We can see in the above image there is no field with the name sal in the relation A. But, there is a field with the name emp_sal.
Now, let us change the field name sal as emp_sal in the query command to get the filtered result.
We can see in the above image, no exception is thrown after changing the field name as emp_salas the fieldemp_sal was already existed in the schema.
Now let us Dump the relation to see the required output.
Thus, we can see from the above execution steps we have learnt the type of error and the solution for the error Projected field does not exist.
5. Successfully stored Zero records :
This type of error will be thrown when a user tries to run an invalid condition on a particular field in the command.
expression(condition) which doesn’t matches with the actual field values in the relation.
We can refer in the below image relation A, emp_sal field doesn’t contain value more than50000.
Since, user tried an invalid expression(condition) which didn’t matched with the actual field values in the relation hence “Successfully stored 0 records” message is thrown.
The solution for the above error is to use valid expression(condition) while performing an action on the field relation values.
Now let us run the same command by giving the proper condition which matches the field values.
From the above image we can see relation C successfully stores 2 records without any exception.
Thus, we can see from the above execution steps we have learnt the type of error and the solution for the error Successfully stored zero records.
We Hope this blog helped you in understanding how to troubleshoot common errors in pig.
Regards
Anand Pandey