当前位置: 首页 > news >正文

国家级示范职业学校 建设网站北京债务优化公司

国家级示范职业学校 建设网站,北京债务优化公司,网站做微信链接怎么做,网站运营需要学什么一. OutputFormat简介 OutputFormat是MapReduce输出的基类,所有MapReduce输出都实现了OutputFormat接口,它接收ReduceTask产生的数据,然后将结果按照指定格式输出。 在MapReduce中,如果不指定,默认使用的是TextOutpu…
一. OutputFormat简介

OutputFormat是MapReduce输出的基类,所有MapReduce输出都实现了OutputFormat接口,它接收ReduceTask产生的数据,然后将结果按照指定格式输出。

在MapReduce中,如果不指定,默认使用的是TextOutputFormat。但是在一些特定的场景下,默认的TextOutputFormat不一定能满足我们的需求,因此可以自定义OutputFormat来实现个性化需求。

二. 需求

使用MapReduce对输入文件中的单词进行计数,单词"hello"的计数结果输出到hello.log中,非"hello"的单词的计数结果输出到non-hello.log。

要实现上面的输出需求,就需要自定义OutputFormat。

自定义OutputFormat的步骤:

  1. 自定义一个类继承FileOutputFormat。
  2. 自定义一个类继承RecordWriter,重写方法write()和close()。

代码实现

package mr;import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
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.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;class MultiOuputFormat extends FileOutputFormat<Text, IntWritable> {@Overridepublic RecordWriter<Text, IntWritable> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {Configuration configuration = job.getConfiguration();String outputPath = configuration.get(FileOutputFormat.OUTDIR);FileSystem fs = FileSystem.get(configuration);Path path1 = new Path(outputPath + "/hello.log");Path path2 = new Path(outputPath + "/non-hello.log");if (fs.exists(path1)) {fs.delete(path1, true);}if (fs.exists(path2)) {fs.delete(path2, true);}FSDataOutputStream out1 = fs.create(path1);FSDataOutputStream out2 = fs.create(path2);return new MyRecordWriter(out1, out2);}
}class MyRecordWriter extends RecordWriter<Text, IntWritable> {private FSDataOutputStream out1;private FSDataOutputStream out2;public MyRecordWriter(FSDataOutputStream out1, FSDataOutputStream out2) {super();this.out1 = out1;this.out2 = out2;}@Overridepublic void write(Text key, IntWritable value) throws IOException, InterruptedException {String outStr = key.toString() + "," + value.toString() + "\n";if (key.toString().contains("hello")) {out1.write(outStr.getBytes());} else {out2.write(outStr.getBytes());}}@Overridepublic void close(TaskAttemptContext context) throws IOException, InterruptedException {IOUtils.close(out1);IOUtils.close(out2);}
}public class WordCountOutputFormat {static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {@Overridepublic void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String[] words = value.toString().split(" ");for (String word: words) {context.write(new Text(word), new IntWritable(1));}}}static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {@Overridepublic void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int sum = 0;for (IntWritable val : values) {sum += val.get();}context.write(key, new IntWritable(sum));}}public static void main(String[] args) throws Exception {Configuration conf = new Configuration();Job job = Job.getInstance(conf);job.setJarByClass(WordCountOutputFormat.class);job.setJobName("WordCount");// 设置输入,输出路径FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));// 设置Mapperjob.setMapperClass(WordCountOutputFormat.WordCountMapper.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);// 设置Reducerjob.setReducerClass(WordCountOutputFormat.WordCountReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);job.setNumReduceTasks(1);job.setOutputFormatClass(MultiOuputFormat.class);boolean waitFor = job.waitForCompletion(true);System.exit(waitFor ? 0 : 1);}
}

运行结果

[root@hadoop1 ~]# yarn jar learn-1.0-SNAPSHOT.jar  mr.WordCountOutputFormat  /test/a.txt  /output# 查看输入文件
[root@hadoop1 ~]# hdfs dfs -text /test/a.txt
hello world
name hello
world# 查看结果文件
[root@hadoop1 ~]# hdfs dfs -ls /output
Found 3 items
-rw-r--r--   3 root supergroup          0 2024-10-29 21:52 /output/_SUCCESS
-rw-r--r--   3 root supergroup          8 2024-10-29 21:52 /output/hello.log
-rw-r--r--   3 root supergroup         15 2024-10-29 21:52 /output/non-hello.log
[root@hadoop1 ~]# hdfs dfs -text /output/hello.log
hello,2
[root@hadoop1 ~]# hdfs dfs -text /output/non-hello.log
name,1
world,2
http://www.yayakq.cn/news/380296/

相关文章:

  • 网站设计的简称石狮建设银行网站
  • 文明网站的建设与管理几点思考如何做网站的301重定向
  • 曲靖网站制作公司服务器网站打不开
  • 辽宁住房和城乡建设厅网站首页河南网站定制
  • 西宁市网站建设公司网站建设安全规划
  • 郑州汉狮哪家做网站好wordpress推送失败
  • 营销型网站框架图基于html5的美食网页设计
  • 引航博景做的网站网站建设 企业观点
  • 十堰网站seo方法seo搜索优化费用
  • 创建个人网站英文wordpress自动封面
  • 扬州网站seo赣州专业网站推广多少钱
  • 做h5的网站页面设计做电影网站怎么赚钱
  • 网站架设地址安阳论坛最新消息
  • 经典网站代码上海松江品划建设网站
  • 建设工程八大员考试网站企业网站建设方案价格
  • 钱多网站wordpress 商业主题插件
  • 做网站需要企业爬虫python入门
  • 在意派建设好网站后网站png小图标怎么做
  • 专业的教育行业网站制作抖音代运营费用一年多少钱
  • 德州手机网站建设费用微信小程序前端开发框架
  • 实用网站建设知识点成全视频免费高清观看在线动漫
  • wordpress快速仿站视频教程网站基本内容
  • 关于怎么做网站怎么做网站简单的
  • 地信的网站建设如何用vps做网站
  • 高邮网站建设免费网站设计素材
  • 网站建设客户常见问题展示中心展厅设计
  • 自营购物网站建设172分销系统
  • 怎样在网站上做链接wordpress弹出式视频
  • 网站授权管理系统怎么做装修公司名字大全参考
  • 铜仁市网站建设情况某商贸网站建设方案