其下函数:设置job名,输出键的类,输出值的类, Mapper类,
设置用户定义的Combiner类(去在map-outputs被发送到reducer之前进行整合),
设置Reducer类,
设置输入格式类(用来合法化job的输入格式、对输入做出逻辑上的划分然后把每个划分分配给各个Mapper、提供RecordReader的实现去收集为Mapper的处理而进行的逻辑划分所得到的输入记录),特别地,TextInputFormat是普通文本文件,文件被分为若干行,key是在文件中的位置,value是文本行。
设置输出格式类(描述输出格式、检测输出是否合法、提供RecorWriter去写job的输出文件)
2.JobClient 是用户和集群交互的基本接口,提供提交job的设施、跟踪他们的执行、获取成员task的日志信息、获取MapReduce的集群状态。
3.Mapper
将输入键值对映射为中间的键值对,是将输入记录转内化为中间记录的独立的task,每一个逻辑划分都被Hadoop MapReduce框架赋予一个task;mapper的实现是通过Job.setMapperClass()传递给job的,然后框架调用map函数获得该task的每个键值对,输出键值对通过context.write收集;
Mapper 输出被排序后按每一个Reducer划分,划分个数便是reduce task个数;用户可以选择指定一个combiner去进行中间输出的本地整合,这样可以减少Mapper到Reducer的数据转移量,中间的已排序输出总是被通过简单的格式存储;
MapReduceBase是
Mapper和
Reducer
的基类,实现包含Map方法的泛型接口。
(一些小的注意点:
IntWritable 是int数据类型的实现WritableComparable接口的类,该接口继承Writable、Comparable,通常是key值的实现接口,
Writable
通常是任何key或value的实现接口;
Text存储标准UTF8编码的内容,提供序列化、反序列化、字节级比较的方法;
OutPutCollector收集中间输出-即Mapper输出和job的输出-即Reducer输出。)
4.Reducer
有三个阶段shuffle、sort、reduce。reduce task 的结果通过Context 被写到文件系统。Reducer的输出没有拍下
HDFS命令:
我写的温湿度数据处理:
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
public class Min {
public static class MinHumidityMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
try {
String line = value.toString();
String[] ss = line.split("\t");//每行数据记录以制表符间隔
String date = ss[0];//日期值
String humidity = ss[3];//湿度值
int humidityValue = Integer.parseInt(humidit y);
context.write(new Text(date), new IntWritable(humidityValue));
//将日期作为键,整型湿度值作为值,建立键值对
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class MinHumidityReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int minValue = Integer.MAX_VALUE;
for (IntWritable value : values) {//遍历每个键的所有值,找出最小的
minValue = Math.min(minValue, value.get());
}
context.write(key, new IntWritable(minValue));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (args.length != 2) {
System.err.println("Usage: MinTemperature<input path> <output path>");
System.exit(-1);
}
Job job = new Job(conf, "Min value");
job.setJarByClass(Min.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
job.setMapperClass(MinHumidityMapper.class);
job.setReducerClass(MinHumidityReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
其下函数:设置job名,输出键的类,输出值的类, Mapper类,
设置用户定义的Combiner类(去在map-outputs被发送到reducer之前进行整合),
设置Reducer类,
设置输入格式类(用来合法化job的输入格式、对输入做出逻辑上的划分然后把每个划分分配给各个Mapper、提供RecordReader的实现去收集为Mapper的处理而进行的逻辑划分所得到的输入记录),特别地,TextInputFormat是普通文本文件,文件被分为若干行,key是在文件中的位置,value是文本行。
设置输出格式类(描述输出格式、检测输出是否合法、提供RecorWriter去写job的输出文件)
2.JobClient 是用户和集群交互的基本接口,提供提交job的设施、跟踪他们的执行、获取成员task的日志信息、获取MapReduce的集群状态。
3.Mapper
将输入键值对映射为中间的键值对,是将输入记录转内化为中间记录的独立的task,每一个逻辑划分都被Hadoop MapReduce框架赋予一个task;mapper的实现是通过Job.setMapperClass()传递给job的,然后框架调用map函数获得该task的每个键值对,输出键值对通过context.write收集;
Mapper 输出被排序后按每一个Reducer划分,划分个数便是reduce task个数;用户可以选择指定一个combiner去进行中间输出的本地整合,这样可以减少Mapper到Reducer的数据转移量,中间的已排序输出总是被通过简单的格式存储;
MapReduceBase是
Mapper和
Reducer
的基类,实现包含Map方法的泛型接口。
(一些小的注意点:
IntWritable 是int数据类型的实现WritableComparable接口的类,该接口继承Writable、Comparable,通常是key值的实现接口,
Writable
通常是任何key或value的实现接口;
Text存储标准UTF8编码的内容,提供序列化、反序列化、字节级比较的方法;
OutPutCollector收集中间输出-即Mapper输出和job的输出-即Reducer输出。)
4.Reducer
有三个阶段shuffle、sort、reduce。reduce task 的结果通过Context 被写到文件系统。Reducer的输出没有拍下
HDFS命令:
我写的温湿度数据处理:
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
public class Min {
public static class MinHumidityMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
try {
String line = value.toString();
String[] ss = line.split("\t");//每行数据记录以制表符间隔
String date = ss[0];//日期值
String humidity = ss[3];//湿度值
int humidityValue = Integer.parseInt(humidit y);
context.write(new Text(date), new IntWritable(humidityValue));
//将日期作为键,整型湿度值作为值,建立键值对
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class MinHumidityReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int minValue = Integer.MAX_VALUE;
for (IntWritable value : values) {//遍历每个键的所有值,找出最小的
minValue = Math.min(minValue, value.get());
}
context.write(key, new IntWritable(minValue));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (args.length != 2) {
System.err.println("Usage: MinTemperature<input path> <output path>");
System.exit(-1);
}
Job job = new Job(conf, "Min value");
job.setJarByClass(Min.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
job.setMapperClass(MinHumidityMapper.class);
job.setReducerClass(MinHumidityReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}