Storm流之NoneGrouping不分组

一、概述

      在功能上和随机分组相同,为将来预留的。

二、代码

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上都会有不同的消息去输出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值