An alternative to creating the tuple with all of the fields as a value is to just create a bean and pass that inside the tuple.
Given the following class:
public class DataBean implements Serializable {
private static final long serialVersionUID = 1L;
// add more properties as necessary
int id;
String word;
public DataBean(int id, String word) {
setId(id);
setWord(word);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
}
Create and emit the DataBean in one bolt:
collector.emit(new Values(bean));
Get the DataBean in the destination bolt:
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
try {
DataBean bean = (DataBean)tuple.getValue(0);
// do your bolt processing with the bean
} catch (Exception e) {
LOG.error("WordCountBolt error", e);
collector.reportError(e);
}
}
Don't forget to make your bean serializable and register when you set up your topology:
Config stormConfig = new Config();
stormConfig.registerSerialization(DataBean.class);
// more stuff
StormSubmitter.submitTopology("MyTopologyName", stormConfig, builder.createTopology());
Disclaimer: Beans will work fine for shuffle grouping. If you need to do a fieldsGrouping
, you should still use a primitive. For example, in the Word Count scenario, you need go group by word so you might emit:
collector.emit(new Values(word, bean));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…