pom文件

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4.  
  5. <groupId>com.zuoyan</groupId>
  6. <artifactId>hadoop</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9.  
  10. <name>hadoop</name>
  11. <url>http://maven.apache.org</url>
  12.  
  13. <properties>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. </properties>
  16.  
  17. <dependencies>
  18. <dependency>
  19. <groupId>junit</groupId>
  20. <artifactId>junit</artifactId>
  21. <version>3.8.1</version>
  22. </dependency>
  23. <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
  24. <dependency>
  25. <groupId>org.apache.hadoop</groupId>
  26. <artifactId>hadoop-client</artifactId>
  27. <version>3.0.0</version>
  28. </dependency>
  29. <!-- https://mvnrepository.com/artifact/com.janeluo/ikanalyzer -->
  30. <dependency>
  31. <groupId>com.janeluo</groupId>
  32. <artifactId>ikanalyzer</artifactId>
  33. <version>2012_u6</version>
  34. </dependency>
  35. </dependencies>
  36. <build>
  37. <plugins>
  38. <plugin>
  39. <artifactId>maven-assembly-plugin</artifactId>
  40. <configuration>
  41. <appendAssemblyId>false</appendAssemblyId>
  42. <descriptorRefs>
  43. <descriptorRef>jar-with-dependencies</descriptorRef>
  44. </descriptorRefs>
  45. <archive>
  46. <manifest>
  47. <!-- 此处指定main方法入口的class -->
  48. <mainClass>com.zuoyan.hadoop.FirstMapReduceJob</mainClass>
  49. <!-- <mainClass>com.geotmt.hadoop.hdfs.FirstMapReduceJob</mainClass> -->
  50. </manifest>
  51. </archive>
  52. </configuration>
  53. <executions>
  54. <execution>
  55. <id>make-assembly</id>
  56. <phase>package</phase>
  57. <goals>
  58. <goal>assembly</goal>
  59. </goals>
  60. </execution>
  61. </executions>
  62. </plugin>
  63. <plugin>
  64. <groupId>org.apache.maven.plugins</groupId>
  65. <artifactId>maven-compiler-plugin</artifactId>
  66. <version>3.6.2</version>
  67. <configuration>
  68. <source>1.8</source>
  69. <target>1.8</target>
  70. <encoding>UTF-8</encoding>
  71. </configuration>
  72. </plugin>
  73. </plugins>
  74. </build>
  75. </project>

  

单词计数-实现

  1. package com.zuoyan.hadoop;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.Reader;
  7.  
  8. import org.apache.hadoop.conf.Configuration;
  9. import org.apache.hadoop.fs.Path;
  10. import org.apache.hadoop.io.IntWritable;
  11. import org.apache.hadoop.io.Text;
  12. import org.apache.hadoop.mapreduce.Job;
  13. import org.apache.hadoop.mapreduce.Mapper;
  14. import org.apache.hadoop.mapreduce.Reducer;
  15. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  16. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  17. import org.apache.hadoop.util.GenericOptionsParser;
  18. import org.wltea.analyzer.core.IKSegmenter;
  19. import org.wltea.analyzer.core.Lexeme;
  20.  
  21. /**
  22. * 单词计数
  23. *
  24. */
  25. public class FirstMapReduceJob {
  26.  
  27. public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{
  28.  
  29. private final static IntWritable one = new IntWritable(1);
  30. private Text word = new Text();
  31.  
  32. public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  33. /*
  34. * 默认英文分词
  35. *
  36. StringTokenizer itr = new StringTokenizer(value.toString());
  37. while (itr.hasMoreTokens()) {
  38. word.set(itr.nextToken());
  39. context.write(word, one);
  40. }
  41. */
  42. /*
  43. * 中文分词-使用IK分词器分词
  44. */
  45. byte[] bytes = value.getBytes();
  46. InputStream inputStream = new ByteArrayInputStream(bytes);
  47. Reader reader = new InputStreamReader(inputStream);
  48. IKSegmenter iKSegmenter = new IKSegmenter(reader,true);
  49. Lexeme t;
  50. while((t=iKSegmenter.next()) != null){
  51. context.write(new Text(t.getLexemeText()), new IntWritable(1));
  52. }
  53.  
  54. //方案二,获取文件信息
  55. // context.getInputSplit().getLocationInfo();
  56.  
  57. }
  58. }
  59.  
  60. public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
  61. private IntWritable result = new IntWritable();
  62.  
  63. public void reduce(Text key, Iterable<IntWritable> values,Context context ) throws IOException, InterruptedException {
  64. int sum = 0;
  65. for (IntWritable val : values) {
  66. sum += val.get();
  67. }
  68. result.set(sum);
  69. context.write(key, result);
  70. }
  71. }
  72.  
  73. public static void main(String[] args) throws Exception {
  74. Configuration conf = new Configuration();
  75. String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  76. if (otherArgs.length != 2) {
  77. System.err.println("Usage: wordcount <in> <out>");
  78. System.exit(2);
  79. }
  80. Job job = new Job(conf, "word count");
  81. job.setJarByClass(FirstMapReduceJob.class);
  82. job.setMapperClass(TokenizerMapper.class);
  83. job.setCombinerClass(IntSumReducer.class);
  84. job.setReducerClass(IntSumReducer.class);
  85. job.setOutputKeyClass(Text.class);
  86. job.setOutputValueClass(IntWritable.class);
  87. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  88. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  89. System.exit(job.waitForCompletion(true) ? 0 : 1);
  90. }
  91. }

  

单词计数-MapReduceJob的更多相关文章

  1. 使用Scala实现Java项目的单词计数:串行及Actor版本

    其实我想找一门“具有Python的简洁写法和融合Java平台的优势, 同时又足够有挑战性和灵活性”的编程语言. Scala 就是一个不错的选择. Scala 有很多语言特性, 建议先掌握基础常用的: ...

  2. MapReduce之单词计数

    最近在看google那篇经典的MapReduce论文,中文版可以参考孟岩推荐的 mapreduce 中文版 中文翻译 论文中提到,MapReduce的编程模型就是: 计算利用一个输入key/value ...

  3. 自定义实现InputFormat、OutputFormat、输出到多个文件目录中去、hadoop1.x api写单词计数的例子、运行时接收命令行参数,代码例子

    一:自定义实现InputFormat *数据源来自于内存 *1.InputFormat是用于处理各种数据源的,下面是实现InputFormat,数据源是来自于内存. *1.1 在程序的job.setI ...

  4. Storm实现单词计数

    package com.mengyao.storm; import java.io.File; import java.io.IOException; import java.util.Collect ...

  5. hadoop笔记之MapReduce的应用案例(WordCount单词计数)

    MapReduce的应用案例(WordCount单词计数) MapReduce的应用案例(WordCount单词计数) 1. WordCount单词计数 作用: 计算文件中出现每个单词的频数 输入结果 ...

  6. 第一章 flex单词计数程序

    学习Flex&Bison目标, 读懂SQLite中SQL解析部分代码 Flex&Bison简介Flex做词法分析Bison做语法分析 第一个Flex程序, wc.fl, 单词计数程序 ...

  7. Strom的trident单词计数代码

    /** * 单词计数 */ public class LocalTridentCount { public static class MyBatchSpout implements IBatchSpo ...

  8. 大数据【四】MapReduce(单词计数;二次排序;计数器;join;分布式缓存)

       前言: 根据前面的几篇博客学习,现在可以进行MapReduce学习了.本篇博客首先阐述了MapReduce的概念及使用原理,其次直接从五个实验中实践学习(单词计数,二次排序,计数器,join,分 ...

  9. python实现指定目录下批量文件的单词计数:并发版本

    在 文章 <python实现指定目录下批量文件的单词计数:串行版本>中, 总体思路是: A. 一次性获取指定目录下的所有符合条件的文件 -> B. 一次性获取所有文件的所有文件行 - ...

随机推荐

  1. CentOS7.X安装FastDFS-5.10

    安装准备 yum install \ vim \ git \ gcc \ gcc-c++ \ wget \ make \ libtool \ automake \ autoconf \ -y \ 安装 ...

  2. vm虚拟机用批处理启动和关闭

    title vmware 虚拟机开启中 cls&&echo 正在开启VMware虚拟机,请稍候... "D:\vmware\vmware.exe" -x " ...

  3. linux RZ 命令

    root 账号登陆后,依次执行以下命令: 1 cd /tmp 2 wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20.tar.gz 3 tar zxv ...

  4. thphp(tp5)项目网站从Apache换成nginx报500

    thphp(tp5)项目网站从Apache换成nginx报500 百度了一下,查看资料是Nginx配置fastcgi.conf的问题,打开文件编辑既可,如下图:

  5. LR为什么用极大似然估计,损失函数为什么是log损失函数(交叉熵)

    首先,逻辑回归是一个概率模型,不管x取什么值,最后模型的输出也是固定在(0,1)之间,这样就可以代表x取某个值时y是1的概率 这里边的参数就是θ,我们估计参数的时候常用的就是极大似然估计,为什么呢?可 ...

  6. 线段树(two value)与树状数组(RMQ算法st表)

    士兵杀敌(三) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进行比 ...

  7. 查找idt table 所對應的page table in Linux

    #include <linux/kernel.h> #include <linux/module.h> #include <linux/types.h> #incl ...

  8. P4126 [AHOI2009]最小割(网络流+tarjan)

    P4126 [AHOI2009]最小割 边$(x,y)$是可行流的条件: 1.满流:2.残量网络中$x,y$不连通 边$(x,y)$是必须流的条件: 1.满流:2.残量网络中$x,S$与$y,T$分别 ...

  9. JVM(10)之 年老代收集器

    开发十年,就只剩下这套架构体系了! >>>   在上一篇博文我们介绍了JAVA新生代收集器,本篇博文我们要讲的就是关于老年代的一些收集器.老年代存活的一般是大对象以及生命很顽强的对象 ...

  10. 【记录】ajax 设置请求header的Content-Type 为 application/json;charset=utf8

    具体案例如下 $.ajax({ url: context.state.IpccSendIm, method: 'POST', data: JSON.stringify(val), headers:{' ...