0% found this document useful (0 votes)
19 views3 pages

Java MapReduce: Word Count Example

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Java MapReduce: Word Count Example

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment 3

MapReduce Programming Basics Word count, sorting, and filtering examples in Java/Python

AIM:

To understand and implement the basics of MapReduce programming in Hadoop by developing and
executing simple programs such as Word Count, Sorting, and Filtering using Java

Step 1:Create WordCount Program in Eclipse

1. Open Eclipse → Create a Java Project


2. Right-click project →New package package name: [Link] and
Finish
3. Right-click project New  Class  class Name:WordCount
4. Type the program
5. Right click on projectconfigure Built path libraries add external jars
6. Right click on projectconfigure Built path java compiler
7. Right click on projectexport javajar file

Step 2: Open VMware

Step 3: Open Winscp

1. Type IP address
2. Username
3. Password
4. Transferring JAR/input files to Hadoop cluster just drag and drop.

Step 4: Open Putty

1. Open PuTTY on Windows.


2. In the Host Name (or IP address) field → enter the server’s IP or hostname (for
example: [Link] or hadoop-master).
3. Port = 22 (default for SSH).
4. Connection type = SSH.
5. Click Open.
6. A terminal will appear → enter your username (e.g., hduser) and password.

Commands:

Type ls- The ls command is used to list files and directories in the current directory or a
specified path.

Type Hadoop check version

Create a directory: Type hadoop fs –mkdir /input folder name

Upload file from local to HDFS: hadoop fs –put [Link] / input folder name
List files in HDFS : hadoop fs –ls / input folder name

View contents of a file: hadoop fs –cat /input folder name /[Link]

Run a jar file: hadoop jar jar [Link] [Link] /input folder / [Link]
/output folder

hadoop fs –ls /output folder

Output: hdfs dfs -cat /output folder /part-r-00000

Program :

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class WordCount {

public static class WordCountMapper extends Mapper<LongWritable,Text,Text,IntWritable>


{
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException
{
String line = [Link]();
String[] words=[Link](",");
for(String word: words )
{
Text outputKey = new Text([Link]().trim());
IntWritable outputValue = new IntWritable(1);
[Link](outputKey, outputValue);
}
}
}

public static class WordCountReducer extends Reducer<Text,IntWritable,Text,IntWritable>


{
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException
{
int sum = 0;
for(IntWritable value : values)
{
sum += [Link]();
}
[Link](key, new IntWritable(sum));
}
}

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

if ([Link] != 2) {
[Link]("Insufficient args");
[Link](-1);
}
Configuration conf = new Configuration();

[Link]("ResourceManager", "hdfs://[Link]:8050");
Job job = new Job(conf, "WordCount");

[Link]([Link]);

[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);

[Link]([Link]);
[Link]([Link]);

[Link]([Link]);
[Link]([Link]);

[Link](job, new Path(args[0]));


[Link](job, new Path(args[1]));

[Link](true);
}
}

Output :

Result:

Thus the MapReduce programs for Word Count, Sorting, and Filtering were successfully
implemented and executed using Hadoop.

You might also like