Samza的task可以把数据进行本地存储,并且对这些数据进行丰富的查询。

 
比较SQL中的select ... where...并不需要保存状态。但是aggregation和join就需要存储row之间的状态。
Samza提供了一些基本功能,能够使得streaming join和aggregation以及其它的有状态的处理更容易实现。
 
何处需要stateful processing?
  • windowed aggregation 比如:每个用户每小时的点击量
    • 这种windowed processing通常用于ranking和relevance , 发现"trending topics ", 以及简单的实时reporting和monitoring。
    • 困难在于:当一个window处理的消息很多时,如果这个window 失败了,当重启时应该如何避免需要把全部消息重新处理一遍。
  • table-table join
  • stream-table join
  • stream-stream join
如何管理task state? 如何支持这样的stateful processing?
 
有以下几种常用方案
In-memory state with checkpointing
周期性的把task在内存中的数据做checkpoint. S4的状态管理就是这样做的。
缺点是当作为state的数据量很大时,每次都完全dump所有数据不切实际,如果用diff又太复杂。
 
Using an external store
另一种常见的方案是把状态写进一个外部的数据库或者key-value store中。
samza支持这种方式,但是提供了本地持久化作为更好的选项。
 
Local state in Samza
Samza allows tasks to maintain persistent, mutable, queryable state that is physically co-located with each task.
Samza支持task在跟task同一台机器上维持持久化的、可变的、可查询的状态。
每个task在写OutputStream的同时,还会写Changlog Stream。
 
Key-value storage
Kafka自带一个key-value store的实现,使用LevelDB。这个k-v store使用一个高可靠的"changelog" stream做为支撑,这个stream通过做为一个"redo log"来为task的状态提供了fault-tolerance功能。
 
Fault-tolerance
一个task的local storage实际上是一个缓存,那么当一个机器fail之后,怎么才能在另一个机器上重建这个缓存呢?
这里有两种选择
  1. 在一个task重启时,重新读取曾经的所有输入以重建它的state。但是通常这个state会比input stream小得多,或者input stream是不可重放的。所以重新处理原有输入是一种浪费
  2. 使用一个changelog流。task把它的每次状态的改变记在这个流里。changelog就是一个普通的流,可以被其它人订阅。当然changelog是不断增长的,为了避免它占用太多空间,可以使用Kafka 0.8.1提供的log compaction功能,来去掉重复的条目。
Using the key-value store
首先在config里加上以下配置
 
# Use the key-value store implementation for a store called "my-store"
stores.my-store.factory=org.apache.samza.storage.kv.KeyValueStorageEngineFactory

# Log changes to the store to an output stream for restore
# If no changelog is specified the store will not be logged (but you can still rebuild off your input streams)
stores.my-store.changelog=kafka.my-stream-name

# The serialization format to use
stores.my-store.key.serde=string
stores.my-store.msg.serde=string

 
然后在StreamTask里这么写
 
public class MyStatefulTask implements StreamTask, InitableTask {
  private KeyValueStore<String, String> store;

public void init(Config config, TaskContext context) {
    this.store = (KeyValueStore<String, String>) context.getStore("store");
  }

public void process(IncomingMessageEnvelope envelope, MessageCollector collector, TaskCoordinator coordinator) {
    System.out.println("Adding " + envelope.getKey() + " => " + envelope.getMessage() + " to the store.");
    store.put((String) envelope.getKey(), (String) envelope.getMessage());
  }
}

 
 
 

State Management的更多相关文章

  1. angular2 学习笔记 ( 状态管理 state management )

    更新 : 2017-12-29  ng5 移除 zone.js https://zhuanlan.zhihu.com/p/29577461 zone 的用途就是拦截游览器事件, 比如 click, a ...

  2. State management(状态管理)

    State management https://en.wikipedia.org/wiki/State_management UI控件的状态管理, 例如按钮的灰化.只读.显示隐藏等. 特殊地一个控件 ...

  3. .NET:CLR via C# Exceptions and State Management

    重点学习的个概念 unhandled exceptions constrained execution regions code contracts runtime wrapped exception ...

  4. Web前端的状态管理(State Management)

    背景 我相信很多朋友跟我一样,初次听到什么Flux, Redux, Vuex,状态管理的时候是一脸懵逼的.因为在外面之前前端大部分开发的时候,根本没有那么多的概念.自从ReactJS火爆后,什么Flu ...

  5. Recoil & React official state management

    Recoil & React official state management Redux Recoil.js https://recoiljs.org/ A state managemen ...

  6. ASP.NET MVC- JSON ,Jquery, State management and Asynch controllers

    一.JSON  MVC And JQuery In case you are new to JSON please read this before moving ahead with this la ...

  7. PatentTips - Virtual machine management using processor state information

    BACKGROUND OF THE INVENTION The invention generally relates to virtual machine management, and more ...

  8. [Hapi.js] Managing State with Cookies

    hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...

  9. 原生 JavaScript 实现 state 状态管理系统

    原生 JavaScript 实现 state 状态管理系统 Build a state management system with vanilla JavaScript | CSS-Tricks 在 ...

随机推荐

  1. response小结(五)—通过response实现请求重定向

    请求重定向指的是一个web资源收到客户端请求后,通知客户端去访问另外一个web资源,这称之为请求重定向.302状态码和location头即可实现重定向. 请求重定向最常见的应用场景就是用户登录. 下面 ...

  2. response小结(二)——文件下载

    我们先来看一个最简单的文件下载的例子: package com.yyz.response; import java.io.FileInputStream; import java.io.IOExcep ...

  3. IIS实现301重定向

    301永久重定向对SEO无任何不好的影响,而且网页A的关键词排名和PR级别都会传达给网页B,网站更换了域名,表示本网页永久性转移到另一个地址,对于搜索引擎优化|SEO来说,给搜索引擎一个友好的信息,告 ...

  4. ArcMap 10.2 crashes during Loading Document launch stage

    问题描述: ArcMap unexpectedly exits during the "Loading Document..." stage on startup. No erro ...

  5. .NET的JSNO 序列化跟反序列化

    由于本人最近在写webservice,之前一直都同通过AJAX,在服务端处理业务,但是最近需要写一些接口给其他人用,需要使用jsno的序列化与反序列化,什么是jsno就不多说,jsno的好处也不多说, ...

  6. MyEclipse自动提示

    MyEclipse自动提示 Eclipse中默认是输入"."后出现自动提示,用于类成员的自动提示,可是有时候我们希望它能在我们输入类的. 首字母后就出现自动提示,可以节省大量的输入 ...

  7. ICallbackEventHandler 接口实现回调处理功能

    在最近的项目实现中遇到了一个问题 在数据处理的过程中,需要请求获取数据,再做处理之后,可以在页面及时获取数据 开始时,首先想到的到是写Ajax请求,但在做后续数据处理后,处理获取数据等操作,感觉实现起 ...

  8. javascript笔记——JavaScript经典实例

    转载自百度文库 http://wenku.baidu.com/view/9a703522bcd126fff7050bfa.html 1. oncontextmenu="window.even ...

  9. 快速搭建MongoDB分布式集群

    目录Outline 1. prerequisites 2. steps to follow3. configuring the cluster4. a little test to see 1. Pr ...

  10. [android网络有效性检测] NetworkMonitor代码造成内存泄漏

    造成内存泄漏的log如下: E StrictMode: A resource was acquired at attached stack trace but never released. See ...