Total Pageviews

Wednesday 12 November 2014

Hadoop Interview Questions part 1


1. What is Hadoop framework?
Ans: Hadoop is an open source framework which is written in java by apache software foundation. 
This framework is used to write software application which requires to process vast amount of data (It 
could handle multi tera bytes of data). It works in-parallel on large clusters which could have 1000 of 
computers (Nodes) on the clusters. It also process data very reliably and fault-tolerant manner. See 
the below image how does it looks.
2. On What concept the Hadoop framework works?
Ans: It works on MapReduce, and it is devised by the Google.
3. What is MapReduce?
Ans: Map reduces is an algorithm or concept to process Huge amount of data in a faster way. As per 
its name you can divide it Map and Reduce.
• The main MapReduce job usually splits the input data-set into independent chunks. (Big data sets in 
the multiple small datasets)
• Reduce Task: And the above output will be the input for the reduce tasks, produces the final result.
Your business logic would be written in the Mapped Task and Reduced Task. Typically both the input 
and the output of the job are stored in a file-system (Not database). The framework takes care of 
scheduling tasks, monitoring them and re-executes the failed tasks.
4. What is compute and Storage nodes?
Ans: Compute Node: This is the computer or machine where your actual business logic will be 
executed.
Storage Node: This is the computer or machine where your file system resides to store the processing 
data. In most of the cases compute node and storage node would be the same machine.
5. How does master slave architecture in the Hadoop?
Ans: the MapReduce framework consists of a single master Job Tracker and multiple slaves, each 
cluster-node will have one Task Tracker.The master is responsible for scheduling the jobs' component tasks on the slaves, monitoring 
them and re-executing the failed tasks. The slaves execute the tasks as directed by the master.
6. How does a Hadoop application look like or their basic components?
Ans: Minimally a Hadoop application would have following components.
• Input location of data
• Output location of processed data.
• A map task.
• A reduced task.
• Job configuration
The Hadoop job client then submits the job (jar/executable etc.) and configuration to the Job Tracker
which then assumes the responsibility of distributing the software/configuration to the slaves, 
scheduling tasks and monitoring them, providing status and diagnostic information to the job-client.
7. Explain how input and output data format of the Hadoop framework?
Ans: The MapReduce framework operates exclusively on pairs, that is, the framework views the 
input to the job as a set of pairs and produces a set of pairs as the output of the job, conceivably of 
different types. See the flow mentioned below (input) -> map -> -> combine/sorting -> -> reduce -> 
(output)
8. What are the restriction to the key and value class?
Ans: The key and value classes have to be serialized by the framework. To make them serializable 
Hadoop provides a Writable interface. As you know from the java itself that the key of the Map 
should be comparable, hence the key has to implement one more interface Writable Comparable.
9. Explain the Word Count implementation via Hadoop framework?
Ans: We will count the words in all the input file flow as below
• Input
Assume there are two files each having a sentence Hello World Hello World (In file 1) Hello World 
Hello World (In file 2)
• Mapper: There would be each mapper for the a file
For the given sample input the first map output:
< Hello, 1>< World, 1>
< Hello, 1>
< World, 1>
The second map output:
< Hello, 1>
< World, 1>
< Hello, 1>
< World, 1>
• Combiner/Sorting (This is done for each individual map)
So output looks like this
The output of the first map:
< Hello, 2>
< World, 2>
The output of the second map:
< Hello, 2>
< World, 2>
• Reducer:
• Output
It sums up the above output and generates the output as below
< Hello, 4>
< World, 4>
Final output would look like
Hello 4 times
World 4 times10. Which interface needs to be implemented to create Mapper and Reducer for the Hadoop?
Ans: org.apache.hadoop.mapreduce.Mapper org.apache.hadoop.mapreduce.Reducer
11. What Mapper does?
Ans: Maps are the individual tasks that transform input records into intermediate records. The 
transformed intermediate records do not need to be of the same type as the input records. A given 
input pair may map to zero or many output pairs.
12. What is the Input Split in map reduce software?
Ans: An Input Split is a logical representation of a unit (A chunk) of input work for a map task; e.g., a 
filename and a byte range within that file to process or a row set in a text file.
13. What is the Input Format?
Ans: The Input Format is responsible for enumerate (itemize) the Input Split, and producing a Record 
Reader which will turn those logical work units into actual physical input records.
14. Where do you specify the Mapper Implementation?
Ans: Generally mapper implementation is specified in the Job itself.
15. How Mapper is instantiated in a running job?
Ans: The Mapper itself is instantiated in the running job, and will be passed a
Map Context object which it can use to configure itself
16. Which are the methods in the Mapper interface?
Ans: the Mapper contains the run () method, which call its own setup () method only once, it also call 
a map () method for each input and finally calls it cleanup () method. All above methods you can 
override in your code.
17. What happens if you don’t override the Mapper methods and keep them as it is?
Ans: If you do not override any methods (leaving even map as-is), it will act as the identity function, 
emitting each input record as a separate output.
18. What is the use of Context object?
Ans: The Context object allows the mapper to interact with the rest of the Hadoop system. It
Includes configuration data for the job, as well as interfaces which allow it to emit output.19. How can you add the arbitrary key-value pairs in your mapper?
Ans: You can set arbitrary (key, value) pairs of configuration data in your Job, e.g. with 
Job.getConfiguration ().set ("myKey", "myVal"), and then retrieve this data in your mapper with
context.getConfiguration ().get ("myKey"). This kind of functionality is typically done in the Mapper's 
setup () method.
20. How does Mapper’s run () method works?
Ans: The Mapper. Run () method then calls map (KeyInType, ValInType, Context) for each key/value 
pair in the Input Split for that task
21. Which object can be used to get the progress of a particular job?
Ans: Context
22. What is next step after Mapper or MapTask?
Ans: The output of the Mapper is sorted and Partitions will be created for the output. Number of 
partition depends on the number of reducer.
23. How can we control particular key should go in a specific reducer?
Ans: Users can control which keys (and hence records) go to which Reducer by implementing a custom 
Partitioner.
24. What is the use of Combiner?
Ans: It is an optional component or class, and can be specify via Job.setCombinerClass (Class Name), 
to perform local aggregation of the intermediate outputs, which helps to cut down the amount of 
data transferred from the Mapper to the Reducer.
25. How many maps are there in a particular Job?
Ans: the number of maps is usually driven by the total size of the inputs, that is, the total number of 
blocks of the input files.
Generally it is around 10-100 maps per-node. Task setup takes awhile, so it is best if the maps take at 
least a minute to execute.
Suppose, if you expect 10TB of input data and have a block size of 128MB, you'll end up with 82,000
maps, to control the number of block you can use the mapreduce.job.maps parameter (which only 
provides a hint to the framework). Ultimately, the number of tasks is controlled by the number of 
splits returned by the InputFormat.getSplits () method (which you can override).
26. What is the Reducer used for?Ans: Reducer reduces a set of intermediate values which share a key to a (usually smaller) set of 
values. The number of reduces for the job is set by theuser via Job.setNumReduceTasks (int).
27. Explain the core methods of the Reducer?
Ans: The API of Reducer is very similar to that of Mapper, there's a run() method that receives a 
Context containing the job's configuration as well as interfacing methods that return data from the 
reducer itself back to the framework. The run() method calls setup() once, reduce() once for each 
key associated with the reduce task, and cleanup() once at the end. Each of these methods can 
access the job's configuration data by using Context.getConfiguration ().
As in Mapper, any or all of these methods can be overridden with custom implementations. If none of 
these methods are overridden, the default reducer operation is the identity function; values are 
passed through without further processing.
The heart of Reducer is it’s reduce () method. This is called once per key; the second argument is an 
Iterable which returns all the values associated with that key.
28. What are the primary phases of the Reducer?
Ans: Shuffle, Sort and Reduce
29. Explain the shuffle?
Ans: Input to the Reducer is the sorted output of the mappers. In this phase the framework fetches 
the relevant partition of the output of all the mappers, via HTTP.
30. Explain the Reducer’s Sort phase?
Ans: The framework groups Reducer inputs by keys (since different mappers may have output the 
same key) in this stage. The shuffle and sort phases occur simultaneously; while map-outputs are 
being fetched they are merged (It is similar to merge-sort).

Sunday 9 November 2014

Searching Data with Apache Solr

Overview

In this tutorial we will walk through how to use Apache Solr with Hadoop to index and search data stored on HDFS. It’s not meant as a general introduction to Solr.
After working through this tutorial you will have Solr running on your Hortonworks Sandbox. You will also have a solrconfig and a schema which you can easily adapt to your own use cases. Also you will learn how to use Hadoop MapReduce to index files.  

Prerequisites

Remarks: I was using VMware’s Fusion to run Sandbox. If you choose Virtualbox things should look the same beside the fact your VM will not have it’s own IP address but rather Solr listening on 127.0.0.1. For convenience I added sandbox as a host to my /etc/hosts file on my Mac. Apache Solr 4.7.2 is the officially by Hortonworks supported version as I’m writing this (May 2014). 

Steps

Let’s get it started: Power-up the sandbox with at least 4GB main memory.
    ssh root@sandbox (Passwort: hadoop)
    ./start_ambari.sh
Open your browser and verify that all services are running. We will only need HDFS and MapReduce but “all lights green” is always good ;-) 
We start by creating a solr user and a folder where we are going to install the binaries:
    adduser solr
    passwd solr

    mkdir /opt/solr
    chown solr /opt/solr
Now copy the binaries you downloaded from the list of ingredients above from your host to the Sandbox from your Mac / Windows host:
    cd ~/Downloads
    scp solr-4.7.2.tar lucidworks-hadoop-1.2.0-0-0.tar solr@sandbox:/opt/solr
Next step is creating dummy data we will later on index in Solr and make searchable. As mentioned above this is “Hello World!” so better do not expect big data. The file we are going to index will be four line csv file. Type the following on your Sandbox command prompt:
    echo id,text >/tmp/mydata.csv; echo 1,Hello>>/tmp/mydata.csv; echo 2,HDP >>/tmp/mydata.csv; echo 3,and >>/tmp/mydata.csv; echo 4,Solr >>/tmp/mydata.csv
Then we need to prepare HDFS:
    su - hdfs
    hadoop fs -mkdir -p /user/solr/data/csv 
    hadoop fs -chown solr /user/solr
    hadoop fs -put /tmp/mydata.csv /user/solr/data/csv
Now it’s getting more interesting as we are about to install Solr:
    su - solr
    cd /opt/solr

    tar xzvf solr-4.7.2.tar 
    // untar is all we need to install Solr! We still need to integrate it into HDP though. 
    tar xvf lucidworks-hadoop-1.2.0-0-0.tar
    ln -s solr-4.7.2 solr
    ln -s lucidworks-hadoop-1.2.0-0-0 jobjar
Solr comes with a nice example which we will use as a starting point:
    cd solr
    cp -r example hdp 
    // Remove unnecessary files:
    rm -fr hdp/examle* hdp/multicore
    // Our core (basically the index) will be called hdp1 instead of collection1
    mv hdp/solr/collection1 hdp/solr/hdp1
    // Remove the existing core
    rm hdp/solr/hdp1/core.properties
Now comes the most difficult part: Making Solr storing its data on HDFS and creating a schema for our “Hello World” csv file. We need to modify two files solrconfig.xml and schema.xml
    vi hdp/solr/hdp1/conf/solrconfig.xml
Search for the tag
    <directoryFactory
    ..
    </directoryFactory>
And completely replace it with (Make sure you copy the full line. The lines may appeart truncated in the browser but when you copy/paste the full lines you’re good):
    <directoryFactory name="DirectoryFactory" class="solr.HdfsDirectoryFactory">
      <str name="solr.hdfs.home">hdfs://sandbox:8020/user/solr</str>
      <bool name="solr.hdfs.blockcache.enabled">true</bool>
      <int name="solr.hdfs.blockcache.slab.count">1</int>
      <bool name="solr.hdfs.blockcache.direct.memory.allocation">true</bool>
      <int name="solr.hdfs.blockcache.blocksperbank">16384</int>
      <bool name="solr.hdfs.blockcache.read.enabled">true</bool>
      <bool name="solr.hdfs.blockcache.write.enabled">true</bool>
      <bool name="solr.hdfs.nrtcachingdirectory.enable">true</bool>
      <int name="solr.hdfs.nrtcachingdirectory.maxmergesizemb">16</int>
      <int name="solr.hdfs.nrtcachingdirectory.maxcachedmb">192</int>
    </directoryFactory>
Now still in solrconfig.xml look for lockType. Change it to hdfs:
    <lockType>hdfs</lockType>
Save the file and open schema.xml
    vi hdp/solr/hdp1/conf/schema.xml
In the
<fields>
tag keep only the fields with the following names:
_version_
_root_
Leave the dynamic fields unchanged (they could be useful for your own use-cases but we will not need them in this example though). 
Add the following fields:
    <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
    <field name="text" multiValued="true" stored="true"  type="text_en" indexed="true"/>
    <field name="data_source" stored="false" type="text_en" indexed="true"/> 
The data_source field is required by the map reduce based indexing we will use later. The fields named id and name are matching the two columns in our csv file. 
Next remove all copyField tags and add:
Lets Add the id to text so we can search both
<copyField dest="text" source="id"/>
Now we need to create our core/index. Start solr and point your browser to it (http://sandbox:8983/solr):
    cd hdp
    java -jar start.jar

Click on “Core Admin” and fill in the fields as below:

If everything goes as expected you should see the following:

If something is broken (xml file non parseable, wrong folder…) you can easily start from fresh by:
    // stop or kill solr
    rm /opt/solr/solr/hdp/solr/hdp1/core.properties
    hadoop fs -rm -r /user/solr/hdp1
    // start solr again
Now choose the just created core “hdp1” from the dropdown box on the left:
Click on Query and press the blue “Execute Query” button. You will see that we still have 0 documents in our index which is no surprise as we have not indexed anything: 
So now we are going to index our big csv file ;-)
    hadoop jar jobjar/hadoop/hadoop-lws-job-1.2.0-0-0.jar com.lucidworks.hadoop.ingest.IngestJob -Dlww.commit.on.close=true -DcsvFieldMapping=0=id,1=text -cls com.lucidworks.hadoop.ingest.CSVIngestMapper -c hdp1 -i /user/solr/data/csv/mydata.csv -of com.lucidworks.hadoop.io.LWMapRedOutputFormat -s http://localhost:8983/solr
If everything went well your output should look like:
    14/05/24 06:46:00 INFO mapreduce.Job: Job job_1400841048847_0036 completed successfully
    14/05/24 06:46:00 INFO mapreduce.Job: Counters: 32
        File System Counters
            FILE: Number of bytes read=0
            FILE: Number of bytes written=201410
            FILE: Number of read operations=0
            FILE: Number of large read operations=0
            FILE: Number of write operations=0
            HDFS: Number of bytes read=287
            HDFS: Number of bytes written=0
            HDFS: Number of read operations=4
            HDFS: Number of large read operations=0
            HDFS: Number of write operations=0
        Job Counters 
            Launched map tasks=2
            Data-local map tasks=2
            Total time spent by all maps in occupied slots (ms)=16727
            Total time spent by all reduces in occupied slots (ms)=0
            Total time spent by all map tasks (ms)=16727
            Total vcore-seconds taken by all map tasks=16727
            Total megabyte-seconds taken by all map tasks=4181750
        Map-Reduce Framework
            Map input records=5
            Map output records=4
            Input split bytes=234
            Spilled Records=0
            Failed Shuffles=0
            Merged Map outputs=0
            GC time elapsed (ms)=146
            CPU time spent (ms)=3300
            Physical memory (bytes) snapshot=295854080
            Virtual memory (bytes) snapshot=1794576384
            Total committed heap usage (bytes)=269484032
        com.lucidworks.hadoop.ingest.BaseHadoopIngest$Counters
            DOCS_ADDED=4
            DOCS_CONVERT_FAILED=1
        File Input Format Counters 
            Bytes Read=53
        File Output Format Counters 
            Bytes Written=0
Go back to your browser and enter “HDP” in the field called “q” and press “Execute Query”: 
Congratulations!!! 
You installed and integrated Solr on HDP. Indexed a csv file through map reduce and successfully executed a Solr query against the index! 
Next steps are now installing Solr in SolrCloud mode on an HDP cluster, index real files and create a nice web app so that business users can easily search for information stored on Hadoop. 
I hope this was useful and you had fun!