HDInsight clusters provide a set of MapReduce samples that can be used to familiarize yourself with running Hadoop MapReduce jobs. In this document, you learn about the available samples and walk through running a few of them.
A Linux-based HDInsight cluster: See Get started using Hadoop with Hive in HDInsight on Linux
Linux is the only operating system used on HDInsight version 3.4 or greater. For more information, see HDInsight Deprecation on Windows.
An SSH client: For more information, see Use SSH with HDInsight.
Location: The samples are located on the HDInsight cluster at /usr/hdp/current/hadoop-mapreduce-client/hadoop-mapreduce-examples.jar
Contents: The following samples are contained in this archive:
- aggregatewordcount: An Aggregate based map/reduce program that counts the words in the input files
- aggregatewordhist: An Aggregate based map/reduce program that computes the histogram of the words in the input files
- bbp: A map/reduce program that uses Bailey-Borwein-Plouffe to compute exact digits of Pi
- dbcount: An example job that counts the pageview logs stored in a a database
- distbbp: A map/reduce program that uses a BBP-type formula to compute exact bits of Pi
- grep: A map/reduce program that counts the matches of a regex in the input
- join: A job that effects a join over sorted, equally partitioned datasets
- multifilewc: A job that counts words from several files
- pentomino: A map/reduce tile laying program to find solutions to pentomino problems
- pi: A map/reduce program that estimates Pi using a quasi-Monte Carlo method
- randomtextwriter: A map/reduce program that writes 10GB of random textual data per node
- randomwriter: A map/reduce program that writes 10GB of random data per node
- secondarysort: An example defining a secondary sort to the reduce
- sort: A map/reduce program that sorts the data written by the random writer
- sudoku: A sudoku solver
- teragen: Generate data for the terasort
- terasort: Run the terasort
- teravalidate: Checking results of terasort
- wordcount: A map/reduce program that counts the words in the input files
- wordmean: A map/reduce program that counts the average length of the words in the input files
- wordmedian: A map/reduce program that counts the median length of the words in the input files
- wordstandarddeviation: A map/reduce program that counts the standard deviation of the length of the words in the input files
Source code: Source code for these samples is included on the HDInsight cluster at /usr/hdp/22.214.171.124-1/hadoop/src/hadoop-mapreduce-project/hadoop-mapreduce-examples
126.96.36.199-1 in the path is the version of the Hortonworks Data Platform for the HDInsight cluster, and may change as HDInsight is updated.
How to run the samples
Connect to HDInsight using SSH. For more information, see Use SSH with HDInsight.
username@#######:~$prompt, use the following command to list the samples:
yarn jar /usr/hdp/current/hadoop-mapreduce-client/hadoop-mapreduce-examples.jar
This generates the list of sample from the previous section of this document.
Use the following command to get help on a specific sample. In this case, the wordcount sample:
yarn jar /usr/hdp/current/hadoop-mapreduce-client/hadoop-mapreduce-examples.jar wordcount
You should receive the following message:
Usage: wordcount <in> [<in>...] <out>
This indicates that you can provide several input paths for the source documents. The final path is where the output (count of words in the source documents,) is stored.
Use the following to count all words in the Notebooks of Leonardo Da Vinci, which are provided as sample data with your cluster:
yarn jar /usr/hdp/current/hadoop-mapreduce-client/hadoop-mapreduce-examples.jar wordcount /example/data/gutenberg/davinci.txt /example/data/davinciwordcount
Input for this job is read from wasbs:///example/data/gutenberg/davinci.txt.
Output for this example is stored in wasbs:///example/data/davinciwordcount.
As noted in the help for the wordcount sample, you could also specify multiple input files. For example,
hadoop jar /usr/hdp/current/hadoop-mapreduce-client/hadoop-mapreduce-examples.jar wordcount /example/data/gutenberg/davinci.txt /example/data/gutenberg/ulysses.txt /example/data/twowordcountwould count words in both davinci.txt and ulysses.txt.
Once the job completes, use the following command to view the output:
hdfs dfs -cat /example/data/davinciwordcount/*
This concatenates all the output files produced by the job, and display them. For this basic example there is only one file, however if there were more this command would iterate over all of them.
The output is similar to the following:
zum 1 zur 1 zwanzig 1 zweite 1
Each line represents a word and how many times it occurred in the input data.
The Sudoku example has somewhat unhelpful usage instructions; "Include a puzzle on the command line."
Sudoku is a logic puzzle made up of nine 3x3 grids. Some cells in the grid have numbers, while others are blank, and the goal is to solve for the blank cells. The link above has more information on the puzzle, but the purpose of this sample is to solve for the blank cells. So our input should be a file that is in the following format:
- Nine rows of nine columns
- Each column can contain either a number or
?(which indicates a blank cell)
- Cells are separated by a space
There is a certain way to construct Sudoku puzzles; you can't repeat a number in a column or row. There's an example on the HDInsight cluster that is properly constructed. It is located at /usr/hdp/188.8.131.52-1/hadoop/src/hadoop-mapreduce-project/hadoop-mapreduce-examples/src/main/java/org/apache/hadoop/examples/dancing/puzzle1.dta and contains the following:
8 5 ? 3 9 ? ? ? ? ? ? 2 ? ? ? ? ? ? ? ? 6 ? 1 ? ? ? 2 ? ? 4 ? ? 3 ? 5 9 ? ? 8 9 ? 1 4 ? ? 3 2 ? 4 ? ? 8 ? ? 9 ? ? ? 8 ? 5 ? ? ? ? ? ? ? ? 2 ? ? ? ? ? ? 4 5 ? 7 8
184.108.40.206-1 portion of the path may change as updates are made to the HDInsight cluster.
To run this through the Sudoku example, use the following command:
yarn jar /usr/hdp/current/hadoop-mapreduce-client/hadoop-mapreduce-examples.jar sudoku /usr/hdp/220.127.116.11-1/hadoop/src/hadoop-mapreduce-project/hadoop-mapreduce-examples/src/main/java/org/apache/hadoop/examples/dancing/puzzle1.dta
The results should appear similar to the following:
8 5 1 3 9 2 6 4 7 4 3 2 6 7 8 1 9 5 7 9 6 5 1 4 3 8 2 6 1 4 8 2 3 7 5 9 5 7 8 9 6 1 4 2 3 3 2 9 4 5 7 8 1 6 9 4 7 2 8 6 5 3 1 1 8 5 7 3 9 2 6 4 2 6 3 1 4 5 9 7 8
The pi sample uses a statistical (quasi-Monte Carlo) method to estimate the value of pi. Points placed at random inside of a unit square also fall within a circle inscribed within that square with a probability equal to the area of the circle, pi/4. The value of pi can be estimated from the value of 4R, where R is the ratio of the number of points that are inside the circle to the total number of points that are within the square. The larger the sample of points used, the better the estimate is.
The mapper for this sample generates a number of points at random inside of a unit square and then counts the number of those points that are inside the circle.
The reducer then accumulates points counted by the mappers and estimates the value of pi from the formula 4R, where R is the ratio of the number of points counted inside the circle to the total number of points that are within the square.
Use the following command to run this sample. This uses 16 maps with 10,000,000 samples each to estimate the value of pi:
yarn jar /usr/hdp/current/hadoop-mapreduce-client/hadoop-mapreduce-examples.jar pi 16 10000000
The value returned by this should be similar to 3.14159155000000000000. For references, the first 10 decimal places of pi are 3.1415926535.
GraySort is a benchmark sort whose metric is the sort rate (TB/minute) that is achieved while sorting very large amounts of data, usually a 100TB minimum.
This sample uses a modest 10GB of data so that it can be run relatively quickly. It uses the MapReduce applications developed by Owen O'Malley and Arun Murthy that won the annual general-purpose ("daytona") terabyte sort benchmark in 2009 with a rate of 0.578TB/min (100TB in 173 minutes). For more information on this and other sorting benchmarks, see the Sortbenchmark site.
This sample uses three sets of MapReduce programs:
- TeraGen: A MapReduce program that generates rows of data to sort
TeraSort: Samples the input data and uses MapReduce to sort the data into a total order
TeraSort is a standard sort of MapReduce functions, except for a custom partitioner that uses a sorted list of N-1 sampled keys that define the key range for each reduce. In particular, all keys such that sample[i-1] <= key < sample[i] are sent to reduce i. This guarantees that the outputs of reduce i are all less than the output of reduce i+1.
TeraValidate: A MapReduce program that validates that the output is globally sorted
It creates one map per file in the output directory, and each map ensures that each key is less than or equal to the previous one. The map function also generates records of the first and last keys of each file, and the reduce function ensures that the first key of file i is greater than the last key of file i-1. Any problems are reported as an output of the reduce with the keys that are out of order.
Use the following steps to generate data, sort, and then validate the output:
Generate 10GB of data, which will be stored to the HDInsight cluster's default storage at wasbs:///example/data/10GB-sort-input:
yarn jar /usr/hdp/current/hadoop-mapreduce-client/hadoop-mapreduce-examples.jar teragen -Dmapred.map.tasks=50 100000000 /example/data/10GB-sort-input
-Dmapred.map.taskstells Hadoop how many map tasks to use for this job. The final two parameters instruct the job to create 10GB worth of data and to store it at wasbs:///example/data/10GB-sort-input.
Use the following command to sort the data:
yarn jar /usr/hdp/current/hadoop-mapreduce-client/hadoop-mapreduce-examples.jar terasort -Dmapred.map.tasks=50 -Dmapred.reduce.tasks=25 /example/data/10GB-sort-input /example/data/10GB-sort-output
-Dmapred.reduce.taskstells Hadoop how many reduce tasks to use for the job. The final two parameters are just the input and output locations for data.
Use the following to validate the data generated by the sort:
yarn jar /usr/hdp/current/hadoop-mapreduce-client/hadoop-mapreduce-examples.jar teravalidate -Dmapred.map.tasks=50 -Dmapred.reduce.tasks=25 /example/data/10GB-sort-output /example/data/10GB-sort-validate
From this article, you learned how to run the samples included with the Linux-based HDInsight clusters. For tutorials about using Pig, Hive, and MapReduce with HDInsight, see the following topics: