Created
October 22, 2021 01:35
-
-
Save jogam5/27df6e7d27dee9588092db03071f109f to your computer and use it in GitHub Desktop.
Inverted Index using MapReduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package my.index; | |
import java.io.*; | |
import java.util.*; | |
import org.apache.hadoop.fs.Path; | |
import org.apache.hadoop.io.*; | |
import org.apache.hadoop.conf.Configuration; | |
import org.apache.hadoop.mapreduce.Job; | |
import org.apache.hadoop.mapreduce.Mapper; | |
import org.apache.hadoop.mapreduce.Reducer; | |
import java.util.ArrayList; | |
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; | |
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; | |
import org.apache.hadoop.io.Text; | |
public class InvertedIndex { | |
public static class Map extends Mapper<LongWritable, Text, Text, Text> { | |
private final Text keyOutput = new Text(); | |
private final Text valueOutput = new Text(); | |
public void map(LongWritable key, Text value, Context context) throws | |
IOException, InterruptedException { | |
/* | |
Remember that what is inside this method is run for every single line | |
in the text file. | |
*/ | |
//1. Read line | |
//2. Transform to string and split by "," | |
//3. Convert each string to tokens | |
//4. Loop over the tokens | |
//5. context.write() -> Reduce output: ({pancake,tweet1}, {day,tweet2}, ... ) | |
String line = value.toString(); | |
String[] lineArray = line.split(","); | |
String valueTmp = lineArray[0]; | |
StringTokenizer tokenizer = new StringTokenizer(lineArray[1]); | |
while (tokenizer.hasMoreTokens()) { | |
keyOutput.set(tokenizer.nextToken()); | |
valueOutput.set(valueTmp); | |
context.write(keyOutput, valueOutput); | |
} | |
} | |
} | |
public static class Reduce extends Reducer<Text, Text, Text, Text> { | |
private final Text keyOutput = new Text(); | |
private final Text valueOutput = new Text(); | |
public void reduce(Text key, Iterable<Text> values, Context context) throws | |
IOException, InterruptedException{ | |
/* | |
Remember you are given a unique key and all its associated values. You | |
loop over each "key:{all values}" once. | |
*/ | |
//1. Create ArrayList | |
//2. Loop over values and add them to ArrayList | |
//3. context.write() -> Reduce output: | |
// {pancake, [tweet1, tweet23, twee87]}, {sun, [tweet2,..]} | |
List<String> listValues = new ArrayList<>(); | |
for (Text val : values) { | |
listValues.add(val.toString()); | |
} | |
keyOutput.set(key); | |
valueOutput.set(listValues.toString()); | |
context.write(keyOutput, valueOutput); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
Configuration conf = new Configuration(); | |
Job job = Job.getInstance(conf, "invertedIndex"); | |
job.setJarByClass(InvertedIndex.class); | |
job.setOutputKeyClass(Text.class); | |
job.setOutputValueClass(Text.class); | |
job.setMapperClass(InvertedIndex.Map.class); | |
job.setReducerClass(InvertedIndex.Reduce.class); | |
FileInputFormat.addInputPath(job, new Path(args[0])); | |
FileOutputFormat.setOutputPath(job, new Path(args[1])); | |
job.waitForCompletion(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment