一、概述
在功能上和随机分组相同,为将来预留的。
二、代码
1.Spout
package com.test.csdn.nogrouping;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;
import java.util.Map;
import java.util.Random;
/**
* Created by Simon on 2018/8/1.
*/
public class NoGroupingSpout extends BaseRichSpout {
private SpoutOutputCollector collector;
private String[] str = {"xiaomi","huawei","apple","oppo","vivo","lenovo","LG",
"samsung","htc","honor","nokia","smartisan","Sony","BlackBerry","sharp"};
@Override
public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
this.collector=spoutOutputCollector;
}
@Override
public void nextTuple() {
try {
Thread.sleep(1000);
int i = new Random().nextInt(10);
String string = str[i];
System.out.println(Thread.currentThread().getName()+"___"+string);
collector.emit(new Values(string));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
outputFieldsDeclarer.declare(new Fields("string"));
}
}
2.Bolt
package com.test.csdn.nogrouping;
import org.apache.storm.topology.BasicOutputCollector;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseBasicBolt;
import org.apache.storm.tuple.Tuple;
/**
* Created by Simon on 2018/8/1.
*/
public class NoGroupingBolt extends BaseBasicBolt {
@Override
public void execute(Tuple tuple, BasicOutputCollector basicOutputCollector) {
System.out.println(Thread.currentThread().getName()+"___"+tuple.getValue(0));
}
@Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
}
}
3.Topo
package com.test.csdn.nogrouping;
import com.test.csdn.allgrouping.AllGroupingBolt;
import com.test.csdn.allgrouping.AllGroupingSpout;
import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.topology.TopologyBuilder;
import org.apache.storm.utils.Utils;
/**
* Created by Simon on 2018/8/1.
*/
public class NoGroupingTopo {
public static void main(String[] args) {
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("spout", new NoGroupingSpout(), 2).setNumTasks(3);
//指定3个,便于测试
builder.setBolt("bolt", new NoGroupingBolt(), 3).setNumTasks(5).noneGrouping("spout");
Config conf = new Config();
conf.setDebug(false);
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("toplogy", conf, builder.createTopology());
Utils.sleep(Long.MAX_VALUE);
cluster.shutdown();
}
}
三、运行输出
和随机分组类似,每个task上都会有不同的消息去输出。