使用storm时如何将拓扑上下文中的对象访问到bolt中? [英] How to access an object from the topology context into a bolt when using storm?

查看:20
本文介绍了使用storm时如何将拓扑上下文中的对象访问到bolt中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要在创建拓扑时传递一个对象,以便螺栓可以访问它并基于该对象进行一些进一步的处理.是否可以通过 TopplogyContext 传递对象,如果是,如何传递?或者在提交拓扑时还有其他方法可以在提交之前传递对象,以便螺栓可以对其进行处理/控制?

We need to pass an object when creating a topology so that the bolt can access that and do some further processing based on that object. Is it possible to pass the object via TopplogyContext and if yes, how? Or are there any other ways to pass an object when submitting a topology, before submitting so that bolt can have a handle/control on it?

我们需要通过上下文传递对象,以便所有螺栓都可以访问它,并且不需要在该拓扑的所有螺栓中强制实现构造函数.那么,想知道是否有任何 API 可以做到这一点?

We need to pass the object via a context so that all bolts can access it and there is no need to force an implementation of constructor in all the bolts for that topology. So, Wanted to know if any API exists to do the same?

推荐答案

您可以在 Storm 配置映射中传递对象,前提是它是可序列化的.在拓扑中任何 spout 或 bolt 的 prepare() 方法中,您都可以检索此对象.

You can pass the object in the storm configuration map, provided that it is serializable. In the prepare() method of any spout or bolt in the topology you can retrieve this object.

这是您在拓扑提交时将对象放入配置图中的方式:

This is how you put your object in the configuration map on topology submission:

Config conf = new Config();
MyObject myPreciousObject = new MyObject("precious");
conf.put("my.object",myPreciousObject);

StormSubmitter.submitTopology(args[0], conf, builder.createTopology());

这是你如何在一个 bolt 或 spout 的 prepare() 方法中检索它:

This is how you retrieve it in the prepare() method of a bolt or spout:

prepare(Map stormConf,TopologyContext context) {

   MyObject myPreciousObject = (MyObject) stormConf.get("my.object");

} 

这篇关于使用storm时如何将拓扑上下文中的对象访问到bolt中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆