1. /**
  2. * 单词计数
  3. */
  4. public class LocalTridentCount {
  5.  
  6. public static class MyBatchSpout implements IBatchSpout {
  7.  
  8. Fields fields;
  9. HashMap<Long, List<List<Object>>> batches = new HashMap<Long, List<List<Object>>>();
  10.  
  11. public MyBatchSpout(Fields fields) {
  12. this.fields = fields;
  13. }
  14. @Override
  15. public void open(Map conf, TopologyContext context) {
  16. }
  17.  
  18. @Override
  19. public void emitBatch(long batchId, TridentCollector collector) {
  20. List<List<Object>> batch = this.batches.get(batchId);
  21. if(batch == null){
  22. batch = new ArrayList<List<Object>>();
  23. Collection<File> listFiles = FileUtils.listFiles(new File("d:\\stormtest"), new String[]{"txt"}, true);
  24. for (File file : listFiles) {
  25. List<String> readLines;
  26. try {
  27. readLines = FileUtils.readLines(file);
  28. for (String line : readLines) {
  29. batch.add(new Values(line));
  30. }
  31. FileUtils.moveFile(file, new File(file.getAbsolutePath()+System.currentTimeMillis()));
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35.  
  36. }
  37. if(batch.size()>0){
  38. this.batches.put(batchId, batch);
  39. }
  40. }
  41. for(List<Object> list : batch){
  42. collector.emit(list);
  43. }
  44. }
  45.  
  46. @Override
  47. public void ack(long batchId) {
  48. this.batches.remove(batchId);
  49. }
  50.  
  51. @Override
  52. public void close() {
  53. }
  54.  
  55. @Override
  56. public Map getComponentConfiguration() {
  57. Config conf = new Config();
  58. conf.setMaxTaskParallelism(1);
  59. return conf;
  60. }
  61.  
  62. @Override
  63. public Fields getOutputFields() {
  64. return fields;
  65. }
  66.  
  67. }
  68.  
  69. /**
  70. * 对一行行的数据进行切割成一个个单词
  71. */
  72. public static class MySplit extends BaseFunction{
  73.  
  74. @Override
  75. public void execute(TridentTuple tuple, TridentCollector collector) {
  76. String line = tuple.getStringByField("lines");
  77. String[] words = line.split("\t");
  78. for (String word : words) {
  79. collector.emit(new Values(word));
  80. }
  81. }
  82.  
  83. }
  84.  
  85. public static class MyWordAgge extends BaseAggregator<Map<String, Integer>>{
  86.  
  87. @Override
  88. public Map<String, Integer> init(Object batchId,
  89. TridentCollector collector) {
  90. return new HashMap<String, Integer>();
  91. }
  92.  
  93. @Override
  94. public void aggregate(Map<String, Integer> val, TridentTuple tuple,
  95. TridentCollector collector) {
  96. String key = tuple.getString(0);
  97. /*Integer integer = val.get(key);
  98. if(integer==null){
  99. integer=0;
  100. }
  101. integer++;
  102. val.put(key, integer);*/
  103. val.put(key, MapUtils.getInteger(val, key, 0)+1);
  104. }
  105.  
  106. @Override
  107. public void complete(Map<String, Integer> val,
  108. TridentCollector collector) {
  109. collector.emit(new Values(val));
  110. }
  111.  
  112. }
  113.  
  114. /**
  115. * 汇总局部的map,并且打印结果
  116. *
  117. */
  118. public static class MyCountPrint extends BaseFunction{
  119.  
  120. HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
  121. @Override
  122. public void execute(TridentTuple tuple, TridentCollector collector) {
  123. Map<String, Integer> map = (Map<String, Integer>)tuple.get(0);
  124. for (Entry<String, Integer> entry : map.entrySet()) {
  125. String key = entry.getKey();
  126. Integer value = entry.getValue();
  127. Integer integer = hashMap.get(key);
  128. if(integer==null){
  129. integer=0;
  130. }
  131. hashMap.put(key, integer+value);
  132. }
  133.  
  134. Utils.sleep(1000);
  135. System.out.println("==================================");
  136. for (Entry<String, Integer> entry : hashMap.entrySet()) {
  137. System.out.println(entry);
  138. }
  139. }
  140.  
  141. }
  142.  
  143. public static void main(String[] args) {
  144. //大体流程:首先设置一个数据源MyBatchSpout,会监控指定目录下文件的变化,当发现有新文件的时候把文件中的数据取出来,
  145. //然后封装到一个batch中发射出来.就会对tuple中的数据进行处理,把每个tuple中的数据都取出来,然后切割..切割成一个个的单词.
  146. //单词发射出来之后,会对单词进行分组,会对一批假设有10个tuple,会对这10个tuple分完词之后的单词进行分组, 相同的单词分一块
  147. //分完之后聚合 把相同的单词使用同一个聚合器聚合 然后出结果 每个单词出现多少次...
  148. //进行汇总 先每一批数据局部汇总 最后全局汇总....
  149. //这个代码也不是很简单...挺多....就是使用批处理的方式.
  150.  
  151. TridentTopology tridentTopology = new TridentTopology();
  152.  
  153. tridentTopology.newStream("spoutid", new MyBatchSpout(new Fields("lines")))
  154. .each(new Fields("lines"), new MySplit(), new Fields("word"))
  155. .groupBy(new Fields("word"))//用到了分组 对一批tuple中的单词进行分组..
  156. .aggregate(new Fields("word"), new MyWordAgge(), new Fields("wwwww"))//用到了聚合
  157. .each(new Fields("wwwww"), new MyCountPrint(), new Fields(""));
  158.  
  159. LocalCluster localCluster = new LocalCluster();
  160. String simpleName = TridentMeger.class.getSimpleName();
  161. localCluster.submitTopology(simpleName, new Config(), tridentTopology.build());
  162. }
  163. }

指定路径下文件中的内容:

程序运行结果:

Strom的trident单词计数代码的更多相关文章

  1. Storm官方提供的trident单词计数的例子

    上代码: public class TridentWordCount { public static class Split extends BaseFunction { @Override publ ...

  2. Strom实现单词统计代码

    import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.HashM ...

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

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

  4. storm(5)-分布式单词计数例子

    例子需求: spout:向后端发送{"sentence":"my dog has fleas"}.一般要连数据源,此处简化写死了. 语句分割bolt(Split ...

  5. MapReduce之单词计数

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

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

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

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

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

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

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

  9. Storm实现单词统计代码

    import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.HashM ...

随机推荐

  1. lambda表达式(c++11)

    1.概念 1)lambda表达式是一个可调用的代码单元,它由一个捕获列表.一个参数列表.一个箭头.一个返回类型.一个函数体组成: 2)可以忽略参数列表和返回类型,但必须包含捕获列表和函数体: 3)忽略 ...

  2. HDU 5212 Code (莫比乌斯反演)

    题意:给定上一个数组,求 析: 其中,f(d)表示的是gcd==d的个数,然后用莫比乌斯反演即可求得,len[i]表示能整队 i 的个数,可以线性筛选得到, 代码如下: #pragma comment ...

  3. 安卓 build/core/Makefile 以及main.mk

    android make 系统总共分为四层 arch board device product 在各个字android.mk文件中引用的定义都存放在./build/core/下!比如android.m ...

  4. 第82讲:Scala中List的ListBuffer是如何实现高效的遍历计算的?

    今天学习下list中的ListBuffer实现的高效计算.让我们先来看下代码 def main(args:Array[String]){        val list = List(1,2,3,4, ...

  5. 一篇谈Flink不错的文章

    精华 : 在执行引擎这一层,流处理系统与批处理系统最大不同在于节点间的数据传输方式.对于一个流处理系统,其节点间数据传输的标准模型是:当一条数据被处理完成后,序列化到缓存中,然后立刻通过网络传输到下一 ...

  6. 转:Ubuntu 10.10 安装后上不了网的原因

    最近新装了个Ubuntu10.10 发现上不了网,折腾了很久,在网上找了很多办法都不行,最后试了一招居然管用了.特此总结下Ubuntu了网的原因及对策分析. 环境:Ubuntu 10.10网络: 通过 ...

  7. double float的精度问题

    三部曲 1: #include <iostream> #include <stdio.h> #include <string.h> using namespace ...

  8. PL/SQL客户端连Oracle很快就断开问题的解决

    PL/SQL登录很短时间session就自动断开 1.首先查看你这个用户的profile文件 select profile from dba_users where username='USERNAM ...

  9. Android-Failed to open zip file

    当AndroidStudio加载工程的时候,出现以下错误❌: 解决前的工程目录: 1.将以上错误认真的分析: 2.找到工程的 gradle文件夹/wrapper文件夹/gradle-wrapper.p ...

  10. C#导入Excel数据常见问题

    今天在做一个excle数据导入的时候遇到了一个奇葩问题,项目使用的是MVC,在VS2010里面调试的时候没有问题,可是当发布到本地IIS或服务器上时就出现了问题: 1.excel文件正在被使用: 2. ...