先使用Hadoop自带的wordcount程序,针对license.txt进行词频统计;后使用Java编写MapReduce程序,实现特定文本的词频统计

一、 使用Hadoop自带的wordcount进行词频统计
·检查防火墙是否关闭:
在这里插入图片描述·创建1.txt文件,并把它上传到hdfs中:
在这里插入图片描述
在这里插入图片描述
·运行程序
在这里插入图片描述
·验证结果:
在这里插入图片描述

二、 使用java编写MapReducd,进行词频统计
·编写test.txt文件;并把它放入hdfs中:
在这里插入图片描述

·创建WordCount.java文件:
在这里插入图片描述
·编译运行:
在这里插入图片描述
·验证结果:
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,这个问题可以通过编写一个 MapReduce 程序来解决。以下是一个简单的 MapReduce 程序来对 Hadoop.txt 中各个单词出现的次数进行统计的示例: 1. Map 阶段 在 Map 阶段,我们将输入文件 Hadoop.txt 中的每一行分解成单词,并将每个单词映射到一个键值对中,其中键是单词本身,值为 1,表示该单词出现了一次。 ```java public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable ONE = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, ONE); } } } ``` 2. Reduce 阶段 在 Reduce 阶段,我们将相同单词的键值对合并,并将它们的值相加,以得到每个单词出现的总次数。 ```java public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public 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)); } } ``` 3. 驱动程序 在驱动程序中,我们指定输入输出路径,以及 Mapper 和 Reducer 类。 ```java public class WordCountDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCountDriver.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 以上代码可以将 Hadoop.txt 中各个单词出现的次数进行统计,并将结果输出到指定的输出路径中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风,风,风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值