<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Spark Streaming Issues while performing left join in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/spark-streaming-issues-while-performing-left-join/m-p/55193#M1974</link>
    <description>&lt;P&gt;Hi team,&lt;/P&gt;&lt;P&gt;I'm struck in a Spark Structured streaming use-case.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Requirement&lt;/STRONG&gt;:&amp;nbsp;To read two streaming data frames, perform a left join on it and display the results.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Issue&lt;/STRONG&gt;:&amp;nbsp;While performing a left join, the resultant data frame contains only rows where there was a match and discards the rest of unmatched rows.&lt;/P&gt;&lt;P&gt;Ex:&lt;/P&gt;&lt;P&gt;Left table - 200 rows,&amp;nbsp;Right table - 150 rows&lt;/P&gt;&lt;P&gt;Final output - 150 rows(These are the ones with key matches).&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Code Snippet:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;As stream-stream join requires defining&amp;nbsp;watermark column and event-time range conditions, I have applied the same in the below code.&lt;/P&gt;&lt;P&gt;My data sources used in this example are static tables(table 1, table 2), but just to process just the incremental data I used spark.readStream() while performing the read. Kindly note there are no duplicates on either tables.&lt;/P&gt;&lt;P&gt;# COMMAND ----------&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;bronze_df1 = spark.readStream.table("catalog.schema.table1").withColumn("load_timestamp", current_timestamp())&lt;BR /&gt;display(bronze_df1)&amp;nbsp; #200 rows&lt;BR /&gt;# COMMAND ----------&lt;/P&gt;&lt;P&gt;bronze_df2 = spark.readStream.table("catalog.schema.table2").withColumn("load_timestamp", current_timestamp())&lt;BR /&gt;display(bronze_df2) #150 rows&lt;BR /&gt;# COMMAND ----------&lt;/P&gt;&lt;P&gt;bronze_df1 = bronze_df1.withWatermark("load_timestamp","5 minutes")&lt;BR /&gt;bronze_df1.createOrReplaceTempView("bronze_df1_view") # We can create a view out of&amp;nbsp; streaming dataframe as per Spark Streaming documentation&lt;BR /&gt;display(spark.sql("select * from bronze_df1_view")) #200 rows&lt;BR /&gt;# COMMAND ----------&lt;/P&gt;&lt;P&gt;bronze_df2 = bronze_df2.withWatermark("load_timestamp","5 minutes")&lt;BR /&gt;bronze_df2.createOrReplaceTempView("bronze_df2_view")&lt;BR /&gt;display(spark.sql("select * from bronze_df2_view"))&lt;/P&gt;&lt;P&gt;# COMMAND ----------&lt;/P&gt;&lt;P&gt;# Define the SQL query to transform the&amp;nbsp;&lt;BR /&gt;final_df = spark.sql(f"""&lt;/P&gt;&lt;P&gt;select&lt;BR /&gt;a.id,&lt;BR /&gt;a.asset_type,&lt;BR /&gt;a.country_code,&lt;BR /&gt;a.state_code,&lt;BR /&gt;b.id, --This can be null,&lt;BR /&gt;a.load_timestamp&lt;BR /&gt;FROM bronze_df1_view a&lt;BR /&gt;LEFT OUTER JOIN bronze_df2_view b&lt;BR /&gt;ON&amp;nbsp;&lt;/P&gt;&lt;P&gt;a.id = b.id and&lt;BR /&gt;b.load_timestamp between a.load_timestamp - interval 10 minutes and a.load_timestamp + interval 10 minutes&lt;/P&gt;&lt;P&gt;--Giving a bigger event-time range just to ensure none of the data gets ruled out&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;""")&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;display(final_df) #As soon as the execution starts, I could see 150 matched rows and rest of the unmatched rows never get printed on the console despite letting the job run for a longer time.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;P.S:&lt;/STRONG&gt; As mentioned earlier, mine is a very simple streaming use-case (Just to process incremental data). Since the source tables are static tables, I don't expect any data arriving late or out-of-order. I have tried using multiple values in watermark column ranging from 0 seconds to 1 hours, yet every time&amp;nbsp;I see left join results are same as inner join.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;P.P.S:&lt;/STRONG&gt;&amp;nbsp;I have been stuck into this issue for over a month now, any heads up you can provide will be much appreciated. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Dec 2023 13:08:36 GMT</pubDate>
    <dc:creator>RamanP9404</dc:creator>
    <dc:date>2023-12-13T13:08:36Z</dc:date>
    <item>
      <title>Spark Streaming Issues while performing left join</title>
      <link>https://community.databricks.com/t5/get-started-discussions/spark-streaming-issues-while-performing-left-join/m-p/55193#M1974</link>
      <description>&lt;P&gt;Hi team,&lt;/P&gt;&lt;P&gt;I'm struck in a Spark Structured streaming use-case.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Requirement&lt;/STRONG&gt;:&amp;nbsp;To read two streaming data frames, perform a left join on it and display the results.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Issue&lt;/STRONG&gt;:&amp;nbsp;While performing a left join, the resultant data frame contains only rows where there was a match and discards the rest of unmatched rows.&lt;/P&gt;&lt;P&gt;Ex:&lt;/P&gt;&lt;P&gt;Left table - 200 rows,&amp;nbsp;Right table - 150 rows&lt;/P&gt;&lt;P&gt;Final output - 150 rows(These are the ones with key matches).&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Code Snippet:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;As stream-stream join requires defining&amp;nbsp;watermark column and event-time range conditions, I have applied the same in the below code.&lt;/P&gt;&lt;P&gt;My data sources used in this example are static tables(table 1, table 2), but just to process just the incremental data I used spark.readStream() while performing the read. Kindly note there are no duplicates on either tables.&lt;/P&gt;&lt;P&gt;# COMMAND ----------&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;bronze_df1 = spark.readStream.table("catalog.schema.table1").withColumn("load_timestamp", current_timestamp())&lt;BR /&gt;display(bronze_df1)&amp;nbsp; #200 rows&lt;BR /&gt;# COMMAND ----------&lt;/P&gt;&lt;P&gt;bronze_df2 = spark.readStream.table("catalog.schema.table2").withColumn("load_timestamp", current_timestamp())&lt;BR /&gt;display(bronze_df2) #150 rows&lt;BR /&gt;# COMMAND ----------&lt;/P&gt;&lt;P&gt;bronze_df1 = bronze_df1.withWatermark("load_timestamp","5 minutes")&lt;BR /&gt;bronze_df1.createOrReplaceTempView("bronze_df1_view") # We can create a view out of&amp;nbsp; streaming dataframe as per Spark Streaming documentation&lt;BR /&gt;display(spark.sql("select * from bronze_df1_view")) #200 rows&lt;BR /&gt;# COMMAND ----------&lt;/P&gt;&lt;P&gt;bronze_df2 = bronze_df2.withWatermark("load_timestamp","5 minutes")&lt;BR /&gt;bronze_df2.createOrReplaceTempView("bronze_df2_view")&lt;BR /&gt;display(spark.sql("select * from bronze_df2_view"))&lt;/P&gt;&lt;P&gt;# COMMAND ----------&lt;/P&gt;&lt;P&gt;# Define the SQL query to transform the&amp;nbsp;&lt;BR /&gt;final_df = spark.sql(f"""&lt;/P&gt;&lt;P&gt;select&lt;BR /&gt;a.id,&lt;BR /&gt;a.asset_type,&lt;BR /&gt;a.country_code,&lt;BR /&gt;a.state_code,&lt;BR /&gt;b.id, --This can be null,&lt;BR /&gt;a.load_timestamp&lt;BR /&gt;FROM bronze_df1_view a&lt;BR /&gt;LEFT OUTER JOIN bronze_df2_view b&lt;BR /&gt;ON&amp;nbsp;&lt;/P&gt;&lt;P&gt;a.id = b.id and&lt;BR /&gt;b.load_timestamp between a.load_timestamp - interval 10 minutes and a.load_timestamp + interval 10 minutes&lt;/P&gt;&lt;P&gt;--Giving a bigger event-time range just to ensure none of the data gets ruled out&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;""")&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;display(final_df) #As soon as the execution starts, I could see 150 matched rows and rest of the unmatched rows never get printed on the console despite letting the job run for a longer time.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;P.S:&lt;/STRONG&gt; As mentioned earlier, mine is a very simple streaming use-case (Just to process incremental data). Since the source tables are static tables, I don't expect any data arriving late or out-of-order. I have tried using multiple values in watermark column ranging from 0 seconds to 1 hours, yet every time&amp;nbsp;I see left join results are same as inner join.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;P.P.S:&lt;/STRONG&gt;&amp;nbsp;I have been stuck into this issue for over a month now, any heads up you can provide will be much appreciated. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 13:08:36 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/spark-streaming-issues-while-performing-left-join/m-p/55193#M1974</guid>
      <dc:creator>RamanP9404</dc:creator>
      <dc:date>2023-12-13T13:08:36Z</dc:date>
    </item>
  </channel>
</rss>

