DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Implementing Budget Policies and Budget Limits on Databricks
  • Using SQS With JMS for Legacy Applications
  • AWS Resources To Help You Get Started in the Cloud Journey
  • Mastering AWS Cost Management and Optimization

Trending

  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • S3 Vectors: How to Build a RAG Without a Vector Database
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • Bringing Intelligence Closer to the Source: Why Real-Time Processing is the Heart of Edge AI
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Run an Elastic MapReduce Job Using a Custom Jar on Amazon EMR

How to Run an Elastic MapReduce Job Using a Custom Jar on Amazon EMR

Learn how to develop an application using Java and the MapReduce framework for Hadoop, then execute that app in Amazon's Elastic MapReduce.

By 
Muhammad Ali Khojaye user avatar
Muhammad Ali Khojaye
·
Mar. 22, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
17.5K Views

Join the DZone community and get the full member experience.

Join For Free

Amazon EMR is a web service which allows developers to easily and efficiently process enormous amounts of data. It uses a hosted Hadoop framework running on the web-scale infrastructure of Amazon EC2 and Amazon S3.

Amazon EMR removes most of the cumbersome details of Hadoop, while taking care of provisioning Hadoop, running the job flow, terminating the job flow, moving the data between Amazon EC2 and Amazon S3, and optimizing Hadoop.

In this tutorial, we will first developed an example Java app (WordCount) using the MapReduce framework for Hadoop and, thereafter, we'll execute our program on Amazon Elastic MapReduce.

Prerequisites

You must have valid AWS account credentials. You should also have a general familiarity with using the Eclipse IDE before you begin. The reader can also use any other IDE of their choice.

Step 1 – Develop MapReduce WordCount Java Program

In this section, we are first going to develop our WordCount application. A WordCount program will determine how many times different words appear in a set of files.

  1. In Eclipse (or whatever the IDE you are using), create a simple Java project with the name "WordCount."
  2. Create a Java class named Map and override the map method as follows:
  3. Java
     




    xxxxxxxxxx
    1
    17


     
    1
    public class Map extends Mapper<longwritable, intwritable="" text,=""> {
    2
     private final static IntWritable one = new IntWritable(1);
    3
     private Text word = new Text();
    4
    
                
    5
     @Override
    6
     public void map(LongWritable key, Text value, Context context)
    7
       throws IOException, InterruptedException {
    8
      String line = value.toString();
    9
      StringTokenizer tokenizer = new StringTokenizer(line);
    10
      while (tokenizer.hasMoreTokens()) {
    11
       word.set(tokenizer.nextToken());
    12
       context.write(word, one);
    13
      }
    14
     }
    15
    }
    16
    </longwritable,>


  4. Create a Java class named Reduce and override the reduce method as shown below:
  5. Java
     




    xxxxxxxxxx
    1
    16


     
    1
    public class Reduce extends Reducer<text, intwritable,="" intwritable="" text,=""> {
    2
     @Override
    3
     protected void reduce(
    4
       Text key,
    5
       java.lang.Iterable<intwritable> values,
    6
       org.apache.hadoop.mapreduce.Reducer<text, intwritable,="" intwritable="" text,="">.Context context)
    7
       throws IOException, InterruptedException {
    8
      int sum = 0;
    9
      for (IntWritable value : values) {
    10
       sum += value.get();
    11
      }
    12
      context.write(key, new IntWritable(sum));
    13
     }
    14
    }
    15
    </text,></intwritable></text,>


  6. Create a Java class named WordCount and defined the main method as shown below:
  7. Java
     




    xxxxxxxxxx
    1
    21


     
    1
    public static void main(String[] args) throws Exception {
    2
      Configuration conf = new Configuration();
    3
    
                
    4
      Job job = new Job(conf, "wordcount");
    5
      job.setJarByClass(WordCount.class);
    6
    
                
    7
      job.setOutputKeyClass(Text.class);
    8
      job.setOutputValueClass(IntWritable.class);
    9
    
                
    10
      job.setMapperClass(Map.class);
    11
      job.setReducerClass(Reduce.class);
    12
    
                
    13
      job.setInputFormatClass(TextInputFormat.class);
    14
      job.setOutputFormatClass(TextOutputFormat.class);
    15
    
                
    16
      FileInputFormat.addInputPath(job, new Path(args[0]));
    17
      FileOutputFormat.setOutputPath(job, new Path(args[1]));
    18
    
                
    19
      job.waitForCompletion(true);
    20
    }


  8. Export the WordCount program in a jar using Eclipse and save it to some location on disk. Make sure that you have provided the Main Class (WordCount.jar) while extracting the jar file as shown below.

Step 2 – Upload the WordCount JAR and Input Files to Amazon S3

Now we are going to upload the WordCount jar to Amazon S3. First, go to the following URL: https://console.aws.amazon.com/s3/home. Next, click “Create Bucket,” give your bucket a name, and click the “Create” button. Select your new S3 bucket in the left-hand pane. Upload the WordCount JAR and sample input file for counting the words.

Step 3 – Running an Elastic MapReduce Job

Now that the JAR is uploaded into S3, all we need to do is to create a new Job flow. Let's execute the below steps (I encourage the reader to check out the following link for details regarding each step, How to Create a Job Flow Using a Custom JAR).

  • Sign in to the AWS Management Console and open the Amazon Elastic MapReduce console at https://console.aws.amazon.com/elasticmapreduce/
  • Click 'Create New Job Flow.'
  • In the DEFINE JOB FLOW page, enter the following details:
    • Job Flow Name = WordCountJob
    • Select 'Run your own application'
    • Select 'Custom JAR' in the drop-down list
    • Click 'Continue'
  • In the SPECIFY PARAMETERS page, enter values in the boxes using the following table as a guide, and then click Continue.
    • JAR Location = bucketName/jarFileLocation
    • JAR Arguments =
      • s3n://bucketName/inputFileLocation
      • s3n://bucketName/outputpath
      • Please note that the output path must be unique each time we execute the job. Hadoop always creates a folder with same name specified here.

After executing the job, just wait and monitor your job that runs through the Hadoop flow. You can also look for errors by using the Debug button. The job should be complete within 10 to 15 minutes (though this can also depend on the size of the input). After completing the job, you can view the results in the S3 Browser panel. You can also download the files from S3 and analyze the outcome of the job.

Amazon Elastic MapReduce Resources

  • Amazon Elastic MapReduce Documentation
  • Amazon Elastic MapReduce Getting Started Guide
  • Amazon Elastic MapReduce Developer Guide
  • Apache Hadoop
career hadoop MapReduce JAR (file format) AWS

Published at DZone with permission of Muhammad Ali Khojaye. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Implementing Budget Policies and Budget Limits on Databricks
  • Using SQS With JMS for Legacy Applications
  • AWS Resources To Help You Get Started in the Cloud Journey
  • Mastering AWS Cost Management and Optimization

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook