I need full code answear of this question Question 7 Implement a pair of classes
ID: 3605885 • Letter: I
Question
I need full code answear of this question
Question 7 Implement a pair of classes: CounterReaderthatcount the numberof times a particular character, such as e, is read. Make sure to override all the read() functions of the FileReader class. The character can be specified when the stream is created. 2. CounterWriter that count the number of times a particular character, such as b, is written. Make sure to override all the write() functions of the FileReader class. The character can be specified when the stream is created. Write a CounterDemo driver class to test your classes. 3. You can use the "input.txt" to read from and write its content into another file "output.txt". 4.Explanation / Answer
Question in not clear, i mean to be implemented in which programming language. As with point no. 3 to write a driver class, i have given the hadoop mapreducer code and the steps of execution.
CounterReader.java
//This is the mapper class
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 CounterReader extends Mapper<LongWritable, Text, Text, IntWritable>
{
public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException
{
String line = value.toString();
char[] charr = line.toCharArray();
for (char c : charr)
{
System.out.println(c);
context.write(new Text(String.valueOf(c)), new IntWritable(1));
}
}
}
CounterWriter.java
//This is the reducer class which matches and counts only the characters a,e,i,o,u. Code can be extended to all //other caharacters
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class CounterWriter extends Reducer<Text, IntWritable, Text, IntWritable>
{
public void reduce(Text key, Iterable<IntWritable> values, Context context)throws IOException,InterruptedException
{
int count = 0;
IntWritable res = new IntWritable();
for (IntWritable val : values)
{
count +=val.get();
res.set(count);
}
String found = key.toString();
if (found.equals("a") || found.equals("e") || found.equals("i") || found.equals("0") || found.equals("u"))
{
context.write(key, result);
}
}
}
CounterDemo.java
//This is the driver code which takes input.txt a text file of your own data and outputs output file
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class CounterDemo
{
public static void main(String[] args) throws Exception
{
if (args.length != 2)
{
System.out.println("usage: [input] [output]");
System.exit(-1);
}
Configuration conf = new Configuration();
Job job = new Job(conf, "Character Count");
job.setJarByClass(CounterDemo.class);
job.setMapperClass(CounterReader.class);
job.setReducerClass(CounterWriter..class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
execution: to be executed in hadoop 2.7 and above
Create directory count(mkdir directoryname)
After completed typing the java code/program in notepad
open terminal
3 programs CounterReader.java, CounterWriter.java and CounterDemo.java should be saved inside your directory which was created
Change directory(cd directoryname)
NOTE:
After this if it shows namenode is in safemode then give the following command.
hdfs dfsadmin -safemode leave
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.