统计单词数
1.输入内容
hello world i am teacher
hello world i am teacher
hello world i am teacher
hello world i am teacher
hello world
hello world
2.输出内容
am 4
hello 6
i 4
teacher 4
world 6
3.代码
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;
import java.io.IOException;
import java.util.StringTokenizer;
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();
@Override
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
System.out.println("切分split后数据--偏移量:" + key + "\t值:" + value);
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();
@Override
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("E:\\HadoopMRData\\input"));
FileOutputFormat.setOutputPath(job, new Path("E:\\HadoopMRData\\output"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
去除重复项
1.输入内容
11
11
11
12
13
2.输出内容
11
12
13
3.代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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;
import java.io.IOException;
public class RemoveRepeat {
public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
System.out.println("切分split后数据--偏移量:" + key + "\t值:" + value);
context.write(new Text(value), new Text(""));
}
}
public static class MyReducer extends Reducer<Text, Text, Text, Text> {
@Override
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
context.write(key, new Text(""));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "remove repeat");
job.setJarByClass(RemoveRepeat.class);
job.setMapperClass(MyMapper.class);
job.setCombinerClass(MyReducer.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("E:\\HadoopMRData\\input"));
FileOutputFormat.setOutputPath(job, new Path("E:\\HadoopMRData\\output"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
去重并两种方式输出序号
1.输入内容
6
16
8
12
5
7
6
6
2.输出内容
1 1 5
2 2 6
3 5 7
4 6 8
5 7 12
6 8 16
3.代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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;
import java.io.IOException;
public class Order {
public static class MyMapper extends Mapper<LongWritable, Text, IntWritable, Text> {
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
System.out.println("切分split后数据--偏移量:" + key + "\t值:" + value);
int va = Integer.parseInt(value.toString());
context.write(new IntWritable(va), new Text("1"));
}
}
public static class MyReducer extends Reducer<IntWritable, Text, IntWritable, Text> {
private int num1 = 1;
private int num2 = 1;
@Override
protected void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
context.write(new IntWritable(num1), new Text(num2 + "\t" + key.get()));
for (Text val : values) {
num2++;
}
num1++;
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "order");
job.setJarByClass(Order.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("E:\\HadoopMRData\\input"));
FileOutputFormat.setOutputPath(job, new Path("E:\\HadoopMRData\\output"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
同一个学生的平均值
1.输入内容
张三 98
李四 94
王五 89
张三 86
李四 92
王五 86
张三 82
李四 90
2.输出内容
张三 88.66666666666667
李四 89.5
王五 89.54166666666667
3.代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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;
import java.io.IOException;
import java.util.StringTokenizer;
public class Average {
public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
System.out.println("切分split后数据--偏移量:" + key + "\t值:" + value);
StringTokenizer tokenizer = new StringTokenizer(value.toString());
while (tokenizer.hasMoreTokens()) {
context.write(new Text(tokenizer.nextToken()), new Text(tokenizer.nextToken()));
}
}
}
public static class MyReducer extends Reducer<Text, Text, Text, Text> {
private int count = 0;
private double sum = 0;
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
for (Text val : values) {
sum += Double.parseDouble((val.toString()));
count++;
}
context.write(key, new Text(String.valueOf(sum / count)));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "average");
job.setJarByClass(Average.class);
job.setMapperClass(MyMapper.class);
job.setCombinerClass(MyReducer.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("E:\\HadoopMRData\\input"));
FileOutputFormat.setOutputPath(job, new Path("E:\\HadoopMRData\\output"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}