Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Introduction to Streaming in Hadoop

It uses UNIX standard streams as the interface between Hadoop and your program so you can write Mapreduce program in any language which can write to standard output and read standard input. Hadoop offers a lot of methods to help non-Java development.

The primary mechanisms are Hadoop Pipes which gives a native C++ interface to Hadoop and Hadoop Streaming which permits any program that uses standard input and output to be used for map tasks and reduce tasks.
With this utility one can create and run Map/Reduce jobs with any executable or script as the mapper and/or the reducer.

Get Hadoop Certification in Just 50 Hours

Example using Python

Streaming supports any programming language that can read from standard input and write to standard output. For Hadoop streaming one must consider the word-count problem. Codes are written for the mapper and the reducer in python script to be run under Hadoop.
Mapper Code

import sys
for intellipaatline in sys.stdin: # Input takes from standard input
intellipaatline = intellipaatline.strip() # Remove whitespace either side
words = intellipaatline.split() # Break the line into words
for myword in words: # Iterate the words list
output print '%s\t%s' % (myword, 1) # Write the results to standard

Reducer Code

#!/usr/bin/python from operator import item getter import sys current_word = “” current_count = 0 word = “” for intellipaatline in sys.stdin: # Input takes from standard input intellipaatline = intellipaatline.strip()# Remove whitespace either side word , count = intellipaatline.split(‘\t’, 1) # Split the input we got from try: # Convert count variable to integer count = int(count) except ValueError: # Count was not a number, so silently ignore this line continue if current_word == word: current_count += count else: if current_word: print ‘%s\t%s’ % (current_word, current_count) # Write result to standard o/p current_count = count current_word = word if current_word == word: # Do not forget to output the last word if needed! print ‘%s\t%s’ % (current_word, current_count) Mapper and Reducer codes should be saved in and in Hadoop home directory.

WordCount Execution

$ $HADOOP_HOME/bin/hadoop jar contrib/streaming/hadoop-streaming-1. 2.1.jar \
-input input_dirs \ -
output output_dir \ -
mapper<path/ \
-reducer <path/

Where “\” is used for line continuation for clear readability

Free Ebook: Step by Step Guide to Master Hadoop

How Streaming Works

Input is read from standard input and the output is emitted to standard output by Mapper and the Reducer. Utility creates a Map/Reduce job, submits the job to an appropriate cluster, and monitors the progress of the job until completion.
Every mapper task will launch the script as a separate process when the mapper is initialized after a script is specified for mappers. Mapper task inputs are converted into lines and fed to the standard input and Line oriented outputs are collected from the standard output of the procedure Mapper and every line is changed into a key, value pair which is collected as the outcome of the mapper.
Each reducer task will launch the script as a separate process and then the reducer is initialized after a script is specified for reducers. As the reducer task runs, reducer task input key/values pairs are converted into lines and feds to the standard input (STDIN) of the process.
Each line of the line-oriented outputs is converted into a key/value pair after it is collected from the standard output (STDOUT) of the process, which is then collected as the output of the reducer.

Important Commands

-input directory/file-nameInput location for mapper. (Required)
-output directory-nameOutput location for reducer. (Required)
-mapper executable or script or JavaClassNameMapper executable. (Required)
-reducer executable or script or JavaClassNameReducer executable. (Required)
-file file-nameCreate the mapper, reducer or combiner executable available locally on the compute nodes.
-inputformat JavaClassNameClass you offer should return key, value pairs of Text class. If not specified TextInputFormat is used as the default.
-outputformat JavaClassNameClass you offer should take key, value pairs of Text class. If not specified TextOutputformat is used as the default.
-partitioner JavaClassNameClass that determines which reduce a key is sent to.
-combiner streaming Command or JavaClassNameCombiner executable for map output.
-inputreaderFor backwards compatibility: specifies a record reader class instead of an input format class.
-verboseVerbose output.
-lazyOutputCreates output lazily. For example if the output format is based on FileOutputFormat, the output file is created only on the first call to output.collect or Context.write.
-numReduceTasksSpecifies the number of reducers.
-mapdebugScript to call when map task fails.
-reducedebugScript to call when reduction makes the task failure
-cmdenv name=valuePasses the environment variable to streaming commands.

Wish to Learn Hadoop? Click Here

Hadoop Pipes

It is the name of the C++ interface to Hadoop MapReduce. Unlike Streaming which uses standard I/O to communicate with the map and reduce code Pipes uses sockets as the channel over which the tasktracker communicates with the process running the C++ map or reduce function. JNI is not used.

Previous Next

Download Interview Questions asked by top MNCs in 2018?

"0 Responses on Streaming"

    100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

    Sales Offer

    Sign Up or Login to view the Free Streaming.