<?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 Re: Registering a dataframe coming from a CDC data stream removes the CDC columns from the resulting temporary view, even when explicitly adding a copy of the column to the dataframe. in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/registering-a-dataframe-coming-from-a-cdc-data-stream-removes/m-p/3840#M746</link>
    <description>&lt;P&gt;Hi @Louis De Geest​&amp;nbsp;&lt;/P&gt;&lt;P&gt;​&lt;/P&gt;&lt;P&gt;This seems like a bug.&lt;/P&gt;&lt;P&gt;Can you send the snippet of error which you are getting?&lt;/P&gt;&lt;P&gt;&amp;nbsp;#DAIS2023​&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 31 May 2023 12:43:22 GMT</pubDate>
    <dc:creator>Ajay-Pandey</dc:creator>
    <dc:date>2023-05-31T12:43:22Z</dc:date>
    <item>
      <title>Registering a dataframe coming from a CDC data stream removes the CDC columns from the resulting temporary view, even when explicitly adding a copy of the column to the dataframe.</title>
      <link>https://community.databricks.com/t5/data-engineering/registering-a-dataframe-coming-from-a-cdc-data-stream-removes/m-p/3839#M745</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;                            df_source_records.filter(F.col("_change_type").isin("delete", "insert", "update_postimage"))
                            .withColumn("ROW_NUMBER", F.row_number().over(window))
                            .filter("ROW_NUMBER = 1")
                            .withColumn("change_indicator", F.col("_change_type"))
                            .drop("_commit_version", "_commit_timestamp","ROW_NUMBER")
&amp;nbsp;
&amp;nbsp;
df_source_records.createOrReplaceGlobalTempView (temporary_view_name)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When now selecting from temporary_view_name, neither the _change_type column nor the change_indicator columns are available to select from. Why is this the case? Seems like a bug? Or is there a workaround?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 07:12:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/registering-a-dataframe-coming-from-a-cdc-data-stream-removes/m-p/3839#M745</guid>
      <dc:creator>Louis_Databrick</dc:creator>
      <dc:date>2023-05-31T07:12:38Z</dc:date>
    </item>
    <item>
      <title>Re: Registering a dataframe coming from a CDC data stream removes the CDC columns from the resulting temporary view, even when explicitly adding a copy of the column to the dataframe.</title>
      <link>https://community.databricks.com/t5/data-engineering/registering-a-dataframe-coming-from-a-cdc-data-stream-removes/m-p/3840#M746</link>
      <description>&lt;P&gt;Hi @Louis De Geest​&amp;nbsp;&lt;/P&gt;&lt;P&gt;​&lt;/P&gt;&lt;P&gt;This seems like a bug.&lt;/P&gt;&lt;P&gt;Can you send the snippet of error which you are getting?&lt;/P&gt;&lt;P&gt;&amp;nbsp;#DAIS2023​&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 12:43:22 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/registering-a-dataframe-coming-from-a-cdc-data-stream-removes/m-p/3840#M746</guid>
      <dc:creator>Ajay-Pandey</dc:creator>
      <dc:date>2023-05-31T12:43:22Z</dc:date>
    </item>
    <item>
      <title>Re: Registering a dataframe coming from a CDC data stream removes the CDC columns from the resulting temporary view, even when explicitly adding a copy of the column to the dataframe.</title>
      <link>https://community.databricks.com/t5/data-engineering/registering-a-dataframe-coming-from-a-cdc-data-stream-removes/m-p/3841#M747</link>
      <description>&lt;P&gt;Seems to work now actually. No idea what changed, as I tried multiple times exactly in this way and it did.not.work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark.sql.functions import expr
from pyspark.sql.utils import AnalysisException
import pyspark.sql.functions as f
&amp;nbsp;
&amp;nbsp;
data = [("John", 25), ("Alice", 30), ("Bob", 35)]
df = spark.createDataFrame(data, ["Name", "Age"])
&amp;nbsp;
try:
    ENTER_YOUR_DATASTORE_HERE = ""
    path = 'abfss://bronze@' + ENTER_YOUR_DATASTORE_HERE + '.dfs.core.windows.net/TEST/test_table'
    df.write.format("delta").mode("overwrite").save(path)
except AnalysisException as e:
    print("Error while writing the dataframe to Delta Lake:", e)
&amp;nbsp;
try:
    spark.sql("CREATE DATABASE IF NOT EXISTS test")
    spark.sql("USE test")
    spark.sql(f"""
        CREATE TABLE IF NOT EXISTS test_table
        USING delta
        LOCATION '{path}'
    """)
except AnalysisException as e:
    print("Error while creating the catalog entry:", e)
&amp;nbsp;
try:
    spark.sql("""
        ALTER TABLE test_table
        SET TBLPROPERTIES ('delta.enableChangeDataCapture' = 'true')
    """)
except AnalysisException as e:
    print("Error while enabling CDC on the table:", e)
&amp;nbsp;
def process_batch(df):
    display(df)
    # CDC columns available.
    df.createOrReplaceGlobalTempView("test_view")
    # They seem to show up now as well
    df_new = spark.sql("SELECT _change_type FROM global_temp.test_view")
    display(df_new)
&amp;nbsp;
&amp;nbsp;
streaming_query = spark.readStream \
    .format("delta") \
    .option("readChangeFeed", "true") \
    .load(path) \
&amp;nbsp;
&amp;nbsp;
(streaming_query.writeStream 
    .foreachBatch(lambda batch_df, batch_id:
                      process_batch(
                        df = batch_df
                      )) 
    .outputMode("append")
    .trigger(once=True)
    .start()
    .awaitTermination()
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jun 2023 11:15:24 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/registering-a-dataframe-coming-from-a-cdc-data-stream-removes/m-p/3841#M747</guid>
      <dc:creator>Louis_Databrick</dc:creator>
      <dc:date>2023-06-08T11:15:24Z</dc:date>
    </item>
  </channel>
</rss>

