溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

storm的本地模式demo怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2021-12-23 15:43:01 來源:億速云 閱讀:106 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“storm的本地模式demo怎么實(shí)現(xiàn)”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

SimpleTopology.java

package com.zgl.helloword;

import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.StormSubmitter;
import backtype.storm.topology.TopologyBuilder;

/**
 * 定義了一個(gè)簡單的topology,包括一個(gè)數(shù)據(jù)噴發(fā)節(jié)點(diǎn)spout和一個(gè)數(shù)據(jù)處理節(jié)點(diǎn)bolt。
 * 
 * @author Administrator
 *
 */
public class SimpleTopology {
    public static void main(String[] args) {
        try {
            // 實(shí)例化TopologyBuilder類。
            TopologyBuilder topologyBuilder = new TopologyBuilder();
            // 設(shè)置噴發(fā)節(jié)點(diǎn)并分配并發(fā)數(shù),該并發(fā)數(shù)將會控制該對象在集群中的線程數(shù)。
            topologyBuilder.setSpout("SimpleSpout", new SimpleSpout(), 1);
            // 設(shè)置數(shù)據(jù)處理節(jié)點(diǎn)并分配并發(fā)數(shù)。指定該節(jié)點(diǎn)接收噴發(fā)節(jié)點(diǎn)的策略為隨機(jī)方式。
            topologyBuilder.setBolt("SimpleBolt", new SimpleBolt(), 3).shuffleGrouping("SimpleSpout");
            Config config = new Config();
            config.setDebug(false);
            if (args != null && args.length > 0) {
                config.setNumWorkers(1);
                StormSubmitter.submitTopology(args[0], config, topologyBuilder.createTopology());
            } else {
                // 這里是本地模式下運(yùn)行的啟動代碼。
                config.setMaxTaskParallelism(1);
                LocalCluster cluster = new LocalCluster();
                cluster.submitTopology("simple", config, topologyBuilder.createTopology());
            }
            
        } catch (Exception e) {
            e.printStackTrace(); 
        }
    }
}

SimpleSpout.java

package com.zgl.helloword;

import java.util.Map;
import java.util.Random;

import backtype.storm.spout.SpoutOutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichSpout;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Values;

/**
 * Spout起到和外界溝通的作用,他可以從一個(gè)數(shù)據(jù)庫中按照某種規(guī)則取數(shù)據(jù),也可以從分布式隊(duì)列中取任務(wù)
 * 
 * @author Administrator
 *
 */

public class SimpleSpout extends BaseRichSpout{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    //用來發(fā)射數(shù)據(jù)的工具類
    private SpoutOutputCollector collector;
    private static String[] info = new String[]{
        "comaple\t,12424,44w46,654,12424,44w46,654,",
        "lisi\t,435435,6537,12424,44w46,654,",
        "lipeng\t,45735,6757,12424,44w46,654,",
        "hujintao\t,45735,6757,12424,44w46,654,",
        "jiangmin\t,23545,6457,2455,7576,qr44453",
        "beijing\t,435435,6537,12424,44w46,654,",
        "xiaoming\t,46654,8579,w3675,85877,077998,",
        "xiaozhang\t,9789,788,97978,656,345235,09889,",
        "ceo\t,46654,8579,w3675,85877,077998,",
        "cto\t,46654,8579,w3675,85877,077998,",
        "zhansan\t,46654,8579,w3675,85877,077998,"};
    
    Random random=new Random();
    
    /**
     * 初始化collector
     */
    @SuppressWarnings("rawtypes")
    public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
        this.collector = collector;
    }
    
    /**
     * 在SpoutTracker類中被調(diào)用,每調(diào)用一次就可以向storm集群中發(fā)射一條數(shù)據(jù)(一個(gè)tuple元組),該方法會被不停的調(diào)用
     */
    
    public void nextTuple() {
        try {
            String msg = info[random.nextInt(11)];
            // 調(diào)用發(fā)射方法
            collector.emit(new Values(msg));
            // 模擬等待100ms
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 定義字段id,該id在簡單模式下沒有用處,但在按照字段分組的模式下有很大的用處。
     * 該declarer變量有很大作用,我們還可以調(diào)用declarer.declareStream();來定義stramId,該id可以用來定義更加復(fù)雜的流拓?fù)浣Y(jié)構(gòu)
     */
   
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("source")); //collector.emit(new Values(msg));參數(shù)要對應(yīng)
    }

}

SimpleBolt.java

package com.zgl.helloword;

import backtype.storm.topology.BasicOutputCollector;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseBasicBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;

/**
 * 接收噴發(fā)節(jié)點(diǎn)(Spout)發(fā)送的數(shù)據(jù)進(jìn)行簡單的處理后,發(fā)射出去。
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("serial")
public class SimpleBolt extends BaseBasicBolt {

    public void execute(Tuple input, BasicOutputCollector collector) {
        try {
            String msg = input.getString(0);
            if (msg != null){
                System.out.println("msg="+msg);
                collector.emit(new Values(msg + "msg is processed!"));
            }
                
        } catch (Exception e) {
            e.printStackTrace(); 
        }
    }

    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("info"));
    }

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>strom-zgl</groupId>
    <artifactId>storm-zgl</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>storm-zgl</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.storm</groupId>
            <artifactId>storm-core</artifactId>
            <version>0.9.1-incubating</version>
        </dependency>
    </dependencies>
</project>

“storm的本地模式demo怎么實(shí)現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI