简体   繁体   English

如何在同一个bolt中处理不同的元组(Storm)

[英]How to process different tuples in the same bolt (Storm)

I have 2 Storm Bolts that emit different Values (defined be the declareOutputFields)我有 2 个风暴螺栓,它们发出不同的值(定义为 declareOutputFields)

1) 1)

    declarer.declare(new Fields("id", "bool"));
    basicOutputCollector.emit(new Values(id, true));
    declarer.declare(new Fields("id", "string"));
    basicOutputCollector.emit(new Values(id, "somestring"));

And I want to have a Bolt that functions as a Logger, that logs something if either the boolean value arrives, or the string value arrives.我想要一个用作记录器的 Bolt,如果 boolean 值到达或字符串值到达,它会记录一些东西。

The (simplified) Topology looks like this: (简化的)拓扑如下所示:

    var topologyBuilder = new TopologyBuilder();
    topologyBuilder.setSpout("spout")
    topologyBuilder.setBolt(BoolBolt, new BoolBolt()).fieldsGrouping("spout", new Fields("id"));
    topologyBuilder.setBolt(StringBolt, new StringBolt()).fieldsGrouping("spout", new Fields("id"));
    topologyBuilder.setBolt("LoggerBolt", new LoggerBolt())
            .fieldsGrouping("BoolBolt", new Fields("id"));
            .fieldsGrouping("StringBolt", new Fields("id"));
    return topologyBuilder.createTopology();

But if I now try to access the fields in the LoggerBolt, sometimes, the value is a boolean, and sometimes the value is a string because both BoolBolt and StringBolt emit Tuples to the LoggerBolt.但如果我现在尝试访问 LoggerBolt 中的字段,有时值是 boolean,有时值是字符串,因为 BoolBolt 和 StringBolt 都向 LoggerBolt 发出元组。

How can I hanlde this?我该如何处理?

Eg I would like to print "BOOL" if the Tuple ("id", "bool") arrives at the LoggerBolt and i want to print "STRING" if the Tuple ("id", "string") arrives at the logger.例如,如果元组(“id”,“bool”)到达 LoggerBolt,我想打印“BOOL”,如果元组(“id”,“string”)到达记录器,我想打印“STRING”。

Is there any possibility to check from what Bold a Tuple was emitted?有没有可能检查从哪个 Bold 发出了 Tuple ? Or is there anything I can check what Fields the Tuple containes?或者有什么我可以检查元组包含的字段吗?

And just checking if the tuples value is a String or not is not what I look for, as in example everything could be emitted!!!并且只是检查元组值是否是字符串不是我要寻找的,例如一切都可以发出!!!

Thanks in advance提前致谢

You can examine the tuple object to find out which component it came from.您可以检查元组 object 以找出它来自哪个组件。 Try the following in your LoggerBolt:在 LoggerBolt 中尝试以下操作:

String from_bolt = tuple.getSourceComponent();
if ("BoolBolt".equals(from_bolt)) {
    LOG.info("BOOL");
} else if ("StringBolt".equals(from_bolt)) {
    LOG.info("STRING");
}

An alternative is to check, if the tuple contains a certain field, this is done with tuple.contains("field_name") .另一种方法是检查元组是否包含某个字段,这是通过tuple.contains("field_name")完成的。 Both methods would work.两种方法都行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM