java简单实现strom流式计算

随机生成的东西加入,系统也是有包的,不过这边自己写了

public class RandomSpout extends BaseRichSpout {
    String[] phones={"iphone","huawei","xiaomi","xiaolajiao","meizu"};
    private SpoutOutputCollector collector;

    public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
        this.collector=spoutOutputCollector;
    }

    public void nextTuple() {
        Random random=new Random();
        int index = random.nextInt(phones.length);
        String phonename=phones[index];
        collector.emit(new Values(phonename));
    }

    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields("pn"));
    }
}

第一步 比如先把他变成大写

public class UpperBolt extends BaseBasicBolt {


    public void execute(Tuple tuple, BasicOutputCollector basicOutputCollector) {
        String phonename=tuple.getString(0);
        String upName=phonename.toUpperCase();
        basicOutputCollector.emit(new Values(upName));
    }


    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
    outputFieldsDeclarer.declare(new Fields("uppername"));
    }
}

再来一步 加上ok

public class OkBolt extends BaseBasicBolt {
    FileWriter fileWriter=null;
    @Override
    public void prepare(Map stormConf, TopologyContext context) {
        try {
            fileWriter=new FileWriter("/home/hadoop/stormdata/"+ UUID.randomUUID());//第一条协调服务,二三计算
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void execute(Tuple tuple, BasicOutputCollector basicOutputCollector) {
        String name=tuple.getString(0);//传了一个值所以0
        String fName=name+"_ok";
        try {
            //HUAWEI,XIAO 写一个换行方便查看
            fileWriter.write(fName);
            fileWriter.write("\n");
            fileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {

    }
}

最后一部连接

public class ToMain {
    public static void main(String[] args) throws AlreadyAliveException, InvalidTopologyException {
        TopologyBuilder builder=new TopologyBuilder();
        builder.setSpout("rspout",new RandomSpout());
        //                  定义    对象                words||task个数               传来的参数
        builder.setBolt("ubolt",new UpperBolt(),4).shuffleGrouping("rspout");
        builder.setBolt("okBolt",new OkBolt(),4).shuffleGrouping("ubolt");
        StormTopology topology = builder.createTopology();
        Config conf=new Config();
        conf.setNumWorkers(4);
        StormSubmitter.submitTopology("ramtopology",conf,topology);
    }
}

不过这个架包要用maven,不然可能出现连接问题,然后再把jar导入linux运行

<dependencies>


        <!-- https://mvnrepository.com/artifact/org.apache.storm/storm-core -->
        <dependency>
            <groupId>org.apache.storm</groupId>
            <artifactId>storm-core</artifactId>
            <version>0.9.3</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值