cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to use read_kafka() SQL with secret()?

rak_haq
New Contributor II

Hi,

I want to read data from the Azure Event Hub using SQL.

Can someone please give me an executable example where you can also use the connection string from the event hub using the SQL function secret(), for example?

This is what i tried but it Databricks can not create a KafkaAdmin:

 

SELECT

cast(value as STRING) as raw_json,

current_timestamp() as processing_time

FROM read_kafka(

bootstrapServers => 'host:9093',

subscribe => 'mytopic',

`kafka.sasl.jaas.config` => secret('scope', 'secret')

)

LIMIT 10
1 ACCEPTED SOLUTION

Accepted Solutions

rak_haq
New Contributor II

I found the solution und could successfully establish a connection to Event-Hub. 

 
SELECT
cast(value as STRING) as raw_json,
current_timestamp() as processing_time
FROM read_kafka(
  bootstrapServers => '<YOUR EVENT-HUB NAMESPACE>.servicebus.windows.net:9093',
  subscribe => '<YOUR TOPIC>',
  `kafka.sasl.mechanism` => 'PLAIN',
  `kafka.security.protocol` => 'SASL_SSL',
  `kafka.sasl.jaas.config` => concat(
    'kafkashaded.org.apache.kafka.common.security.scram.ScramLoginModule required ', 
    'username="$ConnectionString" ', 'password="', secret('<SCOPE>','<KEY>'), '";' -- Get your Credentials from Key Vault
)
)
Limit 10

View solution in original post

3 REPLIES 3

LRALVA
Honored Contributor

Hi @rak_haq 

Here’s how you can read from an Azure Event Hub’s Kafka endpoint in Databricks SQL—using the built-in read_kafka table function and the secret() function to inject your connection string securely:

In summary, you must:
   1.  Point bootstrapServers at your Event Hubs Kafka endpoint (<NAMESPACE>.servicebus.windows.net:9093).
   2. Tell Kafka to use SASL_SSL with PLAIN and pass the full Event Hubs connection string (including EntityPath) via         kafka.sasl.jaas.config.
   3.  Retrieve that connection string from a Databricks Secret with secret('scope','key') and wrap it correctly in the               JAAS login module config.
   4.Include the required Kafka options (security.protocol, sasl.mechanism, sasl.jaas.config) in your read_kafka call.

Example SQL

SELECT
CAST(value AS STRING) AS raw_json,
current_timestamp() AS processing_time
FROM read_kafka(
bootstrapServers => '<YOUR_NAMESPACE>.servicebus.windows.net:9093',
subscribe => '<YOUR_EVENTHUB_NAME>',
`kafka.security.protocol`=> 'SASL_SSL',
`kafka.sasl.mechanism` => 'PLAIN',
`kafka.sasl.jaas.config` => concat(
'org.apache.kafka.common.security.plain.PlainLoginModule required ',
'username="$ConnectionString" ',
'password="', secret('myScope','eventHubConnection'), '";'
)
)
LIMIT 10;

LR

rak_haq
New Contributor II

@LRALVA Thank you, but already tried this and iam getten the following Error-Message:

rak_haq_0-1745479358317.png

I read somewhere you have to use "kafkashaded.org.apache...", do u know something about? (source)

rak_haq
New Contributor II

I found the solution und could successfully establish a connection to Event-Hub. 

 
SELECT
cast(value as STRING) as raw_json,
current_timestamp() as processing_time
FROM read_kafka(
  bootstrapServers => '<YOUR EVENT-HUB NAMESPACE>.servicebus.windows.net:9093',
  subscribe => '<YOUR TOPIC>',
  `kafka.sasl.mechanism` => 'PLAIN',
  `kafka.security.protocol` => 'SASL_SSL',
  `kafka.sasl.jaas.config` => concat(
    'kafkashaded.org.apache.kafka.common.security.scram.ScramLoginModule required ', 
    'username="$ConnectionString" ', 'password="', secret('<SCOPE>','<KEY>'), '";' -- Get your Credentials from Key Vault
)
)
Limit 10

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now