Testing your Hadoop program with Maven on IntelliJ
By: Frazy Nondo
Tags: Mapreduce, Intellij, Data Science, Hadoop, Java

Set your project name, project location, groupId, and artifactId. Leave the version untouched and click finish.

Now we are ready to configure our project dependencies

Configuring Dependencies

Open the pom.xml file. This file is often the default opening screen after clicking finish. Click enable Auto-import but you can also import changes if you prefer to be notified every time you edit your pom.xml file.

In your pom.xml file post the following blocks before the project closing tag </project>

<repositories>

<repository>

<id>apache</id>

<url>http://maven.apache.org</url>

</repository>

</repositories> <dependencies>

<dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-core</artifactId>

<version>1.2.1</version>

</dependency>

<dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-common</artifactId>

<version>3.2.0</version>

</dependency>

</dependencies>

The final pom file should look like the following

Below is the full pom.xml file

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>



<groupId>org.Word</groupId>

<artifactId>WordCount</artifactId>

<version>1.0-SNAPSHOT</version>

<repositories>

<repository>

<id>apache</id>

<url>http://maven.apache.org</url>

</repository>

</repositories>

<dependencies>

<dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-core</artifactId>

<version>1.2.1</version>

</dependency>

<dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-common</artifactId>

<version>3.2.0</version>

</dependency>

</dependencies>



</project>

Now we are ready to create classes for our sample test project WordCount.

Creating a WordCount class

Proceed to src -> main -> java package and create a new class

Name the class and click and enter

Paste the following Java code in your wordCount class.

import java.io.IOException;

import java.util.StringTokenizer;



import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;



public class wordCount {



public static class TokenizerMapper

extends Mapper<Object, Text, Text, IntWritable> {



private final static IntWritable one = new IntWritable(1);

private Text word = new Text();



public void map(Object key, Text value, Context context

) throws IOException, InterruptedException {

StringTokenizer itr = new StringTokenizer(value.toString());

while (itr.hasMoreTokens()) {

word.set(itr.nextToken());

context.write(word, one);

}

}

}



public static class IntSumReducer

extends Reducer<Text, IntWritable, Text, IntWritable> {

private IntWritable result = new IntWritable();



public void reduce(Text key, Iterable<IntWritable> values,

Context context

) throws IOException, InterruptedException {

int sum = 0;

for (IntWritable val : values) {

sum += val.get();

}

result.set(sum);

context.write(key, result);

}

}



public static void main(String[] args) throws Exception {

Configuration conf = new Configuration();

Job job = Job.getInstance(conf, "word count");

job.setJarByClass(wordCount.class);

job.setMapperClass(TokenizerMapper.class);

job.setCombinerClass(IntSumReducer.class);

job.setReducerClass(IntSumReducer.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(job, new Path(args[0]));

FileOutputFormat.setOutputPath(job, new Path(args[1]));

System.exit(job.waitForCompletion(true) ? 0 : 1);

}

}

The wordCount class code includes both the main method, the map class and the reduce class. It scans all text files in the folder defined by the first argument, and outpout the frequencies of all words into a folder defined by the second argument.

We are almost ready to run the program….

First we must create our text input file. In your project package create new folder and name it input. Then within the input folder/directory create your txt file or drag one if you already have.

Copy and paste some texts within this file

Almost ready be patient…

We have not set our program arguments. Select Run → Edit Configuration.

Add a new Application Configuration by selecting “+” then Application.

Set the Main class be wordCount, set Program arguments be input output. This allows the program to read from input folder and save the result to output folder. Do not create the output folder, as Hadoop will create the folder automatically. If the folder exists, Hadoop will raise an exception. When done select apply then ok.

Now we are ready to run our program….

Select Run → Run 'WordCount' to run the Hadoop program. If you re-run the program, delete the output folder before.

An output folder will appear. On each run your results are saved in output→part-r-00000.