美文网首页数客联盟
Storm的分组方式

Storm的分组方式

作者: Woople | 来源:发表于2017-04-26 18:28 被阅读49次

Storm中内置了7种分组方式

Shuffle grouping

  • 定义: Tuples are randomly distributed across the bolt's tasks in a way such that each bolt is guaranteed to get an equal number of tuples.
  • 样例
    此样例由Storm的官方提供,通过下面这个例子可以对Shuffle grouping有更直观的认识

    public class ExclamationTopology {
    
    public static class ExclamationBolt extends BaseRichBolt {
        OutputCollector _collector;
    
        @Override
        public void prepare(Map conf, TopologyContext context, OutputCollector collector) {
            _collector = collector;
        }
    
        @Override
        public void execute(Tuple tuple) {
            System.out.println(tuple.getString(0) + " is from task " + tuple.getSourceTask() + " of Spout/Bolt:" + tuple.getSourceComponent());
    
            _collector.emit(tuple, new Values(tuple.getString(0) + "!!!"));
            _collector.ack(tuple);
        }
    
        @Override
        public void declareOutputFields(OutputFieldsDeclarer declarer) {
            declarer.declare(new Fields("word"));
        }
    
    }
    
    public static void main(String[] args) throws Exception {
        TopologyBuilder builder = new TopologyBuilder();
    
        builder.setSpout("word", new TestWordSpout(), 10);
        builder.setBolt("exclaim1", new ExclamationBolt(), 3).shuffleGrouping("word");
        builder.setBolt("exclaim2", new ExclamationBolt(), 2).shuffleGrouping("exclaim1");
    
        Config conf = new Config();
        conf.setDebug(true);
    
        if (args != null && args.length > 0) {
            conf.setNumWorkers(30);
    
            StormSubmitter.submitTopologyWithProgressBar(args[0], conf, builder.createTopology());
        }
        else {
    
            LocalCluster cluster = new LocalCluster();
            cluster.submitTopology("test", conf, builder.createTopology());
            Utils.sleep(5000);
            cluster.killTopology("test");
            cluster.shutdown();
        }
    }
    

}
```

本地运行这个样例,会有类似如下的日志打印,从这个打印中可以看到,Bolt exclaim1的数据来自于Spout word的10个task,即task[7-16]

```
jackson is from task 11 of Spout/Bolt:word
mike is from task 8 of Spout/Bolt:word
nathan is from task 12 of Spout/Bolt:word
nathan is from task 16 of Spout/Bolt:word
nathan is from task 13 of Spout/Bolt:word
```

Fields grouping

  • 定义:The stream is partitioned by the fields specified in the grouping. For example, if the stream is grouped by the "user-id" field, tuples with the same "user-id" will always go to the same task, but tuples with different "user-id"'s may go to different tasks.
  • 样例

    对上面的样例稍加改造

    builder.setSpout("word", new TestWordSpout(), 10);
    builder.setBolt("exclaim1", new ExclamationBolt(), 3).fieldsGrouping("word", new Fields("word"));
    builder.setBolt("exclaim2", new ExclamationBolt(), 2).shuffleGrouping("exclaim1");
    

    从运行的结果中可以看到类似如下的打印,说明相同的字符都来自于同一个task

    mike!!! is from task 2 of Spout/Bolt:exclaim1
    mike!!! is from task 2 of Spout/Bolt:exclaim1
    mike!!! is from task 2 of Spout/Bolt:exclaim1
    mike!!! is from task 2 of Spout/Bolt:exclaim1
    

    或者在execute方法中在加如下的打印System.out.println("Current thread is " + Thread.currentThread().getId() + " to emit " + tuple.getString(0) + "!!!");,可以看到类似如下的打印,所有的mike!!!都是由同一个线程处理的。

    Current thread is 124 to emit mike!!!
    Current thread is 124 to emit mike!!!
    

All grouping

  • 定义:The stream is replicated across all the bolt's tasks. Use this grouping with care.
  • 样例

    对上面的样例稍加改造

    builder.setSpout("word", new TestWordSpout(), 10);
    builder.setBolt("exclaim1", new ExclamationBolt(), 3).allGrouping("word");
    builder.setBolt("exclaim2", new ExclamationBolt(), 2).shuffleGrouping("exclaim1");
    

    从运行的结果中可以看到类似如下的打印,因为Bolt exclaim1的有3个task,所以下面的结果说明了,Bolt exclaim2要从每个task中都取一次

    Current thread is 124 to emit mike!!!
    Current thread is 128 to emit mike!!!
    Current thread is 150 to emit mike!!!
    
    [Thread-18-exclaim1-executor[2 2]] INFO  o.a.s.d.executor - TRANSFERING tuple [dest: 6 tuple: source: exclaim1:2, stream: default, id: {}, [mike!!!]]
    [Thread-22-exclaim1-executor[3 3]] INFO  o.a.s.d.executor - TRANSFERING tuple [dest: 5 tuple: source: exclaim1:3, stream: default, id: {}, [mike!!!]]
    [Thread-44-exclaim1-executor[4 4]] INFO  o.a.s.d.executor - TRANSFERING tuple [dest: 6 tuple: source: exclaim1:4, stream: default, id: {}, [mike!!!]]
    

Global grouping

  • 定义:The entire stream goes to a single one of the bolt's tasks. Specifically, it goes to the
    task with the lowest id.
  • 样例

    对上面的样例稍加改造

     builder.setSpout("word", new TestWordSpout(), 10);
     builder.setBolt("exclaim1", new ExclamationBolt(), 3).globalGrouping("word");
     builder.setBolt("exclaim2", new ExclamationBolt(), 2).shuffleGrouping("exclaim1");
    

    结果中会有类似如下的打印,说明mike!!!都来自于了同一个Bolt

    mike!!! is from task 2 of Spout/Bolt:exclaim1
    mike!!! is from task 2 of Spout/Bolt:exclaim1
    mike!!! is from task 2 of Spout/Bolt:exclaim1
    

None grouping

  • 定义:This grouping specifies that you don't care how the stream is grouped. Currently, none
    groupings are equivalent to shuffle groupings.

Direct grouping

  • 定义:This is a special kind of grouping. A stream grouped this way means that the producer
    of the tuple decides which task of the consumer will receive this tuple. Direct groupings can only be declared on streams that have been declared as direct streams. Tuples emitted to a direct stream must be emitted using one of the emitDirect methods.

Local or shuffle grouping

  • 定义: If the target bolt has one or more tasks in the same worker process, tuples will be shuffled to just those in-process tasks. Otherwise, this acts like a normal shuffle grouping.

相关文章

  • Storm流分组源码分析

    本文不是停留在字面上去总结Storm的流分组方式,而是列出Storm流分组实现的源码位置,看了源码,对各种流分组也...

  • Storm的分组方式

    Storm中内置了7种分组方式 Shuffle grouping 定义: Tuples are randomly ...

  • 32 storm 单词计数

    上一篇 简单看 storm, 主要简单讲解了storm 的集群架构、核心概念、并行度、流分组,本篇利用 storm...

  • storm grouping 分组介绍

    Stream groupings 流分组 定义一个拓扑部分是指定了每个bolt门闩的流都应该作为输入被接收。一个流...

  • 风控系统四-storm分组策略Stream Grouping详解

    Storm里面有7种类型的stream grouping 1 Shuffle Grouping: 随机分组 随机派...

  • storm的流id

    玩过storm的人都知道storm有流分组的概念。但上级组件传递给下一集组件的策略。但其实stomr还有流id的概...

  • Storm 核心构建及分组

    Storm核心组件:Topology:Storm中运行一个实时应用程序的名称。Nimbus:负责资源分配和任务调度...

  • storm Trident编程的分组策略

    Trident编程中的=数据分组策略演示 代码

  • 分组的过滤方式

    1、分组 分组允许数据分为多个逻辑组,以便能对每个组进行聚集计算。 GROUP BY子句指示MYSQL分组数据,然...

  • Storm入门

    Storm 基本介绍 什么是 Storm 首先Storm是Apache顶级项目之一Storm 官网 Storm 是...

网友评论

    本文标题:Storm的分组方式

    本文链接:https://www.haomeiwen.com/subject/ervizttx.html