【hadoop 学习笔记-3】wordcount 统计小说天龙八部中汉字出现的频率
说明
天龙八部小说,3MB+ 中文 140W字以上。
用wordcount 统计一下每个不同的汉字 / 词汇出现的次数。
Mapper
package com.test;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;import java.io.*;public class CNMapper extends Mapper<LongWritable, Text, Text, IntWritable> {@Overrideprotected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {String data = value.toString();//System.out.println(data);String[] words = data.split("");for (String word : words) {//System.out.println(word);context.write(new Text(word), new IntWritable(1));}}
}
Reducer
package com.test;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class AReducer extends Reducer<Text, IntWritable, Text, IntWritable> {@Overrideprotected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {//super.reduce(key, values, context);int total = 0;for (IntWritable v : values) {total += v.get();}context.write(key, new IntWritable(total));}
}
Main
package com.test;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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class CNMain {public static void main(String[] args) throws Exception {Job job = Job.getInstance( new Configuration());job.setJarByClass(CNMain.class);job.setMapperClass(CNMapper.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);job.setReducerClass(AReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));job.waitForCompletion(true);}}
测试
先用李后主的 《相见欢·林花谢了春红》做一下测试
林花谢了春红,太匆匆。无奈朝来寒雨晚来风。胭脂泪,相留醉,几时重。自是人生长恨水长东。
测试结果:
接下来跑一下 天龙八部:
排序:
k2rn
2 第二列
r 降序
n 以树值的方式排序
结果展示:
所以,第一名是 不
加上jieba 库做一下词频统计
修改mapper
package com.test;import com.huaban.analysis.jieba.JiebaSegmenter;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;
import java.util.List;public class CNJiebaMapper extends Mapper<LongWritable, Text, Text, IntWritable> {@Overrideprotected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {String data = value.toString();JiebaSegmenter segmenter = new JiebaSegmenter();//System.out.println(data);List<String> words = segmenter.sentenceProcess(data);for (String word : words) {//System.out.println(word);context.write(new Text(word), new IntWritable(1));}}
}
修改Main
package com.test;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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class CNJiebaMain {public static void main(String[] args) throws Exception {Job job = Job.getInstance( new Configuration());job.setJarByClass(CNJiebaMain.class);job.setMapperClass(CNJiebaMapper.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);job.setReducerClass(AReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));job.waitForCompletion(true);}}
完整的POM文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>mywordcount</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.7.3</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>2.7.3</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-mapreduce-client-core</artifactId><version>2.7.3</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>2.7.3</version></dependency><dependency><groupId>com.janeluo</groupId><artifactId>ikanalyzer</artifactId><version>2012_u6</version></dependency><dependency><groupId>com.huaban</groupId><artifactId>jieba-analysis</artifactId><version>1.0.2</version></dependency></dependencies><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.18.1</version><configuration><skipTests>true</skipTests></configuration></plugin><!--打包插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>2.4.1</version><configuration><!-- get all project dependencies --><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs><!-- MainClass in mainfest make a executable jar --><archive><manifest><addClasspath>true</addClasspath><mainClass>com.test.CNJiebaMain</mainClass></manifest></archive></configuration><executions><execution><id>make-assembly</id><!-- bind to the packaging phase --><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build></project>
测试结果
还是先上李后主的 相见欢:
再上天龙八部:
注意:运行 jar with dependencies.jar 时,不需要指定main 类。
排序后显示前50名,可以看到 段誉 上榜:
两个汉字以上:
三个汉字以上:
四个汉字???
五个???
== 完 ==
【hadoop 学习笔记-3】wordcount 统计小说天龙八部中汉字出现的频率相关推荐
- Hadoop学习笔记—4.初识MapReduce
一.神马是高大上的MapReduce MapReduce是Google的一项重要技术,它首先是一个 编程模型 ,用以进行大数据量的计算.对于大 数据量的计算,通常采用的处理手法就是并行计算.但对许多开 ...
- Hadoop学习笔记—11.MapReduce中的排序和分组
Hadoop学习笔记-11.MapReduce中的排序和分组 一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出 ...
- Hadoop学习笔记(1) ——菜鸟入门
Hadoop学习笔记(1) --菜鸟入门 Hadoop是什么?先问一下百度吧: [百度百科]一个分布式系统基础架构,由Apache基金会所开发.用户能够在不了解分布式底层细节的情况下.开发分布式 ...
- Hadoop学习笔记(1)
原文:http://www.cnblogs.com/zjfstudio/p/3859704.html Hadoop学习笔记(1) --菜鸟入门 Hadoop是什么?先问一下百度吧: [百度百科]一个分 ...
- Hadoop学习笔记—20.网站日志分析项目案例(一)项目介绍
Hadoop学习笔记-20.网站日志分析项目案例(一)项目介绍 网站日志分析项目案例(一)项目介绍:当前页面 网站日志分析项目案例(二)数据清洗:http://www.cnblogs.com/edis ...
- Hadoop学习笔记—15.HBase框架学习(基础知识篇)
Hadoop学习笔记-15.HBase框架学习(基础知识篇) HBase是Apache Hadoop的数据库,能够对大型数据提供随机.实时的读写访问.HBase的目标是存储并处理大型的数据.HBase ...
- Hadoop学习笔记—16.Pig框架学习
Hadoop学习笔记-16.Pig框架学习 一.关于Pig:别以为猪不能干活 1.1 Pig的简介 Pig是一个基于Hadoop的大规模数据分析平台,它提供的SQL-LIKE语言叫Pig Latin, ...
- Hadoop学习笔记—13.分布式集群中节点的动态添加与下架
Hadoop学习笔记-13.分布式集群中节点的动态添加与下架 开篇:在本笔记系列的第一篇中,我们介绍了如何搭建伪分布与分布模式的Hadoop集群.现在,我们来了解一下在一个Hadoop分布式集群中,如 ...
- Hadoop学习笔记一 简要介绍
Hadoop学习笔记一 简要介绍 这里先大致介绍一下Hadoop. 本文大部分内容都是从官网Hadoop上来的.其中有一篇介绍HDFS的pdf文档,里面对Hadoop介绍的比较全面了.我的这一 ...
最新文章
- 计算几何常用算法的理论基础【转】
- C#中Trim()、TrimStart()、TrimEnd()的错误认识
- c++读取图片_Pytorch读取,加载图像数据(一)
- [Windows] Windows 8.1 取消在任务栏显示应用商店的应用
- 15-07-10 结构体-输入学生成绩进行排序
- 如何在Intellij IDEA中集成Gitlab
- tee:结果输出到文件同时也作为往后的输入信息
- warning C4819: The file contains a character that cannot be represented in the current code page
- NSArray 的遍历
- 使用express重构博客项目
- Unity清除一个物体所有的子物体
- 华为HG255D路由破解方法
- uc android快捷键,UC手机浏览器助力Android快速上网
- 搭建本地私有pip源
- 盘点好用的 Markdown 编辑器
- 千亿美妆市场“野蛮生长”,悦瞳靠“假睫毛”能否“翘”动市场?
- 解决:TypeError: Descriptors cannot not be created directly
- 记一次被“呼死你”电话骚扰的反骚扰经历
- Excel的照相机功能(转载+亲自实践)
- 最强量子计算机争霸:谷歌和IBM,谁是真正赢家?