这个Field通常和fieldsGrouping分组机制一起使用,这个Field特别难理解,我自己也是在网上看了好多文章,感觉依旧讲的不是很清楚,是似而非,没有抓到重点。这个问题足足困扰了我3-4天时间,一直理解不了Field的概念,

当前我觉得new Fields("word")就相当于表的表头,就是定义这个域,这个域里面放的东西,是emit进去的

如果在declareOutputFields方法中new Fields("word1","word2")有2个及以上的fields,则在emit数据时new Value要与其对应(相当于key与value的关系),然后在topology组装时,fieldsGrouping中的new Fields()可以为new Fields("word1")或new Fields("word2")或new Fields("word1",”word2")来指定接受上游spout或bolt的哪些fields

官方文档里有这么一句话:“if the stream is grouped by the “user-id” field, tuples with the same “user-id” will always go to the same task”

一个task就是一个处理逻辑的实例,所以fields能根据tuple stream的id,也就是下面定义的xxx
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("xxx"));
}
xxx所代表的具体内容会由某一个task来处理,并且同一个xxx对应的内容,处理这个内容的task实例是同一个。

比如说:

bolt第一次emit三个流,即xxx有luonq pangyang qinnl三个值,假设分别建立三个task实例来处理:

luonq -> instance1
pangyang -> instance2
qinnl -> instance3

然后第二次emit四个流,即xxx有luonq qinnanluo py pangyang四个值,假设还是由刚才的三个task实例来处理:
luonq -> instance1
qinnanluo -> instance2
py -> instance3
pangyang -> instance2

然后第三次emit两个流,即xxx有py qinnl两个值,假设还是由刚才的三个task实例来处理:
py -> instance3
qinnl -> instance3

最后我们看看三个task实例都处理了哪些值,分别处理了多少次:

instance1: luonq(处理2次)
instance2: pangyang(处理2次) qinnanluo(处理1次)
instance3: qinnl(处理2次) py(处理2次)

结论:
1. emit发出的值第一次由哪个task实例处理是随机的,此后再次出现这个值,就固定由最初处理他的那个task实例再次处理,直到topology结束

2. 一个task实例可以处理多个emit发出的值

3. 和shuffle Grouping的区别就在于,shuffle Grouping当emit发出同样的值时,处理他的task是随机的

例子1:
第一步:定义了一个表头
public void declareOutputFields(OutputFieldsDeclarer declarer)
    {
        declarer.declare(new Fields("word"));
    }
第二步:往这个Field空间里面emit进去内容(可以是Bolt和Spolt)
public void execute(Tuple input, BasicOutputCollector collector)
    {
        String sentence = input.getString(0);
        String[] words = sentence.split(" ");
        for (String word : words)
        {
            word = word.trim();
            if (!word.isEmpty())
            {
                word = word.toLowerCase();
                collector.emit(new Values(word));
            }
        }
    }
第三步:关联步骤
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("word-reader",new WordReader());
builder.setBolt("word-normalizer", new WordNormalizer()).shuffleGrouping("word-reader");
Integer number = 2;
builder.setBolt("word-counter", new WordCounter(), 4).fieldsGrouping("word-normalizer", new Fields("word"));

第四步:
最终实现的结果:
Field:Word
            the
            sporm
            is
            ...

例子2:

第一步:
public void declareOutputFields(OutputFieldsDeclarer declarer)
{
      declarer.declare(new Fields("word", "count"));
}

第二步:
public void execute(Tuple tuple, BasicOutputCollector collector)
 {
            String word = tuple.getString(0);
            Integer count = counts.get(word);
            if (count == null)
                count = 0;
            count++;
            counts.put(word, count);
            collector.emit(new Values(word, count));
}
第三步:
Fields("word", "count")
            “is”,1
            “sporm”,3
            “the”,2
              .....
例子3:
D:\.....\Workspaces\MyEclipse 8.5\bigData\examples-ch06-real-life-app-master\src\main\java\storm\analytics\....
第一步:
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("read-feed", new UsersNavigationSpout(), 3);
builder.setBolt("get-categ", new GetCategoryBolt(), 3).shuffleGrouping("read-feed");
builder.setBolt("user-history", new UserHistoryBolt(), 5).fieldsGrouping("get-categ", new Fields("user"));

第二步:发送者输出是三个结构体:Fields("user","product", "categ")
GetCategoryBolt.java
public void execute(Tuple input, BasicOutputCollector collector)
 {
        NavigationEntry entry = (NavigationEntry)input.getValue(1);
        if("PRODUCT".equals(entry.getPageType())){
            try {
                String product = (String)entry.getOtherData().get("product");

// Call the items API to get item information
                Product itm = reader.readItem(product);
                if(itm ==null)
                    return ;

String categ = itm.getCategory();

collector.emit(new Values(entry.getUserId(), product, categ));

} catch (Exception ex) {
                System.err.println("Error processing PRODUCT tuple"+ ex);
                ex.printStackTrace();
            }
        }
    }

@Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("user","product", "categ"));
    }

第三步:new Fields("user"))只取Fields("user","product", "categ"))中的User
builder.setBolt("user-history", new UserHistoryBolt(), 5).fieldsGrouping("get-categ", new Fields("user"));
---------------------
作者:VessalasdXZ
来源:CSDN
原文:https://blog.csdn.net/vessalasd1/article/details/50472123
版权声明:本文为博主原创文章,转载请附上博文链接!

Storm里面fieldsGrouping和Field的概念详解的更多相关文章

  1. JWT基础概念详解

    JWT基础概念详解 JWT介绍 之前我们文章讲过分布式session如何存储,其中就讲到过Token.JWT.首先,我们来回顾一下使用Token进行身份认证. 客户端发送登录请求到服务器 服务器在用户 ...

  2. Storm 学习之路(二)—— Storm核心概念详解

    一.Storm核心概念 1.1 Topologies(拓扑) 一个完整的Storm流处理程序被称为Storm topology(拓扑).它是一个是由Spouts 和Bolts通过Stream连接起来的 ...

  3. Storm 系列(二)—— Storm 核心概念详解

    一.Storm核心概念 1.1 Topologies(拓扑) 一个完整的 Storm 流处理程序被称为 Storm topology(拓扑).它是一个是由 Spouts 和 Bolts 通过 Stre ...

  4. Storm里面fieldsGrouping和Field参数和 declareOutputFields

    Fields,个人理解,类似于一张表,你取那些字段以及这些字段所对应的数据给后面的bolt用 这个Field通常和fieldsGrouping分组机制一起使用,这个Field特别难理解,我自己也是在网 ...

  5. java入门---对象和类&概念详解&实例

        Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载     这篇文章,我们主要来看下: 对象:对象是类的一个实例(对象不是找个女朋友),有状态 ...

  6. Android屏幕密度(Density)和分辨率概念详解

    移动设备有大有小,那么如何适应不同屏幕呢,这给我们编程人员造成了很多困惑.我也是突然想到这些问题,然后去网上搜搜相关东西,整理如下.   首先,对下面这些长度单位必须了解. Android中的长度单位 ...

  7. 图像处理术语解释:灰度、色相、饱和度、亮度、明度、阿尔法通道、HSL、HSV、RGBA、ARGB和PRGBA以及Premultiplied Alpha(Alpha预乘)等基础概念详解

    ☞ ░ 前往老猿Python博文目录 ░ 一.引言 由于老猿以前没接触过图像处理,在阅读moviepy代码时,对类的有些处理方法代码看不懂是什么含义,为此花了4天时间查阅了大量资料,并加以自己的理解和 ...

  8. 1-Hyperledger Fabric概念详解

    目录 一.Hyperledger Fabric概述 二.基本术语 1.共享账本ledger 2.通道Channel 3.组织Org 4.智能合约Chaincode 5.背书Endorse 6.各种节点 ...

  9. Spring概念详解

    1.什么是 Spring ? Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2E ...

随机推荐

  1. Java探索之旅(1)——概述与控制台输入

    使用的课本: Java语言程序设计(基础篇)----西电 李娜(译) 原著: Introduction to Java Progrmming(Eighth Edition) -----Y.Daniel ...

  2. 【spring容器bean的作用域+spring容器是否是单例的一些问题】

    Spring容器中Bean的作用域 当通过Spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleto ...

  3. [CentOS7] SELinux

    声明:本文主要总结自:鸟哥的Linux私房菜-第十六章.程序管理與 SELinux 初探,如有侵权,请通知博主 SELinux = Security Enhanced Linux 传统的文件权限与账号 ...

  4. gulp使用文档

    gulp的优势 易于使用:通过代码优于配置的策略,Gulp让简单的任务简单,复杂的任务可管理. 构建快速:利用 Node.js 流的威力,你可以快速构建项目并减少频繁的 IO 操作. 插件高质:Gul ...

  5. vue里的tab标签

    <template> <div class="Test2"> <div class="tabs_wrap" v-model=&qu ...

  6. STP-16-根防护,BPDU防护和BPDU过滤

    网络设计者很可能并不打算让终端用户在用于连接终端用户设备的Access端口上连接交换机.然而,这种事情有时却会发生——例如,有人可能需要大厅的会议室里有更多的端口,于是他觉得他可以把一个小的便宜的交换 ...

  7. 通过用axios发送请求,全局拦截请求,获取到错误弄明白promise对象

    axios.interceptors.response.use(function (response) { // 对响应数据做点什么 return response; }, function (err ...

  8. DateTime.MinValue和MaxValue引发的异常

    转载: http://www.cnblogs.com/lori/p/3186807.html 问题描述: SqlDateTime 溢出.必须介于 1/1/1753 12:00:00 AM 和 12/3 ...

  9. 74th LeetCode Weekly Contest Valid Tic-Tac-Toe State

    A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...

  10. Python----Anaconda + PyCharm + Python 开发环境搭建(使用pip,安装selenium,使用IDLE)

    1.Python开发中会用到的工具下载地址 FireBug插件安装地址:https://addons.mozilla.org/en-US/firefox/addon/firebug/ FirePath ...