/**
* 单词计数
*/
public class LocalTridentCount { public static class MyBatchSpout implements IBatchSpout { Fields fields;
HashMap<Long, List<List<Object>>> batches = new HashMap<Long, List<List<Object>>>(); public MyBatchSpout(Fields fields) {
this.fields = fields;
}
@Override
public void open(Map conf, TopologyContext context) {
} @Override
public void emitBatch(long batchId, TridentCollector collector) {
List<List<Object>> batch = this.batches.get(batchId);
if(batch == null){
batch = new ArrayList<List<Object>>();
Collection<File> listFiles = FileUtils.listFiles(new File("d:\\stormtest"), new String[]{"txt"}, true);
for (File file : listFiles) {
List<String> readLines;
try {
readLines = FileUtils.readLines(file);
for (String line : readLines) {
batch.add(new Values(line));
}
FileUtils.moveFile(file, new File(file.getAbsolutePath()+System.currentTimeMillis()));
} catch (IOException e) {
e.printStackTrace();
} }
if(batch.size()>0){
this.batches.put(batchId, batch);
}
}
for(List<Object> list : batch){
collector.emit(list);
}
} @Override
public void ack(long batchId) {
this.batches.remove(batchId);
} @Override
public void close() {
} @Override
public Map getComponentConfiguration() {
Config conf = new Config();
conf.setMaxTaskParallelism(1);
return conf;
} @Override
public Fields getOutputFields() {
return fields;
} } /**
* 对一行行的数据进行切割成一个个单词
*/
public static class MySplit extends BaseFunction{ @Override
public void execute(TridentTuple tuple, TridentCollector collector) {
String line = tuple.getStringByField("lines");
String[] words = line.split("\t");
for (String word : words) {
collector.emit(new Values(word));
}
} } public static class MyWordAgge extends BaseAggregator<Map<String, Integer>>{ @Override
public Map<String, Integer> init(Object batchId,
TridentCollector collector) {
return new HashMap<String, Integer>();
} @Override
public void aggregate(Map<String, Integer> val, TridentTuple tuple,
TridentCollector collector) {
String key = tuple.getString(0);
/*Integer integer = val.get(key);
if(integer==null){
integer=0;
}
integer++;
val.put(key, integer);*/
val.put(key, MapUtils.getInteger(val, key, 0)+1);
} @Override
public void complete(Map<String, Integer> val,
TridentCollector collector) {
collector.emit(new Values(val));
} } /**
* 汇总局部的map,并且打印结果
*
*/
public static class MyCountPrint extends BaseFunction{ HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
@Override
public void execute(TridentTuple tuple, TridentCollector collector) {
Map<String, Integer> map = (Map<String, Integer>)tuple.get(0);
for (Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
Integer integer = hashMap.get(key);
if(integer==null){
integer=0;
}
hashMap.put(key, integer+value);
} Utils.sleep(1000);
System.out.println("==================================");
for (Entry<String, Integer> entry : hashMap.entrySet()) {
System.out.println(entry);
}
} } public static void main(String[] args) {
//大体流程:首先设置一个数据源MyBatchSpout,会监控指定目录下文件的变化,当发现有新文件的时候把文件中的数据取出来,
//然后封装到一个batch中发射出来.就会对tuple中的数据进行处理,把每个tuple中的数据都取出来,然后切割..切割成一个个的单词.
//单词发射出来之后,会对单词进行分组,会对一批假设有10个tuple,会对这10个tuple分完词之后的单词进行分组, 相同的单词分一块
//分完之后聚合 把相同的单词使用同一个聚合器聚合 然后出结果 每个单词出现多少次...
//进行汇总 先每一批数据局部汇总 最后全局汇总....
//这个代码也不是很简单...挺多....就是使用批处理的方式. TridentTopology tridentTopology = new TridentTopology(); tridentTopology.newStream("spoutid", new MyBatchSpout(new Fields("lines")))
.each(new Fields("lines"), new MySplit(), new Fields("word"))
.groupBy(new Fields("word"))//用到了分组 对一批tuple中的单词进行分组..
.aggregate(new Fields("word"), new MyWordAgge(), new Fields("wwwww"))//用到了聚合
.each(new Fields("wwwww"), new MyCountPrint(), new Fields("")); LocalCluster localCluster = new LocalCluster();
String simpleName = TridentMeger.class.getSimpleName();
localCluster.submitTopology(simpleName, new Config(), tridentTopology.build());
}
}

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

程序运行结果:

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. Spring Boot 入门实践

    一.Eclipse配置Spring Boot环境 1.查看eclipse版本信息: 2.登录:http://spring.io/tools/sts/all 看eclipse对应的插件版本对应的ecli ...

  2. IntelliJ IDEA 2017版 spring-boot2.0.4的集成JSP

    一.必须依赖四个包,其中三个是springboot自带包,可以不写版本号,有一个不在springboot中,需要设置版本号 <!--引入Spring Boot内嵌的Tomcat对Jsp的解析包- ...

  3. 好文推荐系列---------(4)使用Yeoman自动构建Ember项目

    好文原文地址:http://segmentfault.com/a/1190000000368881 我决定学习前端开发的效率工具Yeoman.本文将首先介绍Yeoman的基本情况,接着我们会使用Yeo ...

  4. What's New In Python 3.X

    As Python updating to python 3.6, its performance is better than Python 2.x, which is good news to e ...

  5. eclipse环境搭建(插件安装)

    转自:http://www.iteye.com/topic/982182 使用eclipse真的有年头了,相信java程序员没有不知道它的,最近在给团队中新来的应届生做指导,专门讲解了一下Eclips ...

  6. POJ3045--Cow Acrobats(theory proving)

    Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the ...

  7. Java包、权限访问修饰符、封装性

    包 概念: 物理上是文件夹:逻辑上是有逻辑关系的类的集合 作用: 避免类重名:控制访问权限 命名规范: 在包名中,可以使用.号来区分包的级别:包名一般情况下是小写 第一级 指该项目的类型,如com,o ...

  8. C++ 中的continue理解

    continue的在循环中的作用: 1. 跳过当前循环,但是还需要执行自增条件, 如下程序:当i == 3时,执行i++, 即if判定{}执行完毕,则i==4, 然后 for最后一条语句i++, 然后 ...

  9. iOS 使用代码创建约束,实现自动布局

    ///与下面约束对象属性截图相对应//使用Auto Layout约束,禁止将Autoresizing Mask转换为约束 [self.funcView setTranslatesAutoresizin ...

  10. 面向对象的设计原则(JAVA)

    一.单一职责原则(Single Responsibility Principe,SRP)      1.1单一职责原则的定义 1)定义:在软件系统中,一个类只负责一个功能领域中的相应职责. 2)另一种 ...