1.如何让一个spout并行读取多个流?
方法:任何spout、bolts组件都可以访问TopologyContext。利用这个特性可以让Spouts的实例之间划分流。
示例:获取到storm集群spouts并行度的大小,和这个spout实例的Task Index,让输入的tracks[]的多个流合理地分到某一个spout实例中。这样就可以实现一个spout并行读取多个流。
//open()方法中
int spoutsSize = context.getComponentTasks(context.getThisComponentId()).size();
int myIdx = context.getThisTaskIndex();
String[] tracks = ((String) conf.get("track")).split(",");

一个完整的Spout code:输入参数track代表了多个流,在open()方法中用取模%初始化track,在execute()方法读取track的数据,发送。由于spout的多个实例的myIdx不同,它们可以获得各自的一个track,可以实现一个spout读取多个流。

 //ApiStreamingSpout.java
package twitter.streaming; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue; import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException; import backtype.storm.spout.SpoutOutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichSpout;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Values; public class ApiStreamingSpout extends BaseRichSpout { static String STREAMING_API_URL = "https://stream.twitter.com/1/statuses/filter.json?track=";
private String track;
private String user;
private String password;
private DefaultHttpClient client;
private SpoutOutputCollector collector;
private UsernamePasswordCredentials credentials;
private BasicCredentialsProvider credentialProvider; LinkedBlockingQueue<String> tweets = new LinkedBlockingQueue<String>(); static Logger LOG = Logger.getLogger(ApiStreamingSpout.class);
static JSONParser jsonParser = new JSONParser(); @Override
public void nextTuple() {
/*
* Create the client call
*/
client = new DefaultHttpClient();
client.setCredentialsProvider(credentialProvider);
HttpGet get = new HttpGet(STREAMING_API_URL + track); // 每个spout实例track是唯一的。
HttpResponse response;
try {
// Execute
response = client.execute(get);
StatusLine status = response.getStatusLine();
if (status.getStatusCode() == 200) {
InputStream inputStream = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String in;
// Read line by line
while ((in = reader.readLine()) != null) {
try {
// Parse and emit
Object json = jsonParser.parse(in);
collector.emit(new Values(track, json));
} catch (ParseException e) {
LOG.error("Error parsing message from twitter", e);
}
}
}
} catch (IOException e) {
LOG.error("Error in communication with twitter api ["
+ get.getURI().toString() + "]");
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
}
}
} /**
* spoutsSize、myIdx实现了一个spout读取多个流tracks。
*/
@Override
public void open(Map conf, TopologyContext context,
SpoutOutputCollector collector) {
int spoutsSize = context
.getComponentTasks(context.getThisComponentId()).size();
int myIdx = context.getThisTaskIndex();
String[] tracks = ((String) conf.get("track")).split(",");
StringBuffer tracksBuffer = new StringBuffer();
for (int i = 0; i < tracks.length; i++) {
if (i % spoutsSize == myIdx) {
tracksBuffer.append(",");
tracksBuffer.append(tracks[i]);
}
} if (tracksBuffer.length() == 0)
throw new RuntimeException("No track found for spout"
+ " [spoutsSize:" + spoutsSize + ", tracks:"
+ tracks.length + "] the amount"
+ " of tracks must be more then the spout paralellism"); this.track = tracksBuffer.substring(1).toString(); user = (String) conf.get("user");
password = (String) conf.get("password"); credentials = new UsernamePasswordCredentials(user, password);
credentialProvider = new BasicCredentialsProvider();
credentialProvider.setCredentials(AuthScope.ANY, credentials);
this.collector = collector;
} @Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("criteria", "tweet"));
}
}

通过这种技术,可以在数据源间分布收集器。相同的技术可以被应用在其他的场景-例如,从web服务器收集日志文件。PS:没有试过。

2.Bolt可以使用emit(streamId, tuple)发射元组到多条流,每条流由字符串streamId来识别。然后,在TopologyBuilder 中,你可以决定订阅哪条流。

没有试过。2个疑问:如何declare呢?spout有这个功能么?

解答:1.declareOutputFields()方法中声明多条流,不就可以了。

 public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("line"));
declarer.declareStream("second", new Fields("line2"));
}

2.Bolt和spout的实现来看,应该都是可以的。

3.BaseRichSpout是否是自动调用ack方法的,实现IBasicBolt接口可以自动ack?

BaseBasicBolt,  is used to do the acking automatically.意思就是说,这个是自动调用ack的 (测试结果:使用BaseBasicBolt,不主动使用input.ack(),那么storm ui界面上看不到ack的个数。)so,最好使用input.ack().。PS:目前的项目编程是如下方法。

collector.emit();
input.ack();

通过IBasicBolt可以自动ack,用法如下。Storm UI可以看到该bolt的ack数目。

 public class TotalBolt implements IBasicBolt{

     private static final long serialVersionUID = 1L;
static Integer Total = new Integer(0);
  //必须要实现的方法,使用BasicOutputCollector
public void execute(Tuple input,BasicOutputCollector collector) {
try {
String clear = input.getStringByField("PROVINCE_ID");
Total++;
collector.emit(new Values(Total));
} catch (IllegalArgumentException e) {
if (input.getSourceStreamId().equals("signals24Hour")) {
Total = 0;
}
}
}
}

4.关于bolt的锚定

记录原始的spout实例的最好方式是在消息元组中包含一个原始spout的引用。这个技术叫做锚定。

collector.emit(tuple,new Values(word)); 
什么是锚定?(如果这样可以自动重发的话,确实是不错的选择。????测试:测试无法模拟消息失败,我采用主动调用collector.fail(input),但是没看到重发消息的现象,原因是因为重发消息是不调用nextTuple()方法的,因此日志中看不到自己写的日志输出,实际上是重发了的。)
为tuple tree中指定的节点增加一个新的节点,我们称之为锚定(anchoring)。
采用_collector.emit( tuple , new Values(word) )就是锚定了。锚定是分段的,可以一部分bolt锚定。

每个消息都通过这种方式被锚定:把输入消息作为emit方法的第一个参数。因为word消息被锚定在了输入消息上,这个输入消息是spout发送过来的tuple tree的根节点,如果任意一个word消息处理失败,派生这个tuple tree那个spout 消息将会被重新发送。(锚定的好处是什么)

但是,这样会不会导致 元组被重发,计数重复? 会。

系统使用一种哈希算法来根据spout消息的messageId确定由哪个acker跟踪此消息派生出来的tuple tree。

下面这张图是OutputCollector(Bolt)中的emit方法:

 

 

测试结果:做了anchors锚定后,没有看到实际的效果。

 自动锚定和ack的接口:IBasicBolt

A lot of bolts follow a common pattern of reading an input tuple, emitting tuples based on it, and then acking the tuple at the end of the execute method. These bolts fall into the categories of filters and simple functions. Storm has an interface called IBasicBolt that encapsulates this pattern for you. The SplitSentence example can be written as a IBasicBolt like follows:

public class SplitSentence implements IBasicBolt {
public void execute(Tuple tuple, BasicOutputCollector collector) {
String sentence = tuple.getString(0);
for(String word: sentence.split(" ")) {
collector.emit(new Values(word));
}
} public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("word"));
}
}

This implementation is simpler than the implementation from before and is semantically identical. Tuples emitted to BasicOutputCollectorare automatically anchored to the input tuple, and the input tuple is acked for you automatically when the execute method completes.

In contrast, bolts that do aggregations or joins may delay acking a tuple until after it has computed a result based on a bunch of tuples. Aggregations and joins will commonly multi-anchor their output tuples as well. These things fall outside the simpler pattern of IBasicBolt.

5 集群的各级容错

5. 疑问:主动调用input.ack(),日志中没有看到调用了spout的ack方法?这是为什么呢?

原因:Spout中SpoutOutputCollector.emit()方法中,没有加入messageID,也就是说采用了以下第一种emit方法,用第二种方法便可以看到。加入后在storm ui界面看到。

SpoutOutputCollector的emit方法:(messageID用long或者Integer型都是可以的。If the messageId was null, Storm will not track the tuple and no callback will be received. The emitted values must be immutable.)

 collector.emit(new Values(str), messageID++);//messageID is type long.

一个消息默认被成功处理的timeOut是30s,超过30s就会触发spout的fail方法。这个值可以根据实际的集群情况进行调整。在Topology中 Conf conf.setMessageTimeoutSecs(int secs) 设置。

storm ui界面:

Storm学习笔记的更多相关文章

  1. Storm学习笔记 - 消息容错机制

    Storm学习笔记 - 消息容错机制 文章来自「随笔」 http://jsynk.cn/blog/articles/153.html 1. Storm消息容错机制概念 一个提供了可靠的处理机制的spo ...

  2. Storm学习笔记 - Storm初识

    Storm学习笔记 - Storm初识 1. Strom是什么? Storm是一个开源免费的分布式计算框架,可以实时处理大量的数据流. 2. Storm的特点 高性能,低延迟. 分布式:可解决数据量大 ...

  3. Twitter Storm学习笔记

    官方英文文档:http://storm.apache.org/documentation/Documentation.html 本文是学习笔记,转载整合加翻译,主要是为了便于学习. 一.基本概念 参考 ...

  4. Storm学习笔记——高级篇

    1. Storm程序的并发机制 1.1 概念 Workers (JVMs): 在一个物理节点上可以运行一个或多个独立的JVM 进程.一个Topology可以包含一个或多个worker(并行的跑在不同的 ...

  5. Storm学习笔记六

    1 Storm的通信机制 说明:1.worker与worker之间跨进程通信: 2.worker内部中各个executor间的通信,Task对象--->输入队列queue--->执行--- ...

  6. storm学习笔记(一)

    1.storm介绍         storm是一种用于事件流处理的分布式计算框架,它是有BackType公司开发的一个项目,于2014年9月加入了Apahche孵化器计划并成为其旗下的顶级项目之一. ...

  7. apache Storm 学习笔记

    Storm流之FieldGrouping字段分组: https://blog.csdn.net/Simon_09010817/article/details/80092080

  8. Storm学习笔记1:Storm基本组件

    Storm概念及组件 Nimbus:负责资源分配和任务调度. Supervisor:负责接受nimbus分配的任务,启动和停止属于自己管理的worker进程. Worker:运行具体处理组件逻辑的进程 ...

  9. Storm学习笔记——安装配置

    1.安装一个zookeeper集群 2.上传storm的安装包,解压 3.修改配置文件conf/storm.yaml #所使用的zookeeper集群主机storm.zookeeper.servers ...

随机推荐

  1. Android(java)学习笔记193:利用谷歌API对数据库增删改查(推荐使用)

    接下来我们通过项目案例来介绍:这个利用谷歌API对数据库增删改查 1.首先项目图: 2.这里的布局文件activity_main.xml: <LinearLayout xmlns:android ...

  2. Ubuntu安装sar出错Please check if data collecting is enabled in /etc/default/sysstat

    1.安装sysstat apt-get install sysstat 2.安装后无法使用: Cannot open /var/log/sysstat/sa02: No such file or di ...

  3. Linux中Curl命令couldn't connect to host解决方案 php操作Curl(http,https)无法获取远程数据解决方案

    本人在做百度账户第三方登录接口,获取百度token,利用php操作curl post方式发送请求token,出现couldn't connect to host错误.经过调试测试,最后终于成功.回头写 ...

  4. wps批量使标题靠文档左边

  5. javascript 封装 构造函数继承 非构造函数继承

    1 封装 把"属性"(property)和"方法"(method),封装成一个对象,甚至要从原型对象生成一个实例对象 1.1 简单封装:var cat1 = { ...

  6. .net单元测试——解除依赖

    最近在看.net单元测试艺术,我也喜欢单元测试,这里写一下如何在测试中解除对象间的依赖. 假如有这样的一个需求,当用户登陆时,我需要对用户名和密码进行验证,然后再将用户名写入日志中. public c ...

  7. IIS应用地址池监控

    目的:公司服务器IIS有十几个应用地址池,总在不经意间停掉一个,停止线系统日志里会有一大堆警告日志,然后就停掉了,分析了好几次,网上有人说是某一个网站的问题应该查网站, 但是网站又有那么多地址,谁知道 ...

  8. 额定能量不得超过160Wh, 等同是多少mAh电池容量?

    额定能量不得超过160Wh, 等同是多少mAh电池容量?行动电源容量标示, 正确应该是用Whr(Wh)瓦特小时来标示, 不过坊间标榜行动电源的容量通常是用xx000mAhWHr瓦特小时, 即是行动电源 ...

  9. CSS 列表

    CSS列表属性作用如下: 设置不同的列表项标记为有序列表 设置不同的列表项标记为无序列表 设置列表项标记为图像 列表 在HTML中,有两种类型的列表: 无序列表 - 列表项标记用特殊图形(如小黑点.小 ...

  10. ubuntu1404安装配置java环境(jdk8)

    这个安装比较简单,网上也有数不清的教学,我这里记录以下,方便以后万一失忆了回来看看能想起来.个人博客http://www.cnblogs.com/wdfwolf3/ 1.下载安装 言归正传,我们需要到 ...