> ## Documentation Index
> Fetch the complete documentation index at: https://docs.e6data.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kafka

> Read a bounded set of Kafka records through a named connection.

How-to guide

The Kafka connector reads records from configured Kafka connections. It exposes message keys, values, topic metadata, offsets, and optional headers. Kafka is a source only; this engine does not provide a Kafka sink.

## Before you start

Prepare a Kafka topic and an identity that can inspect its metadata and read its records. Brokers must be reachable from the engine and workers. Use [Kafka connection settings](https://docs.e6data.com/transformation/reference/configuration/kafka-connections.md) to configure a named connection before starting the engine.

For example, a connection named `events` can use these non-secret settings:

```toml theme={"theme":{"light":"github-light","dark":"github-dark"}}
[kafka_connections.events]
bootstrap_servers = "<broker-host>:9092"
timeout_ms = 30000
max_partitions = 1024
max_offsets_per_batch = 100000
max_buffered_bytes = 67108864
```

Set TLS and authentication properties for your brokers. Store sensitive values through `secret_properties` and the configured secret resolver. Do not put passwords in source options or examples.

Prepare a Spark Connect client using [Connect a Python client](https://docs.e6data.com/transformation/connectors/files.md#connect-a-python-client). Use a small private topic containing two known text messages, `first` and `second`, to check the example.

## Read records

This client read captures a bounded set of records. It uses the selected partitions' end offsets when the source is planned.

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
records = (
    spark.read.format("kafka")
    .option("connection", "events")
    .option("subscribe", "<private-topic>")
    .option("startingOffsets", "earliest")
    .option("includeHeaders", "true")
    .load()
)

messages = records.selectExpr(
    "CAST(value AS STRING) AS message", "topic", "partition", "offset"
)
messages.orderBy("topic", "partition", "offset").show(truncate=False)
```

For a topic containing only the two sample messages, expect `first` and `second`. Order is defined by offsets within each partition. There is no single global order across partitions. Messages added after the planned end offsets are outside that read.

`startingOffsets = "latest"` starts at those end offsets, so a bounded read can return no rows. Use `earliest` or explicit offsets to read retained records.

## Read selected partitions

Use `assign` instead of `subscribe` to select partitions explicitly. Replace `orders` with your topic and use partition numbers that exist.

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
records = (
    spark.read.format("kafka")
    .option("connection", "events")
    .option("assign", '{"orders":[0,1]}')
    .option("startingOffsets", '{"orders":{"0":0,"1":0}}')
    .load()
)
records.select("topic", "partition", "offset").show()
```

The example offsets must still be available. An explicit starting-offset map must cover every selected partition. Use `-2` for the earliest retained offset or `-1` for the latest offset in that map.

## Record schema

| Column | Type | Meaning |
| - | - | - |
| `key` | Binary, nullable | Message key bytes. |
| `value` | Binary, nullable | Message value bytes. |
| `topic` | String | Topic name. |
| `partition` | Integer | Partition number. |
| `offset` | Big integer | Offset within that partition. |
| `timestamp` | UTC timestamp, nullable | Broker record timestamp. |
| `timestampType` | Integer, nullable | Kafka timestamp type. |
| `headers` | Array of key/value structs | Included only when `includeHeaders` is `true`. Header keys are strings and values are nullable binary data. |

The connector returns a fixed record schema. It does not infer a message schema or accept a user-supplied source schema. Cast known text values or decode supported payloads after reading them. Avro file support does not add automatic schema-registry decoding to Kafka records.

## Source options

| Option | Default | Meaning |
| - | - | - |
| `connection` | Required unless brokers are supplied | Name from `kafka_connections`. |
| `kafka.bootstrap.servers` | Unset | Alternative to `connection`; must exactly match one configured connection. |
| `subscribe` | Required unless `assign` is supplied | Comma-separated topic names. |
| `assign` | Unset | JSON map of topic names to partition arrays. |
| `startingOffsets` | `earliest` | `earliest`, `latest`, or a JSON map covering selected partitions. |
| `includeHeaders` | `false` | Include the `headers` column. |
| `failOnDataLoss` | `true` | Data-loss checks cannot be disabled. |

Choose exactly one of `connection` and `kafka.bootstrap.servers`. Choose exactly one of `subscribe` and `assign`. Broker security and network properties belong in the connection definition.

## Limits

The source validates cluster, topic, and partition identity. Starting offsets outside retained history fail. Connection limits bound partition count, network operations, and broker-response buffers.

There is no Kafka write path. The SQL file-view form does not accept `USING kafka`; use the client reader above. Arbitrary Kafka source options, `subscribePattern`, and `endingOffsets` are not accepted.

To store query results, select a supported [sink connector](https://docs.e6data.com/transformation/connectors.md), such as Iceberg or a file dataset.

## Cleanup

Close the client when finished. If you created a private topic for this example, remove only that topic through your Kafka tools. Keep shared topics and named connections in place.

## Related guides

* [Kafka connection settings](https://docs.e6data.com/transformation/reference/configuration/kafka-connections.md)
* [Secrets](https://docs.e6data.com/transformation/learn/secrets.md)
* [Connector support matrix](https://docs.e6data.com/transformation/connectors.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.