最近开始学storm,模仿storm入门教程中给出单词计数的例子改写了一个进行矩阵乘法运算的storm程序,用以入门storm,下面是代码及注释
首先构造一个Topology
import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.topology.TopologyBuilder;
import org.apache.storm.tuple.Fields;
public class TopologyGo {
public static void main(String args[]) throws InterruptedException {
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("matrix-spout",new MatrixReader()); //用于读取存储矩阵文件的spout
builder.setBolt("matrix-dealer",new MatrixDealer()) //用于进行矩阵运算的bolt
.shuffleGrouping("matrix-spout"); //用随机数据流组进行连接
Config conf = new Config();
conf.put("matrixFile", "/home/nfs/input/storm/matrixFile"); //在conf中存入要读取的文件地址
conf.setDebug(false); //debug设置
conf.put(Config.TOPOLOGY_MAX_SPOUT_PENDING, 1);
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("deal-matrix", conf, builder.createTopology()); //提交Topology
Thread.sleep(100000); //等待100000ms后关闭storm(主要目的是测试)
cluster.shutdown();
}
}
spout负责读取数据,下面是作为spout的MatrixReader
import java.io.BufferedReader;
imp