{"id":771,"date":"2018-01-17T09:02:48","date_gmt":"2018-01-17T09:02:48","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=771"},"modified":"2018-01-17T09:02:48","modified_gmt":"2018-01-17T09:02:48","slug":"spark-streaming","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/","title":{"rendered":"Spark Streaming- Architecture, Working and Operations"},"content":{"rendered":"<p>As we know, there are so many distributed stream processing engines available. The question arises is why Apache spark streaming and what are its unique benefits. Spark Streaming tutorial totally aims at the topic \u201cSpark Streaming\u201d.<\/p>\n<p>To understand the topic better, we will start with basics of spark streaming, spark streaming examples and why it is needful in spark. Moreover, we will learn how streaming works in Spark, apache spark\u00a0streaming operations, sources of spark streaming.<\/p>\n<p>By the end, we will complete the topic with advantages of spark streaming over Big Data Hadoop &amp; Storm.<\/p>\n<h3>What is Spark Streaming<\/h3>\n<p>\u201c<strong>Spark Streaming<\/strong>\u201d is generally known as an <em>extension of the core Spark API<\/em>. It is a unified engine that natively supports both batch and streaming workloads. Spark streaming enables scalability, high-throughput, fault-tolerant stream processing of live data streams.<\/p>\n<p>It is a different system from others. Some engines either have streaming or have similar batch and streaming APIs, yet they compile internally to different engines.<\/p>\n<p>Therefore, streaming came into the picture. This model offers both execution and unified programming for batch and streaming. There are some advantages over other traditional streaming systems. Majorly, there are 4 aspects of it.<\/p>\n<ul>\n<li>It recovers very fast from failures and stragglers.<\/li>\n<li>Resource usage and better load balancing in spark streaming.<\/li>\n<li>Streaming data can be combined with static datasets as well as interactive queries.<\/li>\n<li>We can also integrate it with advanced processing libraries, such as SQL, machine learning, graph processing.<\/li>\n<\/ul>\n<p>Data ingestion is possible from many sources, such as Apache Flume, Kinesis, Kafka or TCP sockets. Processing is also possible by using complex algorithms expressed with high-level functions, such as map, reduce, join and window.<\/p>\n<p>Afterwards, data which is processed can be pushed out to databases,\u00a0filesystems, and live dashboards. Although, can apply spark\u2019s machine learning &amp; graph processing algorithms on data streams.<\/p>\n<h4>Internal Working<\/h4>\n<p>At first, input data streams enter further, it divides the data into batches. After that those batches processed through engine, to generate final results in batches.<\/p>\n<h3>What is Spark Streaming<\/h3>\n<p>DStream or <i>discretized stream<\/i> is a high-level abstraction of spark streaming, that represents a continuous stream of data. Creation of DStreams is possible from input data streams, from following sources, such as Kafka, Flume, and Kinesis.<\/p>\n<p>We can also create it by applying high-level operations on other DStreams. A DStream is just a sequence of spark RDDs, internally, we can write spark streaming programs in Scala, Java or Python.<\/p>\n<h3>Why Streaming in Spark is needful?<\/h3>\n<p>There is traditional stream processing systems are designed. Those are integrated with a continuous operator model for data processing, working of the system is :<\/p>\n<ul>\n<li>\u00a0At first, streaming data is received from data sources (e.g. live logs, IoT device data etc). That data sent into some data ingestion system like Apache Kafka, Amazon Kinesis, etc.<\/li>\n<li>Then data processing takes place in parallel over cluster.<\/li>\n<li>\u00a0Afterwards, results are given to downstream systems like HBase, Cassandra, Kafka, etc.<\/li>\n<\/ul>\n<p>A set of worker nodes is available, that each runs one or more continuous operators. Continuous operator processes the streaming data one record at a time. Afterwards, it forwards the records to other operators in the pipeline.<\/p>\n<p>By source operators data <span class=\"passivevoice\">is received<\/span> from ingestion systems. By sink operators, it returns as output to downstream systems. <span class=\"adverb\">Basically<\/span>, continuous operators are the natural model and very simple. Due to larger scale &amp; complex real-time analytics, the traditional system met some challenges, such as:<\/p>\n<h4>&#8211; Unification of Batch,\u00a0Streaming, and Interactive Workloads<\/h4>\n<p>To query the streaming data interactively or combining static datasets, it turns out to be very attractive, this was not easy in continuous operator systems. Those are not for new operators for ad-hoc queries. A single engine that can combine batch, streaming &amp; interactive queries is necessary.<\/p>\n<h4>&#8211; Advanced Analytics with Machine learning and SQL Queries<\/h4>\n<p>While working on complex workloads it always requires <span class=\"adverb\">continuously<\/span> learning &amp; updating data models. Also, querying the streaming data with SQL queries. Now, the developer\u2019s job much easier by having 1 abstraction across these analytic tasks.<\/p>\n<h4>&#8211; Fast Failure and Straggler Recovery<\/h4>\n<p>It is must that system is able to recover from failures automatically for results. Static allocation of continuous operators to worker nodes is challenging in traditional systems.<\/p>\n<h4>&#8211; Load Balancing<\/h4>\n<p>Uneven allocation of processing load between workers causes bottlenecks in continuous operator system. The system must be able to adapt the resource allocation based on workload.<\/p>\n<h3>Why Spark Streaming?<\/h3>\n<p>Earlier, as Hadoop have high latency that is not right for near <em>real time processing needs<\/em>. In most cases, we use Hadoop for <em>batch processing<\/em> while used Storm for <em>stream processing<\/em>.<\/p>\n<p>It leads to increase in code size, a number of bugs to fix, development effort, and causes other issues, which makes difference between Big data Hadoop and Apache Spark.<\/p>\n<p>Ultimately, Spark Streaming fixed all those issues. It provides the scalable, efficient, resilient, and integrated system. This model offers both execution and unified programming for batch and streaming.<\/p>\n<p>Although there is a major reason for its rapid adoption, is the unification of distinct data processing capabilities. It becomes hot cake for developers to use a single framework to <span class=\"complexword\">attain<\/span> all the processing needs. <span class=\"complexword\">In addition<\/span>, through Spark SQL streaming data can combine with static data sources.<\/p>\n<p>In addition, it uses a new architecture called<strong> Discretized Streams<\/strong>, that offers rich libraries of Spark and fault tolerance property of the Spark engine.<\/p>\n<h3>Architecture of Spark Streaming: Discretized Streams<\/h3>\n<p>As we know, continuous operator processes the streaming data one record at a time. Despite, processing one record at a time, it discretizes data into tiny, micro-batches. We can also say, spark streaming\u2019s receivers accept data in parallel. Also, buffer it in the memory of spark\u2019s worker&#8217;s nodes.<\/p>\n<p>Afterwards, latency optimized engine runs small jobs to process batches and returns output. From the traditional continuous model, it differs as; computation is statically allocated to a node.<\/p>\n<p>Tasks are assigned to the workers based on the locality of the data and available resources. It also enables better load balancing and faster fault recovery.<\/p>\n<p>Moreover, a DStream is just a sequence of spark RDDs, abstraction fault-tolerant dataset. This offers, that we can process streaming data by using any spark library or code.<\/p>\n<h3>Benefits of Discretized Stream Processing<\/h3>\n<h4>1. Fast failure and straggler recovery<\/h4>\n<p>Computation of lost data in case of the node failure in traditional systems is not easy. It has to restart the failed operator on another node. As in spark, the computation <span class=\"passivevoice\">is discretized<\/span> into small tasks, so that can run anywhere without affecting correctness.<\/p>\n<p><span class=\"complexword\">Therefore<\/span>, failed tasks can spread on all nodes over cluster to perform recomputations. Hence, recover from the failure is faster than the traditional approach.<\/p>\n<h4>2. Unification of batch, streaming and interactive analytics<\/h4>\n<p>DStream is just a series of Spark RDDs, that allows batch, streaming workloads to interoperate seamlessly. We can apply Spark functions on each batch of streaming data, that can be interactively queried on demand. Since spark\u2019s worker memory stores it.<\/p>\n<h4>3. Advanced analytics like machine learning and interactive SQL<\/h4>\n<p>We can also integrate it with advanced processing libraries, such as SQL, machine learning, graph processing. RDDs, which are generated by DStreams can also convert into dataframes. Afterwards, they are queried with SQL.<\/p>\n<h4>4. Performance<\/h4>\n<p>Ability to batch data and leverage spark engine leads to almost higher throughput. Latencies of spark streaming are as low as a few hundred milliseconds.<\/p>\n<h3>Working of Spark Streaming<\/h3>\n<p>Data stream converted into batches called DStreams, it is just a series of RDDs in spark that can process using Spark APIs. Afterwards, results are returned in batches. Spark streaming APIs are available in Scala, Java, and Python.<\/p>\n<p>A state based on data coming in a stream called stateful computations, it offers window operations.<\/p>\n<h3>Spark Streaming Sources<\/h3>\n<p>Input DStream is corresponding to a receiver object, that receives the data from a source and stores it in spark\u2019s memory for processing.<\/p>\n<p>There are following two types of built-in streaming sources:<\/p>\n<ul>\n<li>\n<h4>Basic sources<\/h4>\n<\/li>\n<\/ul>\n<p>The sources those are directly available in the StreamingContext API, such as file systems, and socket connections.<\/p>\n<ul>\n<li>\n<h4>Advanced sources<\/h4>\n<\/li>\n<\/ul>\n<p>Sources those are available through extra utility classes, such as Kafka, Flume, Kinesis, &amp; many more. It also requires linking against extra dependencies.<\/p>\n<p>There are two categories of a receiver, on the basis of \u00a0their reliability:<\/p>\n<ul>\n<li>\n<h4>Reliable Receiver<b> <\/b><\/h4>\n<\/li>\n<\/ul>\n<p>A receiver that sends an acknowledgment to a source, when they received the data. Also, stores in spark replication are a reliable receiver.<\/p>\n<ul>\n<li>\n<h4>Unreliable Receiver<b> <\/b><\/h4>\n<\/li>\n<\/ul>\n<p>A receiver that does not send an acknowledgment to a source. We can use it as sources if, we do not want or need to go into the complexity of acknowledgment.<\/p>\n<h3>Spark Streaming Operations<\/h3>\n<p>There are two types of operations, spark streaming supports:<\/p>\n<h4>1. Transformation Operations in Spark<\/h4>\n<p>Like RDDs, here also transformations allow modification of the data from input DStream. It also offers many transformations those are available on normal Spark RDD\u2019s. Few of them are:<\/p>\n<p>Map(), repartition(numPartitions), filter(), flatMap(), union(otherStream),\u00a0 reduce(), count(), countByValue() and many more.<\/p>\n<h4>2. Output Operations in Apache Spark<\/h4>\n<p>DStream\u2019s data can push out to external systems like a database, by output operations. Some of the following output operations are:<\/p>\n<p>print(), saveAsTextFiles(prefix, [suffix])\u201dprefix-TIME_IN_MS[.suffix]\u201d, saveAsObjectFiles(prefix, [suffix]), and many more.<\/p>\n<h3>Conclusion<\/h3>\n<p>As a result, spark streaming overcome all issues with the traditional streaming systems. Now we can handle process batch and streaming workloads at once.<\/p>\n<p>Hence, It becomes hot cake for developers to use a single framework to meet all the processing needs. Therefore, use of streaming also enhances system efficiency and performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we know, there are so many distributed stream processing engines available. The question arises is why Apache spark streaming and what are its unique benefits. Spark Streaming tutorial totally aims at the topic&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":73062,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[614],"tags":[860,861,862,863,864,648,865,866,867,868,869,870,871],"class_list":["post-771","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apache-spark","tag-apache-spark-streaming","tag-architecture-of-spark-streaming","tag-diving-into-apache-spark-streamings-execution-model","tag-getting-started-with-spark-streaming","tag-operations-of-spark-streaming","tag-spark-streaming","tag-spark-streaming-examples","tag-spark-streaming-tutorial","tag-spark-streaming-a-quick-introductory-guide","tag-spark-streaming-what-is-it-and-whos-using-it","tag-streaming-in-spark","tag-what-is-spark-streaming","tag-working-of-spark-streaming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spark Streaming- Architecture, Working and Operations - TechVidvan<\/title>\n<meta name=\"description\" content=\"Spark Streaming-what is Spark Streaming, why spark streaming, Architecture of streaming in Spark, spark streaming working, operation of streaming in Spark\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spark Streaming- Architecture, Working and Operations - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Spark Streaming-what is Spark Streaming, why spark streaming, Architecture of streaming in Spark, spark streaming working, operation of streaming in Spark\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-17T09:02:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Apache-Spark-Streaming-01-scaled-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2048\" \/>\n\t<meta property=\"og:image:height\" content=\"1072\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spark Streaming- Architecture, Working and Operations - TechVidvan","description":"Spark Streaming-what is Spark Streaming, why spark streaming, Architecture of streaming in Spark, spark streaming working, operation of streaming in Spark","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/","og_locale":"en_US","og_type":"article","og_title":"Spark Streaming- Architecture, Working and Operations - TechVidvan","og_description":"Spark Streaming-what is Spark Streaming, why spark streaming, Architecture of streaming in Spark, spark streaming working, operation of streaming in Spark","og_url":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2018-01-17T09:02:48+00:00","og_image":[{"width":2048,"height":1072,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Apache-Spark-Streaming-01-scaled-1.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Spark Streaming- Architecture, Working and Operations","datePublished":"2018-01-17T09:02:48+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/"},"wordCount":1596,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Apache-Spark-Streaming-01-scaled-1.jpg","keywords":["Apache Spark streaming","architecture of spark streaming","Diving into Apache Spark Streaming's Execution Model","Getting Started With Spark Streaming","operations of spark streaming","spark streaming","spark streaming examples","Spark streaming tutorial","Spark Streaming: A quick introductory guide","Spark Streaming: What Is It and Who's Using It?","streaming in spark","what is spark streaming","working of spark streaming"],"articleSection":["Spark Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/spark-streaming\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/","url":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/","name":"Spark Streaming- Architecture, Working and Operations - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Apache-Spark-Streaming-01-scaled-1.jpg","datePublished":"2018-01-17T09:02:48+00:00","description":"Spark Streaming-what is Spark Streaming, why spark streaming, Architecture of streaming in Spark, spark streaming working, operation of streaming in Spark","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/spark-streaming\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Apache-Spark-Streaming-01-scaled-1.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Apache-Spark-Streaming-01-scaled-1.jpg","width":2048,"height":1072,"caption":"apache spark streaming architechture"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/spark-streaming\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Spark Streaming- Architecture, Working and Operations"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=771"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/771\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/73062"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}