<?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 Rajesh kumar on Medium]]></title>
        <description><![CDATA[Stories by Rajesh kumar on Medium]]></description>
        <link>https://medium.com/@rajeshku9560?source=rss-b94aa10a3a11------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*G7FheAI2lnds33dr.jpg</url>
            <title>Stories by Rajesh kumar on Medium</title>
            <link>https://medium.com/@rajeshku9560?source=rss-b94aa10a3a11------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 06:01:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rajeshku9560/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[Best Approaches to Add Row Number in PySpark DataFrame]]></title>
            <link>https://rajeshku9560.medium.com/best-approaches-to-add-row-number-in-pyspark-dataframe-b203342a8916?source=rss-b94aa10a3a11------2</link>
            <guid isPermaLink="false">https://medium.com/p/b203342a8916</guid>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[pyspark]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[spark]]></category>
            <dc:creator><![CDATA[Rajesh kumar]]></dc:creator>
            <pubDate>Tue, 17 Sep 2024 08:45:44 GMT</pubDate>
            <atom:updated>2024-09-17T08:45:44.354Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*GBDhGRz6XUafDqfJhAcvpA.png" /></figure><p><strong>Recommendation:</strong></p><ul><li>monotonically_increasing_id(): Best choice for large datasets due to minimal overhead.</li><li>zipWithIndex(): Suitable for small to medium datasets or local indexing.</li></ul><p><strong>Notes:</strong></p><ul><li>zipWithIndex(): Minimal shuffling, suitable for local indexing.</li><li>monotonically_increasing_id(): No shuffling, ideal for large datasets.</li><li>row_number() with orderBy: Avoid due to high shuffling and performance impact.</li><li>repartition() + monotonically_increasing_id(): Avoid unless necessary, due to high shuffling.</li></ul><p>Example Code:</p><p><strong>Method 1: zipWithIndex()</strong></p><pre>df.rdd.zipWithIndex().toDF().select(col(&quot;_1.*&quot;),col(&quot;_2&quot;).alias(&#39;increasing_id&#39;).show()</pre><p><strong>Method 2: monotonically_increasing_id()</strong></p><pre>df.withColumn(&quot;increasing_id&quot;, monotonically_increasing_id()).show()&#39;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b203342a8916" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing SCD Type 2 in PostgreSQL: A Comprehensive Guide]]></title>
            <link>https://rajeshku9560.medium.com/implementing-scd-type-2-in-postgresql-a-comprehensive-guide-fe4367905bb9?source=rss-b94aa10a3a11------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe4367905bb9</guid>
            <category><![CDATA[data-engineer]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[scd-type-2]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[big-data-engineer]]></category>
            <dc:creator><![CDATA[Rajesh kumar]]></dc:creator>
            <pubDate>Thu, 01 Aug 2024 17:51:21 GMT</pubDate>
            <atom:updated>2024-08-01T17:51:21.863Z</atom:updated>
            <content:encoded><![CDATA[<p>Slowly Changing Dimensions (SCD) are a crucial aspect of data warehousing, allowing us to manage historical data effectively. In this blog, we will delve into implementing SCD Type 2 in PostgreSQL, which involves tracking historical changes by creating multiple records for a given natural key in the dimensional tables with different surrogate keys and timestamps.</p><h3>Creating the Staging and Dimension Tables</h3><p>First, let’s create our staging table product_stg and the dimension table product_dim_2:</p><pre>CREATE TABLE product_stg(<br>    Product_id INT,<br>    Product_Name VARCHAR(50),<br>    Price DECIMAL(9,2)<br>);<br><br>CREATE TABLE product_dim_2(<br>    product_key SERIAL PRIMARY KEY, -- Surrogate key<br>    Product_id INT,<br>    Product_Name VARCHAR(50),<br>    Price DECIMAL(9,2),<br>    start_date TIMESTAMP,<br>    end_date TIMESTAMP<br>);</pre><h3>Updating Existing Records</h3><p>The first step in handling SCD Type 2 is to update the existing records in the dimension table by setting the end_date to the current timestamp when there is a change in the price:</p><pre>UPDATE product_dim_2<br>SET end_date = CURRENT_TIMESTAMP<br>FROM product_stg<br>WHERE product_stg.Product_id = product_dim_2.Product_id<br>AND product_stg.Price &lt;&gt; product_dim_2.Price<br>AND product_dim_2.end_date IS NULL;</pre><p>This query ensures that we mark the current record as outdated by setting its end_date when there is a price change.</p><h3>Inserting New Records</h3><p>Next, we need to insert new records into the dimension table for the updated products and for new products that don’t already exist in the dimension table:</p><pre>INSERT INTO product_dim_2 (Product_id, Product_Name, Price, start_date, end_date)<br>SELECT<br>    Product_id,<br>    Product_Name,<br>    Price,<br>    CURRENT_TIMESTAMP AS start_date,<br>    NULL AS end_date<br>FROM product_stg<br>WHERE (Product_id, Price) NOT IN (<br>    SELECT Product_id, Price<br>    FROM product_dim_2<br>);</pre><p>This query inserts new records with the current timestamp as the start_date and sets the end_date to NULL indicating that these records are the most current ones.</p><h3>Using the MERGE Function</h3><p>PostgreSQL also supports the MERGE function, which can be used to simplify the SCD Type 2 implementation by combining the update and insert operations:</p><pre>MERGE INTO product_dim_2 USING<br>(<br>    SELECT product_stg.Product_id AS merge_key, product_stg.*<br>    FROM product_stg<br>    UNION ALL<br>    SELECT NULL AS merge_key, product_stg.*<br>    FROM product_stg<br>    JOIN product_dim_2 ON product_stg.Product_id = product_dim_2.Product_id<br>    WHERE product_dim_2.end_date IS NULL AND product_stg.Price &lt;&gt; product_dim_2.Price<br>) stage_update<br>ON product_dim_2.Product_id = merge_key<br>WHEN MATCHED AND product_dim_2.end_date IS NULL AND product_dim_2.Price &lt;&gt; stage_update.Price THEN<br>    UPDATE SET end_date = CURRENT_TIMESTAMP<br>WHEN NOT MATCHED THEN<br>    INSERT (Product_id, Product_Name, Price, start_date, end_date)<br>    VALUES (<br>        stage_update.Product_id, stage_update.Product_Name, stage_update.Price, CURRENT_TIMESTAMP, NULL<br>    );</pre><p>The MERGE function efficiently handles both updating the existing records and inserting new records in one go. It matches records from the staging table to the dimension table and updates the end_date where necessary, or inserts new records if there are no matches.</p><h3>Conclusion</h3><p>By following these steps, you can implement SCD Type 2 in Postgres using a combination of update, insert, and merge functions. This approach ensures that you capture changes in dimension data while maintaining a history of changes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe4367905bb9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integrating Snowflake with Git: A Step-by-Step Guide]]></title>
            <link>https://rajeshku9560.medium.com/integrating-snowflake-with-git-a-step-by-step-guide-c12ea105a577?source=rss-b94aa10a3a11------2</link>
            <guid isPermaLink="false">https://medium.com/p/c12ea105a577</guid>
            <dc:creator><![CDATA[Rajesh kumar]]></dc:creator>
            <pubDate>Tue, 16 Apr 2024 05:30:10 GMT</pubDate>
            <atom:updated>2024-04-16T05:30:10.710Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction</strong>: Integrating Snowflake with Git brings powerful version control capabilities to your data management workflows. This guide will walk you through the process of setting up this integration, from creating secrets to executing Git repository files as stored procedures in Snowflake.</p><p><strong>Prerequisites:</strong></p><ul><li>Access to a Snowflake account with appropriate privileges.</li><li>Basic understanding of Git version control system.</li><li>Familiarity with Snowflake’s concepts like roles, databases, and schemas.</li></ul><p><strong>Step 1</strong>: Setting Up Secrets for Git Authentication To authenticate with your Git repository, you need to create a secret containing your credentials. Follow these steps:</p><pre>-- Create a secret<br>CREATE OR REPLACE SECRET myco_git_secret<br>  TYPE = password<br>  USERNAME = &#39;xxxxx&#39;<br>  PASSWORD = &#39;xxxxxxxx&#39;;<br><br>-- Grant privileges on the secret<br>USE ROLE securityadmin;<br>GRANT USAGE ON SECRET myco_git_secret TO ROLE myco_git_admin;</pre><p><strong>Step 2</strong>: Creating an API Integration for Git Next, you’ll create an API integration to define how Snowflake interacts with your Git repository’s API:</p><pre>-- Create API integration<br>CREATE OR REPLACE API INTEGRATION git_api_integration<br>  API_PROVIDER = git_https_api<br>  API_ALLOWED_PREFIXES = (&#39;https://github.com/my-account/&#39;)<br>  ALLOWED_AUTHENTICATION_SECRETS = (myco_git_secret)<br>  ENABLED = TRUE;</pre><p><strong>Step 3</strong>: Configuring a Git Repository Stage Now, set up a Snowflake Git repository stage to synchronize files from your Git repository:</p><pre>-- Create Git repository stage<br>CREATE OR REPLACE GIT REPOSITORY snowflake_extensions<br>  API_INTEGRATION = git_api_integration<br>  GIT_CREDENTIALS = myco_git_secret<br>  ORIGIN = &#39;https://github.com/my-account/snowflake-extensions.git&#39;;</pre><p><strong>Step 4</strong>: Refreshing the Repository Stage.</p><p>Periodically refresh the repository stage to ensure it reflects the latest changes from the Git repository. Use ALTER GIT REPOSITORY command with the FETCH option to accomplish this.</p><p><strong>Step 5</strong>: Executing Git Repository Files Snowflake allows executing Git repository files directly, including SQL and Python scripts. Here’s how you can do it:</p><pre>-- Execute SQL file<br>EXECUTE IMMEDIATE FROM @snowflake_extensions/branches/main/sql/create-database.sql;<br><br>-- Execute Python file as stored procedure<br>CREATE OR REPLACE PROCEDURE filter_by_role(tableName VARCHAR, role VARCHAR)<br>  RETURNS TABLE(id NUMBER, name VARCHAR, role VARCHAR)<br>  LANGUAGE PYTHON<br>  RUNTIME_VERSION = &#39;3.11&#39;<br>  PACKAGES = (&#39;snowflake-snowpark-python&#39;)<br>  IMPORTS = (&#39;@myco_db.integrations.snowflake_extensions/branches/main/python-handlers/filter.py&#39;)<br>  HANDLER = &#39;filter.filter_by_role&#39;;<br><br>-- Test the stored procedure<br>CALL filter_by_role(&#39;employees&#39;, &#39;dev&#39;);</pre><p><strong>Conclusion</strong>: Integrating Snowflake with Git streamlines data management processes by leveraging version control capabilities. By following the steps outlined in this guide, you can seamlessly synchronize code and data between Snowflake and your Git repository, enhancing collaboration and ensuring data integrity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c12ea105a577" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>