Posts

Showing posts from January, 2021

MapReduce Character Count

 Mapper Logic : import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class CharCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {   @Override   public void map(LongWritable key, Text value, Context con)       throws IOException, InterruptedException {     String line = value.toString();     char[] chars = line.toCharArray();     for(char c:chars) {     con.write(new Text("Total Characters"), new IntWritable(1));     }   } } Reducer Logic : import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class CharCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {   /*    * bigdata   <1>    */   @Override   public void reduce(Text key, Iterable<IntWritable> values,Context con)       throws

MapReuce K-Mean Clustering

import java.io.IOException; import java.util.*; import java.io.*; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.filecache.DistributedCache; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.*; import org.apache.hadoop.mapred.*; import org.apache.hadoop.mapred.Reducer;   @SuppressWarnings("deprecation") public class KMean {     public static String OUT = "outfile";     public static String IN = "inputlarger";     public static String CENTROID_FILE_NAME = "/centroid.txt";     public static String OUTPUT_FILE_NAME = "/part-00000";     public static String DATA_FILE_NAME = "/data.txt";     public static String JOB_NAME = "KMeans";     public static String SPLITTER = "t| ";     public static List<Double> mCenters = new ArrayList<Double>();     public static class Map extends MapReduceBase implements Mapper<LongWritable, Te

MapReduce Matrix Multiplication Code

 Mapper Logic  : import java.io.IOException;  import org.apache.hadoop.conf.*;  import org.apache.hadoop.io.*; import org.apache.hadoop.mapreduce.*; public class MatrixMapper extends Mapper<LongWritable, Text, Text, Text>  { public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException  { Configuration conf = context.getConfiguration();  int m = Integer.parseInt(conf.get("m")); int p = Integer.parseInt(conf.get("p"));  String line = value.toString(); String[] indicesAndValue = line.split(",");  Text outputKey = new Text(); Text outputValue = new Text(); if (indicesAndValue[0].equals("M"))  {  for (int k = 0; k < p; k++) { outputKey.set(indicesAndValue[1] + "," + k); outputValue.set("M," + indicesAndValue[2] + "," + indicesAndValue[3]);  context.write(outputKey, outputValue); } }  else  { for (int i = 0; i < m; i++)  { outputKey.set(i + "," + indicesAnd

Word Count MapReduce Code

  Mapper Logic: import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {   @Override   public void map (LongWritable key, Text value, Context con)       throws IOException, InterruptedException {     String line = value.toString();     String[] words = line.split("\\s");     for(String s:words) {                 con.write(new Text(s), new IntWritable(1));     }   } }     Reducer Logic: import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {     @Override   public void reduce (Text key, Iterable&l