NLineInputFormat的示例分析
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,小编给大家分享一下NLineInputFormat的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!package com.test;import java.io.IOExce
千家信息网最后更新 2026年10月31日NLineInputFormat的示例分析
小编给大家分享一下NLineInputFormat的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
package com.test;import java.io.IOException;import java.util.Iterator;import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;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.input.NLineInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;import org.apache.hadoop.util.Tool;import org.apache.hadoop.util.ToolRunner;/** * TextInputFormat处理的数据来自于一个InputSplit。InputSplit根据块大小划分。 * 由于每条记录有长有短,所以,每个map任务处理的记录数都不一样 * NLineInputFormat决定每个map处理记录数是相同的 */public class WordCountNL extends Configured implements Tool { public static class Map extends Mapper { public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer st = new StringTokenizer(line); while(st.hasMoreElements()) { context.write(new Text(st.nextElement().toString()), new IntWritable(1)); } } } public static class Combiner extends Reducer { public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { int count = 0; Iterator it = values.iterator(); while(it.hasNext()) { count = count + it.next().get(); } context.write(key, new IntWritable(count)); } } public static class Reduce extends Reducer { public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { int count = 0; Iterator it = values.iterator(); while(it.hasNext()) { count = count + it.next().get(); } context.write(key, new IntWritable(count)); } } public int run(String[] args) throws IOException, InterruptedException, ClassNotFoundException { Configuration conf = this.getConf(); //设置每个map可以处理多少行数据 //conf.set("mapreduce.input.lineinputformat.linespermap", "1"); conf.set(NLineInputFormat.LINES_PER_MAP, "1"); Job job = new Job(conf); job.setJobName(WordCountNL.class.getSimpleName()); job.setJarByClass(WordCountNL.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setMapperClass(Map.class); job.setCombinerClass(Combiner.class); job.setReducerClass(Reduce.class); job.setInputFormatClass(NLineInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.waitForCompletion(true); return job.isSuccessful()?0:1; } public static void main(String[] args) throws Exception { int exit = ToolRunner.run(new WordCount(), args); System.exit(exit); } } 看完了这篇文章,相信你对"NLineInputFormat的示例分析"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
处理
示例
分析
数据
篇文章
相同
有长有短
任务
大小
完了
更多
知识
行业
资讯
资讯频道
频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
科技金融是不是互联网金融
科技与互联网
互联网下的科技发展
互联网 科技公司
互联网科技集团
嘟嘟瓜互联网科技
极限挑战互联网科技
远拓互联网科技
深圳齐家互联网科技
银优互联网科技有限公司
科技互联网的估值
小米互联网科技园设置
随着互联网科技发展
金融科技与互联网金融相同和不同
互联网科技有哪些公司
米聚互联网科技南京有限公司排面
电子科技大学互联网创业大赛
互联网相关科技技术
广州互联网高科技取餐柜
科技发展第三阶段互联网时代
互联网科技垄断
朗新科技与工业互联网
重庆互联网络科技
菲比寻常互联网科技
全球顶尖互联网科技公司
南博望互联网科技有限公司
卡尔迅互联网科技有限公司
银川新华互联网科技学校是国营吗
互联网金融金融科技
北京瑞云互联网科技有限公司