---
title: Stream from Apache Pulsar
description: Learn how to configure a streaming read from Apache Pulsar on Databricks, including authentication, schema details, and configuration options.
canonical_url: https://docs.databricks.com/aws/en/connect/streaming/pulsar
---

# Stream from Apache Pulsar

> [!NOTE]
> **Preview**
>
> This feature is in [Public Preview](https://docs.databricks.com/aws/en/release-notes/release-types.md).

In Databricks Runtime 14.1 and above, you can use Structured Streaming to stream data from Apache Pulsar on Databricks.

Structured Streaming provides exactly-once processing semantics for data read from Pulsar sources.

## Syntax example

The following is a basic example of using Structured Streaming to read from Pulsar:

### Python

```python
query = (spark.readStream
  .format("pulsar")
  .option("service.url", "pulsar://broker.example.com:6650")
  .option("topics", "topic1,topic2")
  .load()
)
```

### Scala

```scala
val query = spark.readStream
  .format("pulsar")
  .option("service.url", "pulsar://broker.example.com:6650")
  .option("topics", "topic1,topic2")
  .load()
```

To read from Pulsar topics, you must provide a `service.url` and one of the following options:

* `topic`
* `topics`
* `topicsPattern`

For a complete list of options, see [Configure options for Pulsar streaming read](https://docs.databricks.com/aws/en/connect/streaming/pulsar.md#options).

## <a id="auth" />Authenticate to Pulsar

Databricks supports truststore and keystore authentication to Pulsar. Databricks recommends that you use secrets to store configuration details.

For the full list of authentication options, see [Authentication](https://docs.databricks.com/aws/en/spark/api-options.md#stream-reader-pulsar-auth).

### Example

The following example demonstrates configuring authentication options:

#### Python

```python
client_auth_params = dbutils.secrets.get(scope="pulsar", key="clientAuthParams")
client_pw = dbutils.secrets.get(scope="pulsar", key="clientPw")
starting_offsets = "earliest"
trust_store_path = "/var/private/tls/truststore.jks"

# clientAuthParams is a comma-separated list of key-value pairs, such as:
# "keyStoreType:JKS,keyStorePath:/var/private/tls/client.keystore.jks,keyStorePassword:clientpw"

query = (spark.readStream
  .format("pulsar")
  .option("service.url", "pulsar://broker.example.com:6650")
  .option("topics", "topic1,topic2")
  .option("startingOffsets", starting_offsets)
  .option("pulsar.client.authPluginClassName", "org.apache.pulsar.client.impl.auth.AuthenticationKeyStoreTls")
  .option("pulsar.client.authParams", client_auth_params)
  .option("pulsar.client.useKeyStoreTls", "true")
  .option("pulsar.client.tlsTrustStoreType", "JKS")
  .option("pulsar.client.tlsTrustStorePath", trust_store_path)
  .option("pulsar.client.tlsTrustStorePassword", client_pw)
  .load()
)
```

#### Scala

```scala
val clientAuthParams = dbutils.secrets.get(scope = "pulsar", key = "clientAuthParams")
val clientPw = dbutils.secrets.get(scope = "pulsar", key = "clientPw")
val startingOffsets = "earliest"
val trustStorePath = "/var/private/tls/truststore.jks"

// clientAuthParams is a comma-separated list of key-value pairs, such as:
// "keyStoreType:JKS,keyStorePath:/var/private/tls/client.keystore.jks,keyStorePassword:clientpw"

val query = spark.readStream
  .format("pulsar")
  .option("service.url", "pulsar://broker.example.com:6650")
  .option("topics", "topic1,topic2")
  .option("startingOffsets", startingOffsets)
  .option("pulsar.client.authPluginClassName", "org.apache.pulsar.client.impl.auth.AuthenticationKeyStoreTls")
  .option("pulsar.client.authParams", clientAuthParams)
  .option("pulsar.client.useKeyStoreTls", "true")
  .option("pulsar.client.tlsTrustStoreType", "JKS")
  .option("pulsar.client.tlsTrustStorePath", trustStorePath)
  .option("pulsar.client.tlsTrustStorePassword", clientPw)
  .load()
```

## Pulsar schema

When you read from Pulsar, the schema of rows depends on the schemas of the source's topics.

* For topics with Avro or JSON schema, field names and field types are preserved in the resulting Spark DataFrame.
* For topics without schema or with a simple data type in Pulsar, the payload is loaded to a `value` column.
* If you configure the stream to read multiple topics with different schemas, set `allowDifferentTopicSchemas` to load the raw content to a `value` column.

Pulsar records have the following metadata fields:

| Column                | Type                  |
| --------------------- | --------------------- |
| `__key`               | `binary`              |
| `__topic`             | `string`              |
| `__messageId`         | `binary`              |
| `__publishTime`       | `timestamp`           |
| `__eventTime`         | `timestamp`           |
| `__messageProperties` | `map<String, String>` |

## <a id="options" />Configure options for Pulsar streaming read

For the full list of options, see [Pulsar](https://docs.databricks.com/aws/en/spark/api-options.md#stream-reader-pulsar).

## Construct starting offsets JSON

To use a custom message ID that specifies an offset, as JSON, with the `startingOffsets` option, see the following example:

```scala
import org.apache.spark.sql.pulsar.JsonUtils
import org.apache.pulsar.client.api.MessageId
import org.apache.pulsar.client.impl.MessageIdImpl

val topic = "my-topic"
// Replace these placeholder values with the message ID to start from
val ledgerId = 1L
val entryId = 0L
val partitionIndex = -1
val msgId: MessageId = new MessageIdImpl(ledgerId, entryId, partitionIndex)
val startOffsets = JsonUtils.topicOffsets(Map(topic -> msgId))

val query = spark.readStream
  .format("pulsar")
  .option("service.url", "pulsar://broker.example.com:6650")
  .option("topic", topic)
  .option("startingOffsets", startOffsets)
  .load()
```
