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

关于建筑工程的网站青岛seo排名扣费

关于建筑工程的网站,青岛seo排名扣费,北京微信网站开发费用,免费html网站代码一. 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/592838/

相关文章:

  • 建设个人银行网站商城网站设计图
  • 网站域名备案需要什么品牌宣传型企业网站
  • 北京网站建设 网络推广上海网站建设就q479185700顶上
  • 网站建设哈尔滨网站优化4wordpress会员插件开发
  • 营销型网站建设推荐编写网站策划方案
  • 网站内容编辑怎么做后台管理系统页面模板
  • 电商类网站设计模板怎么在手机上做微电影网站
  • 宁波网站推广软件建e室内设计网 3d模型医美前台
  • 湘潭什么网站做c1题目wordpress英文版切换中文
  • 邯郸wap网站建设价格重庆专业网站推广公司
  • 华建河北住房和城乡建设厅网站河北建设厅网站开通账号
  • 自己如何建一个网站奥门网站建设
  • 网站建设优化怎么做网站建设公司美工如何找
  • 代做论文网站网站建设的目标的意思
  • wordpress淘宝客主题带条件筛选郑州网站seo外包
  • 网站备案需要ftp吗iis7 网站无法显示该页面
  • 网站网站建设娄底高端网站建设
  • 科技企业网站源码下载网站页面架构怎么写
  • 咸宁网站建设多少钱公司怎么制作网站
  • 梦织做网站泰安企业建站公司服务
  • 什么样建广告网站百度联盟注册
  • 网站开发与维护的相关大学网站设计任务书范文
  • 网盘做网站创意设计团队
  • 厂家做网站企业网站网上推广的途径
  • 网上做流量对网站有什么影响吗肇庆建网站服务
  • 网络调查问卷在哪个网站做网站备案 英文
  • 建立网站每项内容可以设计成什么乡镇中心小学校园网站建设指南
  • 做网络课程的网站宁波公司网站开发
  • 乐清做网站培训如何网上做个人广告
  • 网站建设公司专业网站企业开发文山州中小企业网站建设