Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ASSIGNMENT

NAME: DEBASHIS ROY

CSE202203003

1. "Hello world"

import java.io.IOException;

import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;

public class HelloWorldMapper 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 tokenizer = new StringTokenizer(value.toString());

while (tokenizer.hasMoreTokens()) {

word.set(tokenizer.nextToken());

context.write(word, one);

2. Do a text search on large Data File or grep on Map-Reduce

import java.io.IOException;

import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Reducer;
public class GrepMapper extends Mapper<Object, Text, Text, IntWritable> {

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

private Text word = new Text();

private String searchTerm;

public void setup(Context context) throws IOException, InterruptedException {

searchTerm = context.getConfiguration().get("searchTerm");

public void map(Object key, Text value, Context context) throws IOException,
InterruptedException {

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

while (tokenizer.hasMoreTokens()) {

word.set(tokenizer.nextToken());

if (word.toString().equals(searchTerm)) {

context.write(word, one);

public class GrepReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

private IntWritable total = new IntWritable();

public void reduce(Text key, Iterable<IntWritable> values, Context context) throws


IOException, InterruptedException {

int sum = 0;
for (IntWritable value : values) {

sum += value.get();

total.set(sum);

context.write(key, total);

3. A full-fledged solution for a problem:

Finding the prime numbers less than or equal to a given number in Java:

import java.util.ArrayList;

import java.util.List;

public class PrimeNumbers {

public static List<Integer> findPrimeNumbers(int n) {

List<Integer> primeNumbers = new ArrayList<>();

for (int i = 2; i <= n; i++) {

boolean isPrime = true;

for (int j = 2; j * j <= i; j++) {

if (i % j == 0) {

isPrime = false;

break;

}
if (isPrime) {

primeNumbers.add(i);

return primeNumbers;

public static void main(String[] args) {

List<Integer> primeNumbers = findPrimeNumbers(100);

for (int primeNumber : primeNumbers) {

System.out.println(primeNumber);

You might also like