MapReduce 的格式输入----NLineInputFormat
来源:互联网 发布:奚梦瑶天涯扒皮知乎 编辑:程序博客网 时间:2024/05/22 13:43
通过TextInputFormat和keyvalueTextInputformat,每个Mapper收到的输入的行数不同,行数取决于分片的大小和行的长度,如果希望mapper收到固定的行数的输入,需要将NLineInputFormat作为InputFormat使用,与TextInputFormat一样,键是行的字节的偏移量,值是行本身。
1、WordCountMapper
package hadoop.mr.input.nline;
import com.it18zhang.debugtool.RTUtil;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/**
* WordCountMapper
*/
public class WordCountMapper extends Mapper<LongWritable, Text, Text,IntWritable> {
/**
* 每一行
*/
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] arr = line.split(" ");
Text keyOut = new Text();
IntWritable valueOut = new IntWritable(1);
for(String word : arr){
keyOut.set(word);
context.write(keyOut,valueOut);
}
}
}
2、WordCountReducer
package .hadoop.mr.input.nline;
import com.it18zhang.debugtool.RTUtil;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/**
* WordCountReducer
*/
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int count = 0 ;
for(IntWritable iw : values){
count = count + iw.get() ;
}
context.write(key,new IntWritable(count));
}
}
3、App
package c.hadoop.mr.input.nline;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.NLineInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
*/
public class App {
public static void main(String[] args) throws Exception {
args = new String[]{"d:/java/mr/data/6.txt" , "d:/java/mr/out"} ;
Configuration conf = new Configuration();
conf.set("fs.defaultFS","file:///");
conf.set("mapreduce.framework.name","local");
FileSystem fs = FileSystem.get(conf);
if(fs.exists(new Path(args[1]))){
fs.delete(new Path(args[1]),true);
}
Job job = Job.getInstance(conf);
job.setJobName("WordCount");
job.setJarByClass(App.class);
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
FileInputFormat.addInputPath(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
//n行输入格式
job.setInputFormatClass(NLineInputFormat.class);
//修改N行的数量
job.getConfiguration().setInt(NLineInputFormat.LINES_PER_MAP,3);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setNumReduceTasks(2);
job.waitForCompletion(true);
}
}
阅读全文
0 0
- MapReduce 的格式输入----NLineInputFormat
- MapReduce的NlineInputFormat
- Mapreduce的输入格式
- Mapreduce的输入格式
- mapreduce的输入格式详解
- MapReduce类的输入文件的格式
- MapReduce 的格式输入----SequenceFileInputFormat ---源码分析
- MapReduce的输入格式---KeyValueTextInputFormat---源码分析
- MapReduce自定义输入格式
- MapReduce多种输入格式
- MapReduce 的格式输入----MultipleInputs多个输入
- Hadoop学习笔记(一):MapReduce的输入格式
- 【Hadoop】MapReduce输入输出格式之输入格式
- MapReduce的输入输出格式
- MapReduce的输入输出格式
- MapReduce的输入输出格式
- MapReduce的输入输出格式
- MapReduce的输入输出格式
- NSLineBreakMode 的区别
- 输出最高分数的学生姓名
- 【CUGBACM15级BC第23场 A】hdu 5146 Sequence
- Lua 模拟面向对象
- python下双星号的意义
- MapReduce 的格式输入----NLineInputFormat
- 外部过滤器,程序和命令--混杂命令
- TCPNIOSocket通道
- 【CUGBACM15级BC第23场 B】hdu 5147 Sequence II
- 文件的上传与下载
- python中reshape的负参数
- js中事件绑定,$("body").on("click")与$("").click()区别
- MapReduce 的格式输入----MultipleInputs多个输入
- show()和exec()的区别