Accumulators and Broadcast Variables
这些不能从checkpoint重新恢复

如果想启动检查点的时候使用这两个变量,就需要创建这写变量的懒惰的singleton实例。
下面是一个例子:
  1. def getWordBlacklist(sparkContext):
  2. if ('wordBlacklist' not in globals()):
  3. globals()['wordBlacklist'] = sparkContext.broadcast(["a", "b", "c"])
  4. return globals()['wordBlacklist']
  5. def getDroppedWordsCounter(sparkContext):
  6. if ('droppedWordsCounter' not in globals()):
  7. globals()['droppedWordsCounter'] = sparkContext.accumulator(0)
  8. return globals()['droppedWordsCounter']
  9. def echo(time, rdd):
  10. # Get or register the blacklist Broadcast
  11. blacklist = getWordBlacklist(rdd.context)
  12. # Get or register the droppedWordsCounter Accumulator
  13. droppedWordsCounter = getDroppedWordsCounter(rdd.context)
  14. # Use blacklist to drop words and use droppedWordsCounter to count them
  15. def filterFunc(wordCount):
  16. if wordCount[0] in blacklist.value:
  17. droppedWordsCounter.add(wordCount[1])
  18. False
  19. else:
  20. True
  21. counts = "Counts at time %s %s" % (time, rdd.filter(filterFunc).collect())
  22. wordCounts.foreachRDD(echo)

DataFrame and SQL Operations
通过创建SparkSession的懒惰的singleton实例,可以从失败中恢复。
  1. # Lazily instantiated global instance of SparkSession
  2. def getSparkSessionInstance(sparkConf):
  3. if ('sparkSessionSingletonInstance' not in globals()):
  4. globals()['sparkSessionSingletonInstance'] = SparkSession\
  5. .builder\
  6. .config(conf=sparkConf)\
  7. .getOrCreate()
  8. return globals()['sparkSessionSingletonInstance']
  9. ...
  10. # DataFrame operations inside your streaming program
  11. words = ... # DStream of strings
  12. def process(time, rdd):
  13. print("========= %s =========" % str(time))
  14. try:
  15. # Get the singleton instance of SparkSession
  16. spark = getSparkSessionInstance(rdd.context.getConf())
  17. # Convert RDD[String] to RDD[Row] to DataFrame
  18. rowRdd = rdd.map(lambda w: Row(word=w))
  19. wordsDataFrame = spark.createDataFrame(rowRdd)
  20. # Creates a temporary view using the DataFrame
  21. wordsDataFrame.createOrReplaceTempView("words")
  22. # Do word count on table using SQL and print it
  23. wordCountsDataFrame = spark.sql("select word, count(*) as total from words group by word")
  24. wordCountsDataFrame.show()
  25. except:
  26. pass
  27. words.foreachRDD(process)

MLlib Operations
streaming machine learning algorithms (e.g. Streaming Linear Regression, Streaming KMeans, etc.) 
can simultaneously learn from the streaming data as well as apply the model on the streaming data
for a much larger class of machine learning algorithms
you can learn a learning model offline (i.e. using historical data) and then apply the model online on streaming data


Caching / Persistence
DStreams also allow developers to persist the stream’s data in memory
using the persist() method on a DStream will automatically persist every RDD of that DStream in memory
For window-based operations like reduceByWindow and reduceByKeyAndWindow and state-based operations like updateStateByKey, this is implicitly true(对这些操作,默认实现自动的缓存)
For input streams that receive data over the network (such as, Kafka, Flume, sockets, etc.), the default persistence level is set to replicate the data to two nodes for fault-tolerance.(网络数据,默认存两份来容错)

You can mark an RDD to be persisted using the persist() or cache() methods on it.
levels are set by passing a StorageLevel object to persist()
The cache() method is a shorthand for using the default storage level, which is StorageLevel.MEMORY_ONLY
unlike RDDs, the default persistence level of DStreams keeps the data serialized in memory



Checkpointing

Spark Streaming needs to checkpoint enough information to a fault- tolerant storage system such that it can recover from failures,There are two types of data that are checkpointed.
  1. Metadata checkpointing - Saving of the information defining the streaming computation to fault-tolerant storage like HDFS. This is used to recover from failure of the node running the driver of the streaming application. Metadata includes:
    Configuration - The configuration that was used to create the streaming application.
    DStream operations - The set of DStream operations that define the streaming application.
    Incomplete batches - Batches whose jobs are queued but have not completed yet.
  2. Data checkpointing - Saving of the generated RDDs to reliable storage. 


When to enable Checkpointing
  1. Usage of stateful transformations - If either updateStateByKey or reduceByKeyAndWindow (with inverse function) is used in the application, then the checkpoint directory must be provided to allow for periodic(周期的) RDD checkpointing.
  2. Recovering from failures of the driver running the application - Metadata checkpoints are used to recover with progress information.
How to configure Checkpointing
    Checkpointing can be enabled by setting a directory in a fault-tolerant, reliable file system (e.g., HDFS, S3, etc.) to which the checkpoint information will be saved
    done by using streamingContext.checkpoint(checkpointDirectory)
     if you want to make the application recover from driver failures, you should rewrite your streaming application to have the following behavior:
  1. When the program is being started for the first time, it will create a new StreamingContext, set up all the streams and then call start().
  2. When the program is being restarted after failure, it will re-create a StreamingContext from the checkpoint data in the checkpoint directory.
This behavior is made simple by using StreamingContext.getOrCreate
  1. # Function to create and setup a new StreamingContext
  2. def functionToCreateContext():
  3. sc = SparkContext(...) # new context
  4. ssc = new StreamingContext(...)
  5. lines = ssc.socketTextStream(...) # create DStreams
  6. ...
  7. ssc.checkpoint(checkpointDirectory) # set checkpoint directory
  8. return ssc
  9. # Get StreamingContext from checkpoint data or create a new one
  10. context = StreamingContext.getOrCreate(checkpointDirectory, functionToCreateContext)
  11. # Do additional setup on context that needs to be done,
  12. # irrespective of whether it is being started or restarted
  13. context. ...
  14. # Start the context
  15. context.start()
  16. context.awaitTermination()
如果checkpointDirectory存在,会从检查点重新新建
如果路径不存在,函数functionToCreateContext会创建新的context
You can also explicitly(明确的) create a StreamingContext from the checkpoint data and start the computation by using
  1. StreamingContext.getOrCreate(checkpointDirectory, None).
In addition to using getOrCreate one also needs to ensure that the driver process gets restarted automatically on failure,This is further discussed in the Deployment section

At small batch sizes (say 1 second), checkpointing every batch may significantly reduce operation throughput。
he default interval is a multiple of the batch interval that is at least 10 seconds
It can be set by using dstream.checkpoint(checkpointInterval)
Typically, a checkpoint interval of 5 - 10 sliding intervals of a DStream is a good setting to try.



Deploying Applications

Requirements
  • Cluster with a cluster manager
  • Package the application JAR
    If you are using spark-submit to start the application, then you will not need to provide Spark and Spark Streaming in the JAR. However, if your application uses advanced sources (e.g. Kafka, Flume), then you will have to package the extra artifact they link to, along with their dependencies, in the JAR that is used to deploy the application.
  • Configuring sufficient memory for the executors
    Note that if you are doing 10 minute window operations, the system has to keep at least last 10 minutes of data in memory. So the memory requirements for the application depends on the operations used in it.
  • Configuring checkpointing
  • Configuring automatic restart of the application driver
    • Spark Standalone 
      the Standalone cluster manager can be instructed to supervise the driver, and relaunch it if the driver fails either due to non-zero exit code, or due to failure of the node running the driver.
    • YARN automatically restarting an application
    • Mesos  Marathon has been used to achieve this with Mesos
  • Configuring write ahead logs
     If enabled, all the data received from a receiver gets written into a write ahead log in the configuration checkpoint directory.
  • Setting the max receiving rate


Upgrading Application Code
两种机制去更新代码
  1. 更新的应用和旧的应用并行的执行,Once the new one (receiving the same data as the old one) has been warmed up and is ready for prime time, the old one be can be brought down.这要求,数据源可以向两个地方发送数据。
  2. 优雅的停止,就是处理完接受到的数据之后再停止。ensure data that has been received is completely processed before shutdown。Then the upgraded application can be started, which will start processing from the same point where the earlier application left off.为了实现这个需要数据源的数据是可以缓存的。


Monitoring Applications
 


Performance Tuning

目的或者方式
  1. Reducing the processing time of each batch of data by efficiently using cluster resources.
  2. Setting the right batch size such that the batches of data can be processed as fast as they are received (that is, data processing keeps up with the data ingestion).

Level of Parallelism in Data Receiving
Level of Parallelism in Data Processing

Spark Streaming官方文档学习--下的更多相关文章

  1. Spark Streaming官方文档学习--上

    官方文档地址:http://spark.apache.org/docs/latest/streaming-programming-guide.html Spark Streaming是spark ap ...

  2. Spark监控官方文档学习笔记

    任务的监控和使用 有几种方式监控spark应用:Web UI,指标和外部方法 Web接口 每个SparkContext都会启动一个web UI,默认是4040端口,用来展示一些信息: 一系列调度的st ...

  3. Spring 4 官方文档学习(十一)Web MVC 框架

    介绍Spring Web MVC 框架 Spring Web MVC的特性 其他MVC实现的可插拔性 DispatcherServlet 在WebApplicationContext中的特殊的bean ...

  4. Spark SQL 官方文档-中文翻译

    Spark SQL 官方文档-中文翻译 Spark版本:Spark 1.5.2 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 Data ...

  5. Spring 4 官方文档学习(十二)View技术

    关键词:view technology.template.template engine.markup.内容较多,按需查用即可. 介绍 Thymeleaf Groovy Markup Template ...

  6. Spring 4 官方文档学习(十一)Web MVC 框架之配置Spring MVC

    内容列表: 启用MVC Java config 或 MVC XML namespace 修改已提供的配置 类型转换和格式化 校验 拦截器 内容协商 View Controllers View Reso ...

  7. Spring Data Commons 官方文档学习

    Spring Data Commons 官方文档学习   -by LarryZeal Version 1.12.6.Release, 2017-07-27 为知笔记版本在这里,带格式. Table o ...

  8. Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图

    接前面的Spring 4 官方文档学习(十一)Web MVC 框架,那篇太长,故另起一篇. 针对web应用的所有的MVC框架,都会提供一种呈现views的方式.Spring提供了view resolv ...

  9. Spring Boot 官方文档学习(一)入门及使用

    个人说明:本文内容都是从为知笔记上复制过来的,样式难免走样,以后再修改吧.另外,本文可以看作官方文档的选择性的翻译(大部分),以及个人使用经验及问题. 其他说明:如果对Spring Boot没有概念, ...

随机推荐

  1. oracle sql语言模糊查询--通配符like的使用教程

    转自:http://www.cnblogs.com/tyler2000/archive/2011/04/28/oracleSql.html oracle在Where子句中,可以对datetime.ch ...

  2. lz: linux ls 变种 只显示大小和名称(包括目录)

    本次输入法使用: 手心输入法 for Mac 1.0版 测试环境为:Ubuntu 14.14.2 LTS updates 测试时间为:2015年5月28日,感觉死亡将至的夜晚,独自一人坐在一个角落,戴 ...

  3. JS和CSS的多浏览器兼容(3)

    3.Javascript的浏览器兼容性问题 3.1 集合类对象问题说明:IE下,可以使用()或[]获取集合类对象; Safari及Chrome下,只能使用[]获取集合类对象. 解决方法:统一使用[]获 ...

  4. iptables禁止端口和开放端口

    1.关闭所有的 INPUT FORWARD OUTPUT 只对某些端口开放. 下面是命令实现: iptables -P INPUT DROP iptables -P FORWARD DROP ipta ...

  5. C#中一个关于不同窗体间的颜色参数的传递

    1目标是 在弹出菜单中选择颜色,在主菜单中对控件进行操作(弹出菜单选择的颜色就是主菜单控件的颜色) 2颜色属性需要来回转换(也许不用转换,暂时还不会,有会的提醒下,TKS) 3用到一个颜色控件(col ...

  6. 浅谈SQL中的单引号

    单引号:对很对计算机语言包括(SQL)是做字符串引用的:这个是大家通常知道的作用:但是对SQL语言来说:还有另外一个作用是作引号的转义 总结下:对oracle(sql)的作用. 做字符串引用:例如'a ...

  7. 有三个线程T1 T2 T3,如何保证他们按顺序执行-转载

    T3先执行,在T3的run中,调用t2.join,让t2执行完成后再执行t3 在T2的run中,调用t1.join,让t1执行完成后再让T2执行 public class Test { // 1.现在 ...

  8. JavaScript push()和splice()方法

    JavaScript push() 方法 定义和用法 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. 语法 arrayObject.push(newelement1,newele ...

  9. [OSG][转]osg格式文件

    转自:http://blog.csdn.net/timothyfly/article/details/7826139 osg格式文件中如何处理多个节点共享一个子节点 下面一段程序中,共有三个Group ...

  10. python :生产者和消费者模型 即简单的协程

    def consumer(name): print('%s开始准备吃包子了' %name) while True: baozi=yield print('[%s]包子来了,被[%s]吃了' %(bao ...