|
[schou@maya-usr1 ~]$ start_hadoop ... === hdfs229969 === ...
[schou@maya-usr1 ~]$ scontrol show lic hadoop
LicenseName=hadoop
Total=16 Used=0 Free=16 Remote=no
$ module load default-environment $ module load hadoop/[instance name here]
import java.io.IOException;
import java.util.StringTokenizer;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCount {
public static class TokenizerMapper
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 itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.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);
}
}
$ hadoop com.sun.tools.javac.Main WordCount.java $ jar cf wc.jar WordCount*.class
$ hadoop fs -mkdir /user/slet1/input $ hadoop fs -put test.txt /user/slet1/input/
$ hadoop fs -ls /user/slet1/input Found 1 items -rw------- 3 slet1 hadoop 82 2015-04-08 14:24 /user/slet1/input/test.txt
$ hadoop jar wc.jar WordCount /user/slet1/input/test.txt /user/slet1/output
Once the job completes, do an 'ls' on the directory that you wrote your output to and you should see something close to the following:
$ hadoop fs -ls /user/slet1/output Found 2 items -rw------- 3 slet1 hadoop 0 2015-04-08 14:28 /user/slet1/output/_SUCCESS -rw------- 3 slet1 hadoop 88 2015-04-08 14:28 /user/slet1/output/part-r-00000
$ hadoop fs -cat /user/slet1/output/part-r-00000 Goodbye 1 Hello 1 world 2
$ hadoop fs -get /user/slet1/output/part-r-00000 output.txt $ cat output.txt Goodbye 1 Hello 1 world 2
$ hadoop fs -rm -r /user/slet1/input /user/slet1/output
import java.io.IOException;
import java.util.StringTokenizer;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordSearch {
public static class TokenizerMapper
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 itr = new StringTokenizer(value.toString());
String search_phrase = context.getConfiguration().get( "search" );
while (itr.hasMoreTokens() ) {
word.set(itr.nextToken().toLowerCase());
if( word.toString().equals( search_phrase ) )
context.write(word, one);
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set( "search", args[ 2 ] );
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordSearch.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.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);
}
}
$ hadoop com.sun.tools.javac.Main WordSearch.java $ jar cf ws.jar WordSearch*.class
$ hadoop fs -mkdir /user/slet1/input $ hadoop fs -put const.txt /user/slet1/input/ $ hadoop fs -put decl.txt /user/slet1/input/
$ hadoop fs -ls /user/slet1/input Found 2 items -rw------- 3 slet1 hadoop 45771 2015-04-08 16:37 /user/slet1/input/const.txt -rw------- 3 slet1 hadoop 8031 2015-04-08 16:37 /user/slet1/input/decl.txt
$ hadoop jar ws.jar WordSearch /user/slet1/input/* /user/slet1/output the
$ hadoop fs -cat /user/slet1/output/* the 802
Same as before, to clean up the directories after having run our job, type the following:
$ hadoop fs -rm -r /user/slet1/input /user/slet1/output