---
title: Ingest data from Apache Kafka
description: Create a managed Apache Kafka ingestion pipeline in Databricks Lakeflow Connect with Declarative Automation Bundles.
canonical_url: https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-pipeline
---

# Ingest data from Apache Kafka

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/aws/en/release-notes/release-types.md). To use it, a workspace admin must turn on **Lakeflow Connect for Kafka** from the **Previews** page. See [Manage Databricks previews](https://docs.databricks.com/aws/en/admin/workspace-settings/manage-previews.md).

This page shows how to create a managed Kafka ingestion pipeline using Databricks Lakeflow Connect and Declarative Automation Bundles.

## <a id="requirements" />Requirements

To create an ingestion pipeline, you must first meet the following requirements:

* Your workspace must be enabled for Unity Catalog.

* Serverless compute must be enabled for your workspace. See [Serverless compute requirements](https://docs.databricks.com/aws/en/compute/serverless/index.md#requirements).

* To create a new connection, you must have `CREATE CONNECTION` privileges on the metastore. See [Manage privileges in Unity Catalog](https://docs.databricks.com/aws/en/data-governance/unity-catalog/manage-privileges/index.md).

  If the connector supports UI-based pipeline authoring, an admin can create the connection and the pipeline at the same time by completing the steps on this page. However, if the users who create pipelines use API-based pipeline authoring or are non-admin users, an admin must first create the connection in Catalog Explorer. See [Connect to managed ingestion sources](https://docs.databricks.com/aws/en/connect/managed-ingestion.md).

* To use an existing connection, you must have `USE CONNECTION` privileges or `ALL PRIVILEGES` on the connection object.

* You must have `USE CATALOG` privileges on the target catalog.

* You must have `USE SCHEMA` and `CREATE TABLE` privileges on an existing schema or `CREATE SCHEMA` privileges on the target catalog.

To ingest from Kafka, you must first complete the steps in [Connect to Apache Kafka for managed ingestion](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-source-setup.md).

## <a id="create-an-ingestion-pipeline" />Create an ingestion pipeline

Each Kafka topic is ingested into a streaming table. For a list of supported data and limitations, see [Supported data](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-limits.md#supported-data).

> [!NOTE]
>
> UI-based pipeline authoring is not supported for the Kafka connector in Beta. Use Declarative Automation Bundles or a Databricks notebook to create your pipeline.

### Declarative Automation Bundles

Use Declarative Automation Bundles to manage Kafka pipelines as code. Bundles can contain YAML definitions of jobs and tasks, are managed using the Databricks CLI, and can be shared and run in different target workspaces (such as development, staging, and production). For more information, see [What are Declarative Automation Bundles?](https://docs.databricks.com/aws/en/dev-tools/bundles/index.md).

1. Create a new bundle using the Databricks CLI:

   ```bash
   databricks bundle init
   ```

2. Add a pipeline definition file to the bundle (for example, `resources/kafka_pipeline.yml`). See [pipeline.ingestion\_definition](https://docs.databricks.com/aws/en/dev-tools/bundles/resources.md#pipeline-ingestion_definition) and [Examples](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-pipeline.md#examples).

3. Deploy the bundle using the Databricks CLI:

   ```bash
   databricks bundle deploy
   ```

### Databricks notebook

1. Import the following notebook into your Databricks workspace:

   [Get notebook](https://docs.databricks.com/notebooks/source/streaming/managed-kafka-pipeline.html)

2. Leave cell one as-is. Do not modify the `channel` field — it must remain `PREVIEW`.

3. Modify the cell with your pipeline configuration details, including your targeted deserialization format. See [pipeline.ingestion\_definition](https://docs.databricks.com/aws/en/dev-tools/bundles/resources.md#pipeline-ingestion_definition) and [Examples](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-pipeline.md#examples).

4. Click **Run all**.

## <a id="examples" />Examples

Use these examples to configure your pipeline.

### <a id="minimal-raw-binary" />Minimal pipeline — raw binary key and value

This example ingests one or more Kafka topics with key and value columns retained as `BINARY`:

#### Declarative Automation Bundles

```yaml
variables:
  connection_name:
    default: my-kafka-connection
  dest_catalog:
    default: main
  dest_schema:
    default: kafka_ingest
resources:
  pipelines:
    kafka_pipeline:
      name: kafka-ingestion-pipeline
      serverless: true
      continuous: true
      channel: PREVIEW
      catalog: ${var.dest_catalog}
      target: ${var.dest_schema}
      ingestion_definition:
        connection_name: ${var.connection_name}
        objects:
          - table:
              source_table: N/A
              destination_catalog: ${var.dest_catalog}
              destination_schema: ${var.dest_schema}
              destination_table: user_events
              connector_options:
                kafka_options:
                  topics: [user-events, power-user-events]
```

### <a id="with-transformers" />Pipeline with transformers — JSON value and string key

This example deserializes message keys as `STRING` and values as `JSON` with schema evolution enabled:

#### Declarative Automation Bundles

```yaml
variables:
  connection_name:
    default: my-kafka-connection
  dest_catalog:
    default: main
  dest_schema:
    default: kafka_ingest
resources:
  pipelines:
    kafka_pipeline:
      name: kafka-ingestion-pipeline
      serverless: true
      continuous: true
      channel: PREVIEW
      catalog: ${var.dest_catalog}
      target: ${var.dest_schema}
      ingestion_definition:
        connection_name: ${var.connection_name}
        objects:
          - table:
              source_table: N/A
              destination_catalog: ${var.dest_catalog}
              destination_schema: ${var.dest_schema}
              destination_table: user_events
              connector_options:
                kafka_options:
                  topics:
                    - user-events
                  starting_offset: latest
                  key_transformer:
                    format: STRING
                  value_transformer:
                    format: JSON
                    json_options:
                      schema_evolution_mode: rescue
```

### <a id="with-avro" />Pipeline with Avro values

This example deserializes message values as `AVRO`, resolving the schema at runtime from a Confluent-compatible schema registry. By default, the registry is accessed using the pipeline's Kafka source connection; to use a different Unity Catalog connection, set `connection_name` under `schema_registry`. You can instead provide the schema inline (`avro_options.schema`) or from an `.avsc` file (`avro_options.schema_file_path`). See [Avro options](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-reference.md#avro-options).

#### Declarative Automation Bundles

```yaml
variables:
  connection_name:
    default: my-kafka-connection
  dest_catalog:
    default: main
  dest_schema:
    default: kafka_ingest
resources:
  pipelines:
    kafka_pipeline:
      name: kafka-ingestion-pipeline
      serverless: true
      continuous: true
      channel: PREVIEW
      catalog: ${var.dest_catalog}
      target: ${var.dest_schema}
      ingestion_definition:
        connection_name: ${var.connection_name}
        objects:
          - table:
              source_table: N/A
              destination_catalog: ${var.dest_catalog}
              destination_schema: ${var.dest_schema}
              destination_table: user_events
              connector_options:
                kafka_options:
                  topics:
                    - user-events
                  value_transformer:
                    format: AVRO
                    avro_options:
                      schema_registry:
                        confluent_options:
                          subject: user-events-value
```

### <a id="with-protobuf" />Pipeline with Protobuf values

This example deserializes message values as `PROTOBUF`, resolving the schema at runtime from a Confluent-compatible schema registry. By default, the registry is accessed using the pipeline's Kafka source connection; to use a different Unity Catalog connection, set `connection_name` under `schema_registry`. You can instead provide a compiled descriptor set (`.desc`) file and message name (`protobuf_options.desc_file_path` and `message_name`). See [Protobuf options](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-reference.md#protobuf-options).

#### Declarative Automation Bundles

```yaml
variables:
  connection_name:
    default: my-kafka-connection
  dest_catalog:
    default: main
  dest_schema:
    default: kafka_ingest
resources:
  pipelines:
    kafka_pipeline:
      name: kafka-ingestion-pipeline
      serverless: true
      continuous: true
      channel: PREVIEW
      catalog: ${var.dest_catalog}
      target: ${var.dest_schema}
      ingestion_definition:
        connection_name: ${var.connection_name}
        objects:
          - table:
              source_table: N/A
              destination_catalog: ${var.dest_catalog}
              destination_schema: ${var.dest_schema}
              destination_table: user_events
              connector_options:
                kafka_options:
                  topics:
                    - user-events
                  value_transformer:
                    format: PROTOBUF
                    protobuf_options:
                      schema_registry:
                        confluent_options:
                          subject: user-events-value
```

### <a id="with-source-metadata" />Pipeline with a source metadata column

This example adds a `_kafka_metadata` struct column that holds the Kafka source metadata (`topic`, `partition`, `offset`, `timestamp`, `timestampType`, `headers`) for each record. Set `source_metadata_column` under `table_configuration` on the table object:

#### Declarative Automation Bundles

```yaml
variables:
  connection_name:
    default: my-kafka-connection
  dest_catalog:
    default: main
  dest_schema:
    default: kafka_ingest
resources:
  pipelines:
    kafka_pipeline:
      name: kafka-ingestion-pipeline
      serverless: true
      continuous: true
      channel: PREVIEW
      catalog: ${var.dest_catalog}
      target: ${var.dest_schema}
      ingestion_definition:
        connection_name: ${var.connection_name}
        objects:
          - table:
              source_table: N/A
              destination_catalog: ${var.dest_catalog}
              destination_schema: ${var.dest_schema}
              destination_table: user_events
              table_configuration:
                source_metadata_column: _kafka_metadata
              connector_options:
                kafka_options:
                  topics: [user-events]
```

## <a id="fanout" />Route records to multiple tables (fanout)

> [!NOTE]
> **Preview**
>
> This feature is in [Private Preview](https://docs.databricks.com/aws/en/release-notes/release-types.md). To try it, reach out to your Databricks contact.

Fanout routes each record from a single Kafka source to one of many destination tables. You configure fanout on a schema object instead of a table object. A routing key derived from each record determines the destination table name: `{destination_catalog}.{destination_schema}.{key_value}`.

Set `fanout_options` on the schema object. The `fanout_by` field is a SQL expression evaluated against the raw source record (with the Kafka `key` and `value` columns), and its result becomes the final segment (the table name) of the destination table `{destination_catalog}.{destination_schema}.{result}`. Because `fanout_by` runs on the raw record, cast the binary `value` to a string before you extract a field from it, as in the following example. Its resolved value is used as the table-name segment verbatim, without quoting or sanitization, so it must be a valid unquoted table identifier. Values with spaces, dots, or other characters that aren't valid in an unquoted identifier fail the write, as does a value that is entirely digits (for example, `123`). A leading digit is allowed when the value also contains a letter or underscore, such as `2024_events`.

You can optionally apply a single JSON transform to each route. This transform runs on each routed record after routing, so it doesn't affect the value that `fanout_by` sees. For the full list of options and the v1 constraints, see [Fanout options](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-reference.md#fanout-options) and [Fanout limitations](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-limits.md#fanout-limitations).

The following example reads records from topics matching a pattern and routes each record to a table named after its `event_type` field. The optional JSON transform parses the message `value` column in place in each destination table:

### Declarative Automation Bundles

```yaml
variables:
  connection_name:
    default: my-kafka-connection
  dest_catalog:
    default: main
  dest_schema:
    default: kafka_ingest
resources:
  pipelines:
    kafka_pipeline:
      name: kafka-ingestion-pipeline
      serverless: true
      continuous: true
      channel: PREVIEW
      catalog: ${var.dest_catalog}
      target: ${var.dest_schema}
      ingestion_definition:
        connection_name: ${var.connection_name}
        objects:
          - schema:
              destination_catalog: ${var.dest_catalog}
              destination_schema: ${var.dest_schema}
              connector_options:
                kafka_options:
                  topic_pattern: 'events-.*'
                  starting_offset: earliest
              fanout_options:
                fanout_by: 'cast(value as string):event_type::string'
                transforms:
                  - format: JSON
                    input_column: value
```

## <a id="common-patterns" />Common patterns

For advanced pipeline configurations, see [Common patterns for managed ingestion pipelines](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/common-patterns.md).

## <a id="next-steps" />Next steps

Start and set alerts on your pipeline. See [Common pipeline maintenance tasks](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/pipeline-maintenance.md).

## <a id="additional-resources" />Additional resources

* [Kafka connector FAQs](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-faq.md)
* [Apache Kafka connector limitations](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-limits.md)
* [Kafka connector reference](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-reference.md)
* [Troubleshoot the Kafka connector](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/kafka-troubleshoot.md)
