统计/var/log/boot.log中含k的字符的数量,并对含k的字符按照数量排序。需分两个job完成,一个用来统计,一个用来排序。

一、统计

1、上传文件到hadoop:
   1)新建文件夹:hadoop fs -mkdir /tmp/fjs
   2)上传文件:hadoop fs -put /var/log/boot.log /tmp/fjs

2、编写wordcount代码并导出jar和上传到namenode
   1)挂载共享文件夹,上传jar包:mount -t cifs //ip/tmp /mnt -o username=xxx,password=xxx
   2)移动jar包到tmp目录下:cp -R /mnt/wordcount.jar /tmp
   3)jar包是root权限,更改给hadoop用户:chown -R hdfs:hdfs /tmp/wordcount.jar

代码如下:

package com;import java.io.IOException;
import java.util.StringTokenizer;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;public class WordCount {public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{private final static IntWritable one = new IntWritable(1);private Text word = new Text();public void map(Object key, Text value, Context context)throws IOException, InterruptedException {StringTokenizer itr = new StringTokenizer(value.toString());while (itr.hasMoreTokens()) {String strVal=itr.nextToken();//获取字符//if(strVal.contains("k")){//如果字符包含k,则统计word.set(strVal);context.write(word, one);//}}}}public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {private IntWritable result = new IntWritable();public void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {int sum = 0;for (IntWritable val : values) {sum += val.get();}result.set(sum);context.write(key, result);}}public static void main(String[] args) throws Exception {Configuration conf = new Configuration();String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();if (otherArgs.length != 2) {System.err.println("Usage: wordcount <in> <out>");System.exit(2);}Job job = new Job(conf, "word count");job.setJarByClass(WordCount.class);job.setMapperClass(TokenizerMapper.class);job.setCombinerClass(IntSumReducer.class);job.setReducerClass(IntSumReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);FileInputFormat.addInputPath(job, new Path(otherArgs[0]));FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));System.exit(job.waitForCompletion(true) ? 0 : 1);}}

3、执行wordcount.jar并查看结果
   1)执行:yarn jar /tmp/wordcount.jar /tmp/fjs /tmp/fjs/out
   2)查看:hadoop fs -text /tmp/fjs/out/part-r-0000.bz2

二、排序

1、编写wordsort代码并导出jar和上传namenode,对wordcount执行的结果进行排序;
   排序就是利用mapreduce本身的key排序功能,主要是互换key和value。
   代码如下:

package com;
import java.io.IOException;
import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration;
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;public class WordSort {public static class SortIntValueMapper extends Mapper<LongWritable, Text, IntWritable, Text>{private final static IntWritable wordCount = new IntWritable(1);private Text word = new Text();public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {StringTokenizer tokenizer = new StringTokenizer(value.toString());while (tokenizer.hasMoreTokens()) {word.set(tokenizer.nextToken().trim());wordCount.set(Integer.valueOf(tokenizer.nextToken().trim()));context.write(wordCount, word);//<k,v>互换}}}public static class SortIntValueReduce extends  Reducer<IntWritable, Text, Text, IntWritable> {private Text result = new Text();public void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {for (Text val : values) {result.set(val.toString());context.write(result, key);//<k,v>互换}}}public static void main(String[] args) throws Exception {Configuration conf = new Configuration();String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();if (otherArgs.length != 2) {System.err.println("Usage: wordsort <in> <out>");System.exit(2);}Job job = new Job(conf, "word sort");job.setJarByClass(WordSort.class);job.setMapperClass(SortIntValueMapper.class);job.setReducerClass(SortIntValueReduce.class);job.setOutputKeyClass(IntWritable.class);job.setOutputValueClass(Text.class);FileInputFormat.addInputPath(job, new Path(otherArgs[0]));FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));System.exit(job.waitForCompletion(true) ? 0 : 1);}
}

2、执行wordsort.jar并查看结果
   1)执行:yarn jar /tmp/wordsort.jar /tmp/fjs/out /tmp/fjs/out1
   2)查看:hadoop fs -text /tmp/fjs/out1/part-r-0001.bz2

MapReduce基础开发之一词汇统计和排序(wordcount)相关推荐

  1. MapReduce基础开发之二数据去重和排序

    因Hadoop集群平台网络限制,只能在eclipse里先写好代码再提交jar到集群平台namenode上执行,不能实时调试,所以没有配置eclipse的hadoop开发环境,只是引入了hadoop的l ...

  2. Hadoop实战——MapReduce对英文单词文本进行统计和排序(超详细教学,算法分析)

    B站视频操作过程 Hadoop实战--对单词文本进行统计和排序_哔哩哔哩_bilibili 更多MapReduce设计案例地址 https://github.com/yuanprogrammer/Ma ...

  3. MapReduce基础开发之十读写ORC File

    1.ORC File Orc是Hive特有的一种列式存储的文件格式,它有着非常高的压缩比和读取效率,因此很快取代了之前的RCFile,成为Hive中非常常用的一种文件格式. 2.编译ORC Jar包 ...

  4. MapReduce基础开发之六Map多输入

    在实际MapReduce开发中,会遇到需要数据多输入路径并有对应的Map函数来处理,这需要MultipleInputs.addInputPath(job, path, inputFormatClass ...

  5. MapReduce基础开发之三字段处理并输出Hive表

    1.MR设计和开发    1)设计:      输入:用户名 | 数字ip | 时间戳 |  url      MR处理:正则表达式匹配url,满足则解析url并转换ip和时间戳,      输出:用 ...

  6. MapReduce基础开发之十二ChainMapper和ChainReducer使用

    1.需求场景:    过滤无意义的单词后再进行文本词频统计.处理流程是: 1)第一个Map使用无意义单词数组过滤输入流: 2)第二个Map将过滤后的单词加上出现一次的标签: 3)最后Reduce输出词 ...

  7. MapReduce基础开发之十一DistributedCache使用

    1.需求场景:    过滤无意义的单词后再进行文本词频统计.处理流程是: 1)预定义要过滤的无意义单词保存成文件,保存到HDFS中: 2)程序中将该文件定位为作业的缓存文件,使用Distributed ...

  8. MapReduce基础开发context.write注意new text()多出一列的问题

    1.问题描述:在MapReduce中代码中,Map输出context.write(okey,new text("")),Reduce也是context.write(okey,new ...

  9. MapReduce基础开发之八HDFS文件CRUD操作

    HDFS文件操作的基础代码. package com.hive;import java.io.BufferedInputStream; import java.io.BufferedOutputStr ...

最新文章

  1. 解决Python OpenCV 读取视频并抽帧出现error while decoding的问题
  2. 对3维数组中间一维进行操示例
  3. Unity3D 多人协作开发 环境搭建 笔记(场景合并)
  4. 游标卡尺尺身的刻度间距为_【物业】游标卡尺使用及读数方法
  5. Spring Boot 2.x 新特性总结及迁移指南
  6. spring-kafka整合:DefaultKafkaProducerFactory默认kafka生产者工厂介绍
  7. Mybatis框架(待完善)
  8. java 多线程基础, 我觉得还是有必要看看的
  9. PureCode--iOS--自定义UITableViewCell(含疑问)
  10. Vue之webpack之基本使用
  11. C#: 数字经纬度和度分秒经纬度间的转换
  12. c语言汉字属于什么类型_C语言为什么需要定义数据类型
  13. html圆圈男女,html 圆形符号
  14. 【noiOJ】p7939
  15. Python之——获取电脑连接过的所有wifi名称和密码
  16. 一年当中几月份买车最合适?什么时候最便宜?
  17. 劳动仲裁解除劳动关系要多长时间
  18. Devops之制品库平台nexus实践
  19. TwinCAT3 控制器PLC之间EAP通讯1-主机给多个从机发送
  20. CentOS8 图形界面和命令行切换

热门文章

  1. include详解 shell_Linux 系统结构详解,看这一篇就够了
  2. MySQL的sql语句分类汇总
  3. Win2008 R2 WEB 服务器设置之禁用不必要的服务和关闭端口
  4. win7_iis报500.19和500.21错误问题解决
  5. Windows7 apache启动失败的解决方法
  6. dede后台title怎么修改的?去掉XXXX-织梦内容管理系统V5.7
  7. Android 简单记事本
  8. 启动vm虚拟机里的系统时,提示此主机支持 AMD-V,但 AMD-V 处于禁用状态。
  9. P3390矩阵快速幂
  10. error_reporting()函数用法