1. 程式人生 > >SparkStreaming 核心概念與程式設計

SparkStreaming 核心概念與程式設計

核心概念:1)StreamingContext2)DStream(batched RDDs)3)Input DStream 、Receiver4)Transformation、Output Operation(RDD的轉化、行動操作)StreamingContext常用構造方法:def this(sparkContext: SparkContext, batchDuration: Duration) = {    this(sparkContext, null, batchDuration)}def this(conf: SparkConf, batchDuration: Duration) = {    this(StreamingContext.createNewSparkContext(conf), null, batchDuration)}batch interval可以根據你的應用程式需求的延遲要求以及叢集可用的資源情況來設定一旦StreamingContext定義好之後,就可以做一些事情
  • Define the input sources by creating input DStreams.
  • Define the streaming computations by applying transformation and output operations to DStreams.(類似RDD)
  • Start receiving data and processing it using streamingContext.start().
  • Wait for the processing to be stopped (manually or due to any error) using streamingContext.awaitTermination().
  • The processing can be manually stopped using streamingContext.stop().
注意:
  • Once a context has been started, no new streaming computations(計算) can be set up or added to it.
  • Once a context has been stopped, it cannot be restarted.
  • Only one StreamingContext can be active in a JVM at the same time.
  • stop() on StreamingContext also stops the SparkContext. 
    • To stop only the StreamingContext, set the optional parameter of stop() called stopSparkContext to false.
  • A SparkContext can be re-used to create multiple StreamingContexts, as long as the previous StreamingContext is stopped (without stopping the SparkContext) before the next StreamingContext is created.
Discretized(離散) Streams (DStreams)Internally, a DStream is represented by a continuous series(一系列) of RDDsEach RDD in a DStream contains data from a certain interval對DStream操作運算元,比如map/flatMap,其實底層會被翻譯為對DStream中的每個RDD都做相同的操作;因為一個DStream是由不同批次的RDD所構成的。Any operation applied on a DStream translates to operations on the underlying RDDs. For example, in the earlier example of converting a stream of lines to words, the flatMap operation is applied on each RDD in the lines DStream to generate the RDDs of the words DStream. This is shown in the following figure.These underlying RDD transformations are computed by the Spark engineInput DStreams and ReceiversEvery input DStream (except file stream)is associated with a Receiver object which receives the data from a source and stores it in Spark’s memory for processing.Points to remember(注意)
  • When running a Spark Streaming program locally, do not use “local” or “local[1]” as the master URL. 
    • Either of these means that only one thread will be used for running tasks locally. If you are using an input DStream based on a receiver (e.g. sockets, Kafka, Flume, etc.), then the single thread will be used to run the receiver, leaving no thread for processing the received data. 
    • Hence, when running locally, always use “local[n]” as the master URL, where n > number of receivers to run (see Spark Properties for information on how to set the master).
  • Extending the logic to running on a cluster,
    •  the number of cores allocated to the Spark Streaming application must be more than the number of receivers. 
    • Otherwise the system will receive data, but not be able to process it.
Spark Streaming provides two categories of built-in streaming sources.
  • Basic sources: Sources directly available in the StreamingContext API. Examples: file systems, and socket connections.
  • Advanced sources: Sources like Kafka, Flume, etc. are available through extra utility classes. 
For reading data from files on any file system compatible with the HDFS API (that is, HDFS, S3, NFS, etc.), a DStream can be created as via StreamingContext.fileStream[KeyClass, ValueClass, InputFormatClass].Spark Streaming will monitor the directory dataDirectory and process any files created in that directory.
  • A simple directory can be monitored, such as "hdfs://namenode:8040/logs/". All files directly under such a path will be processed as they are discovered.
  • A POSIX glob pattern can be supplied, such as "hdfs://namenode:8040/logs/2017/*". Here, the DStream will consist of all files in the directories matching the pattern. 
    • That is: it is a pattern of directories, not of files in directories.
  • All files must be in the same data format.
  • A file is considered part of a time period based on its modification time, not its creation time.
  • Once processed, changes to a file within the current window will not cause the file to be reread. 
    • That is: updates are ignored.
  • The more files under a directory, the longer it will take to scan for changes — even if no files have been modified.
  • If a wildcard is used to identify directories, such as "hdfs://namenode:8040/logs/2016-*", renaming an entire directory to match the path will add the directory to the list of monitored directories. Only the files in the directory whose modification time is within the current window will be included in the stream.
  • Calling FileSystem.setTimes() to fix the timestamp is a way to have the file picked up in a later window, even if its contents have not changed.

相關推薦

SparkStreaming 核心概念程式設計

核心概念:1)StreamingContext2)DStream(batched RDDs)3)Input DStream 、Receiver4)Transformation、Output Operation(RDD的轉化、行動操作)StreamingContext常用構造方法:def this(sparkC

Spark學習(玖)- Spark Streaming核心概念程式設計

文章目錄 核心概念之StreamingContext 核心概念之DStream 核心概念之Input DStreams和Receivers 基本資源 高階資源 核心概念之Transformat

Spark Streaming核心概念編程

fyi res port 使用 文件夾 most min contain 現在 1. 核心概念 StreamingContext Create StreamingContext import org.apache.spark._ import org.apache.s

Strom核心元件程式設計模型

1.Strom介紹 Storm用來實時處理資料,特點:低延遲、高可用、分散式、可擴充套件、資料不丟失。提供簡單容易理解的介面,便於開發 2.strom與hadoop的區別  Storm用於實時計算,Hadoop用於離線計算。  Storm處理的資料儲存在記憶體中,源源不斷;Hadoop處

Docker 容器的核心概念使用

Docker背景 Docker 可以讓開發者打包他們的應用以及依賴包到一個輕量級、可移植的容器中,然後釋出到任何流行的 Linux 機器上,也可以實現虛擬化。   容器是完全使用沙箱機制,相互之間不會有任何介面(類似手機app,一個容器就是一個app),更重要的是容器效能開銷極

Elasticsearch最佳實踐之核心概念原理

  每一個系統都擁有很多概念,這些概念是作者在設計與實現時為不同的模組或功能做的定義。概念本身只是一個名詞,往往會跟隨作者的喜好不同而不同,重要的是理解其設計的初衷以及要表達的實際內容,否則很快就會忘記其意義。作為專欄文章的第二篇,本文將從多個方面對Elasticsearch的核心概念進

區塊鏈技術核心概念原理理解

區塊鏈的前世今生 說到區塊鏈,就不得不提及密碼朋克。 密碼朋克萌芽於1970年代、正式發起於1993年。 認為保護個人隱私是自由社會的重要基石,反對政府、公司對個人隱私的侵害。政權的基礎經常建立在控制資料上,通過此類控制可以害人,壓迫人,或讓人閉嘴 以程式碼和密碼學為

第2章 Docker核心概念安裝--2.2 Linux 各發行版核心升級Docker 安裝

Docker 支援在主流的作業系統平臺上使用,包括Ubuntu、Centos、Windows以及MacOS系統等。當然,在Linux系列平臺上是原生支援,使用體驗也是最好。 官方網站上有各種環境下的 安裝指南,這裡主要介紹下 Ubuntu、Debian 和CentOS 系列

SparkStreaming 運行原理核心概念

dst dstream 抽象 com size ont 分享 logs -1 SparkStreaming 運行原理 sparkstreaming 的高層抽象DStream Dstream與RDD的關系 Batch dur

Flink SQL 核心概念剖析程式設計案例實戰

本次,我們從 0 開始逐步剖析 Flink SQL 的來龍去脈以及核心概念,並附帶完整的示例程式,希望對大家有幫助! ## 本文大綱 ![](https://imgkr2.cn-bj.ufileos.com/ee018625-f90d-4159-b77c-703451cf56d9.png?UCloudPu

ElasticSearch筆記整理(二):CURL操作、ES插件、集群安裝核心概念

大數據 ElasticSearch ELK [TOC] CURL操作 CURL簡介 curl是利用URL語法在命令行方式下工作的開源文件傳輸工具,使用curl可以簡單實現常見的get/post請求。簡單的認為是可以在命令行下面訪問url的一個工具。在centos的默認庫裏面是有curl工具的,如

Spark筆記整理(二):RDDspark核心概念名詞

大數據 Spark [TOC] Spark RDD 非常基本的說明,下面一張圖就能夠有基本的理解: Spark RDD基本說明 1、Spark的核心概念是RDD (resilient distributed dataset,彈性分布式數據集),指的是一個只讀的,可分區的分布式數據集,這個數據集的全

[Flink基本概念部署]--Flink 程式設計模型【一】

一、Flink基本程式設計模型 圖1   1、Stateful Stream Processing 它位於最底層, 是core API 的底層實現;推薦學習時使用,生產中以穩定為主,不建議使用。 processFunction:開

Maven基本概念核心配置

Maven的安裝與核心配置 1、安裝Maven   1)、官網下載 Maven (http://maven.apache.org/download.cgi);   2)、解壓指定目錄;   3)、配置環境變數;   4)、使用mvn -version檢視Maven是否安裝成功

Mave基本概念核心配置

Maven的安裝與核心配置 1、安裝Maven   1)、官網下載 Maven (http://maven.apache.org/download.cgi);   2)、解壓指定目錄;   3)、配置環境變數;   4)、使用mvn -version檢視Ma

Java多執行緒程式設計總結筆記——03概念原理

作業系統中執行緒和程序的概念 現在的作業系統是多工作業系統。多執行緒是實現多工的一種方式。 程序是指一個記憶體中執行的應用程式,每個程序都有自己獨立的一塊記憶體空間,一個程序中可以啟動多個執行緒。比如在Windows系統中,一個執行的exe就是一個程序。執行

Git基本概念核心命令掌握

一、GIT體系概述 GIT 與 svn 主要區別: 儲存方式不一樣 使用方式不一樣 管理模式不一樣 二、GIT 核心命令使用 主要內容: git 客戶端安裝配置 整體認識GIT的基本使用 分支管理 標籤管理 遠端倉庫配置

js核心概念之applycall

apply /** _layout: function (pjs, node, js) { var ar = [node]; for (var i = 0; i < js.lengt

迭代器概念Traits程式設計技法

1 #include <iostream> 2 using namespace std; 3 4 template <class T> 5 struct MyIter 6 { 7 typedef T value_type; 8 T*

shell程式設計技術和例項--《linux0.01核心分析作業系統設計》

雖然不知道盧軍到底是何許人也,但是他的這本書確實寫得淺顯易懂,頗具培訓教材的意味。 linux的shell程式設計從unix起步,早期作為一種人機互動工具而呈現,經過多年的成長進步和發展。先後出現了