How to implicitly cast ISO-8601 Avro column as Timestamp column in Delta Live Table pipeline

this one has stumped me for a few days… I have a Postgres server with a table to which a deployed Debezium instance is connected, recording CDC. Debezium serialises using Avro and Confluent Schema Registry.

I have successfully built a Delta Live Table pipeline in Databricks that deserialises the Avro messages using Schema Registry. The issue is, the Postgres table has TIMESTAMPTZ columns. Thus, Debezium serialises data in these columns as ISO-8601 strings (e.g. 2024-09-16T09:17:35.621000Z) – to preserve the time zone information. Below is an example field from the Avro schema:

{
    "name": "created",
    "type": {
        "type": "string",
        "connect.version": 1,
        "connect.name": "io.debezium.time.ZonedTimestamp"
    }
}

The resultant Delta Live Table has the created column as type StringType not TimestampType. I do not care about timezone information; I only care for UTC timestamps. Now, according to the Spark docs, Avro fields are automatically interpreted to be of TimestampType when "type": "long" and "logicalType": "timestamp-micros". However, as mentioned, because the Postgres column is of type TIMESTAMPTZ, the connector serialises the data as strings, not as longs. My goal is for the resultant DLT to recognise ISO-8601 columns as being of type TimestampType.

Basically, I want Databricks to recognise that fields with "connect.name": "io.debezium.time.ZonedTimestamp" should be treated as TimestampType. The DLT pipeline code I am using is below:

from pyspark.sql.avro.functions import from_avro
from pyspark.sql.functions import col, explode, flatten, split, lit
from confluent_kafka.schema_registry import SchemaRegistryClient

def create_dlt_name(topic_name: str) -> str:
    prefix, database, schema, table = topic_name.split('.')
    return f"{database}__{schema}__{table}"

def create_dlt_for_topic(topic_name):
    dlt_name = create_dlt_name(topic_name)
    schema_registry_subject = f"{topic_name}-value"

    @dlt.table(
        name=dlt_name
    )
    def t():
        cdc_df = (
            spark.readStream
            .format("kafka")
            .option("kafka.bootstrap.servers", KAFKA_BOOTSTRAP_SERVERS)
            .option("subscribe", topic_name)
            .option("startingOffsets", "latest")
            .option("kafka.security.protocol", "SSL")
            .option("kafka.ssl.endpoint.identification.algorithm", "")
            .load()
        )

        cdc_deserialised_df = cdc_df.select(
            from_avro(
                data=col("value"),
                subject=schema_registry_subject,
                schemaRegistryAddress=SCHEMA_REGISTRY_URL
            ).alias("deserialised_value")
        )

        transposed_df = cdc_deserialised_df.select(
            col("deserialised_value.after.*"),
            col("deserialised_value.op").alias("cdc_operation"),
            col("deserialised_value.source.lsn").alias("cdc_log_sequence_number")
        )

        return transposed_df
    
for topic_name in CDC_TOPIC_NAMES:
    create_dlt_for_topic(topic_name)

I have tried using Kafka Connect SMTs to convert timestamps, but as per this question, you have to explicitly declare each field that you want to convert – which I want to avoid. If I was happy to do this, I’d be better off just calling to_timestamp on the known columns to create the final DLT.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật