<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohit Saxena on Medium]]></title>
        <description><![CDATA[Stories by Mohit Saxena on Medium]]></description>
        <link>https://medium.com/@mohit.saxena8787?source=rss-431ba2c29f8d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*zh0xyG2T5kpgDkIX.jpg</url>
            <title>Stories by Mohit Saxena on Medium</title>
            <link>https://medium.com/@mohit.saxena8787?source=rss-431ba2c29f8d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 15:59:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mohit.saxena8787/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding Spark DataFrames as a Distributed Data Structure]]></title>
            <link>https://medium.com/@mohit.saxena8787/understanding-spark-dataframes-as-a-distributed-data-structure-d72ad67df64f?source=rss-431ba2c29f8d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d72ad67df64f</guid>
            <dc:creator><![CDATA[Mohit Saxena]]></dc:creator>
            <pubDate>Thu, 07 Aug 2025 07:20:07 GMT</pubDate>
            <atom:updated>2025-08-07T07:20:07.720Z</atom:updated>
            <content:encoded><![CDATA[<p>A Spark DataFrame is a core abstraction in Apache Spark, representing data in a distributed manner. This design enables Spark to process massive datasets efficiently by using multiple nodes in a cluster.</p><p>This article explains:</p><ol><li>How a DataFrame is a distributed data structure</li><li>How distributed storage splits and stores data</li><li>How Spark reads and partitions files (HDFS and S3)</li><li>The complete execution flow from logical planning to task execution</li><li>Role of Catalyst Optimizer in query optimization</li><li>How executors are allocated and used</li></ol><h3>1. Data in Distributed Storage</h3><p>In production environments, datasets such as CSV, JSON, or Parquet files are often stored in distributed storage systems such as:</p><ul><li>HDFS (Hadoop Distributed File System)</li><li>Cloud storage services like Amazon S3, Azure Data Lake, or Google Cloud Storage</li></ul><h3>1.1 File Splitting in HDFS</h3><p>When a file is stored in HDFS:</p><ul><li><strong>Block Size</strong>: Files are split into fixed-size blocks (e.g., 128 MB).</li><li><strong>Splitting Example</strong>: A 12.8 GB file is split into 100 blocks (12.8 GB / 128 MB).</li><li><strong>Replication</strong>: Each block is stored on multiple nodes (e.g., replication factor = 3).</li><li><strong>Distribution</strong>: Blocks are distributed across different nodes for fault tolerance.</li><li><strong>Parallel Read</strong>: Spark can read different blocks in parallel.</li></ul><h3>1.2 Storage in Amazon S3</h3><p>Amazon S3 behaves differently:</p><ul><li>Files are internally replicated across multiple devices and availability zones.</li><li>This replication is not visible to Spark.</li><li>Spark sees an S3 object as a single logical file.</li><li>If multiple files (e.g., part-0000.csv, part-0001.csv) exist, they are treated as multiple partitions.</li></ul><h3>2. How Spark Splits a Single File into Partitions</h3><p>Even a single file can be split into multiple partitions at read time.</p><h3>2.1 How It Works</h3><ul><li>Spark uses <strong>Hadoop InputFormat</strong> to calculate byte-range splits.</li><li>Each split becomes a Spark partition.</li><li>Executors read and process these partitions in parallel.</li></ul><p><strong>Example:</strong></p><ul><li>File size: 1 GB</li><li>Split size: 128 MB (default in Spark)</li><li>Number of partitions: ~8</li></ul><h3>2.2 Splittable vs. Non-splittable Formats</h3><p>In Spark, whether a file can be split into multiple partitions during reading depends on the file format and compression type:</p><ul><li><strong>CSV and JSON files (uncompressed)</strong> are splittable. Spark can divide them by byte ranges and process the chunks in parallel across multiple tasks.</li><li><strong>Parquet and ORC</strong> files are also splittable. These columnar formats are optimized for distributed processing and allow splitting by internal row groups.</li><li><strong>GZIP-compressed CSV files</strong> (e.g., CSV.gz) are <strong>not splittable</strong>. Since GZIP is a non-splittable compression codec, Spark must read the entire file using a single executor task, which limits parallelism.</li><li><strong>BZIP2-compressed CSV files</strong> (e.g., CSV.bz2) are <strong>splittable</strong>, making them a better choice than GZIP when compression is needed along with distributed processing.</li></ul><h3>3. Reading DataFrame: Logical Planning Phase</h3><p>When you run:</p><pre>df = spark.read.csv(&quot;s3a://bucket/big.csv&quot;)</pre><h3>3.1 What Happens Internally</h3><ul><li>Spark Driver contacts the cluster and storage manager to get metadata about the file.</li><li>It creates a <strong>Logical Plan</strong>, containing:</li><li>Schema (column names, types)</li><li>File path and partitioning info</li></ul><h3>3.2 Catalyst Optimizer</h3><p>The logical plan is passed to the <strong>Catalyst Optimizer</strong>, which performs:</p><ul><li><strong>Analysis</strong>: Resolves column names, data types.</li><li><strong>Logical Optimization</strong>: Pushes down filters, prunes unused columns.</li><li><strong>Physical Planning</strong>: Generates multiple physical plans and selects the most efficient one.</li><li><strong>Code Generation</strong>: Produces optimized JVM bytecode using whole-stage code generation.</li></ul><blockquote><em>At this point, </em><strong><em>NO DATA </em></strong><em>is loaded. Spark is still in the planning phase.</em></blockquote><h3>3.3 Lazy Evaluation</h3><ul><li>The logical/optimized plan exists in memory.</li><li><strong>Nothing is executed until an action is triggered</strong>, such as show(), count(), or write().</li></ul><h3>4. Execution Phase: From Plan to Processing</h3><h4>4.1 Triggering an Action</h4><p>Once an action is called, Spark:</p><ol><li>Converts the optimized logical plan to a <strong>physical plan</strong>.</li><li>The physical plan defines the exact execution strategy: how joins are performed (e.g., broadcast hash join vs. sort merge join), which operations are pipelined, and how data will be shuffled across stages.</li><li>Divides the plan into <strong>stages</strong> and <strong>tasks</strong>, considering shuffle boundaries and transformations.</li><li>Requests resources (executors) from the <strong>cluster manager</strong>.</li></ol><h4>4.2 Executor Allocation</h4><ul><li>Executors are JVMs launched on worker nodes.</li><li>Each executor is assigned memory and CPU (e.g., 5 cores, 10 GB).</li><li>Executors are launched only when an action is triggered — not before.</li></ul><h4>4.3 Task Execution</h4><ul><li>Each partition is assigned to a task.</li><li>The driver assigns these tasks to executor cores.</li><li>Executors read partitions into memory and start processing.</li><li>All of this happens in <strong>parallel</strong> across the cluster.</li></ul><h3>5. Data Locality Optimization</h3><p>Spark tries to assign partitions to executors that are closest to the data to minimize network transfer.</p><ul><li>If running on HDFS, Spark considers data locality.</li><li>In S3, locality isn’t visible; so Spark relies on network efficiency and caching.</li></ul><h3>Summary</h3><ul><li>Spark DataFrame is a distributed data structure.</li><li>Files are split into partitions by storage systems and Spark itself.</li><li>Spark builds a logical and optimized plan — <strong>but executes only when an action is triggered</strong>.</li><li><strong>Catalyst Optimizer</strong> plays a critical role in planning and optimizing query execution, including physical plan selection.</li><li>Executors are allocated only at execution time, and tasks run in parallel using assigned partitions.</li></ul><p>This design is what enables Spark to handle massive datasets with high performance and scalability.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d72ad67df64f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>