Read from Kafka
SparkKafkaEventhubSource
Bases: SourceInterface
This Spark source class is used to read batch or streaming data from an Eventhub using the Kafka protocol. This enables Eventhubs to be used as a source in applications like Delta Live Tables or Databricks Serverless Jobs as the Spark Eventhubs JAR is not supported in these scenarios.
The dataframe returned is transformed to ensure the schema is as close to the Eventhub Spark source as possible. There are some minor differences:
offset
is dependent onx-opt-offset
being populated in the headers provided. If this is not found in the headers, the value will be nullpublisher
is dependent onx-opt-publisher
being populated in the headers provided. If this is not found in the headers, the value will be nullpartitionKey
is dependent onx-opt-partition-key
being populated in the headers provided. If this is not found in the headers, the value will be nullsystemProperties
are identified according to the list provided in the Eventhub documentation and IoT Hub documentation
Default settings will be specified if not provided in the options
parameter:
kafka.sasl.mechanism
will be set toPLAIN
kafka.security.protocol
will be set toSASL_SSL
kafka.request.timeout.ms
will be set to60000
kafka.session.timeout.ms
will be set to60000
Examples
#Kafka Source for Streaming Queries
from rtdip_sdk.pipelines.sources import SparkKafkaEventhubSource
from rtdip_sdk.pipelines.utilities import SparkSessionUtility
# Not required if using Databricks
spark = SparkSessionUtility(config={}).execute()
connectionString = "Endpoint=sb://{NAMESPACE}.servicebus.windows.net/;SharedAccessKeyName={ACCESS_KEY_NAME};SharedAccessKey={ACCESS_KEY}=;EntityPath={EVENT_HUB_NAME}"
consumerGroup = "{YOUR-CONSUMER-GROUP}"
kafka_eventhub_source = SparkKafkaEventhubSource(
spark=spark,
options={
"startingOffsets": "earliest",
"maxOffsetsPerTrigger": 10000,
"failOnDataLoss": "false",
},
connection_string=connectionString,
consumer_group="consumerGroup"
)
kafka_eventhub_source.read_stream()
#Kafka Source for Batch Queries
from rtdip_sdk.pipelines.sources import SparkKafkaEventhubSource
from rtdip_sdk.pipelines.utilities import SparkSessionUtility
# Not required if using Databricks
spark = SparkSessionUtility(config={}).execute()
connectionString = "Endpoint=sb://{NAMESPACE}.servicebus.windows.net/;SharedAccessKeyName={ACCESS_KEY_NAME};SharedAccessKey={ACCESS_KEY}=;EntityPath={EVENT_HUB_NAME}"
consumerGroup = "{YOUR-CONSUMER-GROUP}"
kafka_eventhub_source = SparkKafkaEventhubSource(
spark=spark,
options={
"startingOffsets": "earliest",
"endingOffsets": "latest",
"failOnDataLoss": "false"
},
connection_string=connectionString,
consumer_group="consumerGroup"
)
kafka_eventhub_source.read_batch()
Required and optional configurations can be found in the Attributes and Parameter tables below. Additionally, there are more optional configurations which can be found here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spark |
SparkSession
|
Spark Session |
required |
options |
dict
|
A dictionary of Kafka configurations (See Attributes tables below). For more information on configuration options see here |
required |
connection_string |
str
|
Eventhubs connection string is required to connect to the Eventhubs service. This must include the Eventhub name as the |
required |
consumer_group |
str
|
The Eventhub consumer group to use for the connection |
required |
decode_kafka_headers_to_amqp_properties |
optional bool
|
Perform decoding of Kafka headers into their AMQP properties. Default is True |
True
|
The only configuration that must be set for the Kafka source for both batch and streaming queries is listed below.
Attributes:
Name | Type | Description |
---|---|---|
kafka.bootstrap.servers |
A comma-separated list of host︰port
|
The Kafka "bootstrap.servers" configuration. (Streaming and Batch) |
There are multiple ways of specifying which topics to subscribe to. You should provide only one of these parameters:
Attributes:
Name | Type | Description |
---|---|---|
assign |
json string {"topicA"︰[0,1],"topicB"︰[2,4]}
|
Specific TopicPartitions to consume. Only one of "assign", "subscribe" or "subscribePattern" options can be specified for Kafka source. (Streaming and Batch) |
subscribe |
A comma-separated list of topics
|
The topic list to subscribe. Only one of "assign", "subscribe" or "subscribePattern" options can be specified for Kafka source. (Streaming and Batch) |
subscribePattern |
Java regex string
|
The pattern used to subscribe to topic(s). Only one of "assign, "subscribe" or "subscribePattern" options can be specified for Kafka source. (Streaming and Batch) |
The following configurations are optional:
Attributes:
Name | Type | Description |
---|---|---|
startingTimestamp |
timestamp str
|
The start point of timestamp when a query is started, a string specifying a starting timestamp for all partitions in topics being subscribed. Please refer the note on starting timestamp offset options below. (Streaming and Batch) |
startingOffsetsByTimestamp |
JSON str
|
The start point of timestamp when a query is started, a json string specifying a starting timestamp for each TopicPartition. Please refer the note on starting timestamp offset options below. (Streaming and Batch) |
startingOffsets |
"earliest", "latest" (streaming only), or JSON string
|
The start point when a query is started, either "earliest" which is from the earliest offsets, "latest" which is just from the latest offsets, or a json string specifying a starting offset for each TopicPartition. In the json, -2 as an offset can be used to refer to earliest, -1 to latest. |
endingTimestamp |
timestamp str
|
The end point when a batch query is ended, a json string specifying an ending timestamp for all partitions in topics being subscribed. Please refer the note on ending timestamp offset options below. (Batch) |
endingOffsetsByTimestamp |
JSON str
|
The end point when a batch query is ended, a json string specifying an ending timestamp for each TopicPartition. Please refer the note on ending timestamp offset options below. (Batch) |
endingOffsets |
latest or JSON str
|
The end point when a batch query is ended, either "latest" which is just referred to the latest, or a json string specifying an ending offset for each TopicPartition. In the json, -1 as an offset can be used to refer to latest, and -2 (earliest) as an offset is not allowed. (Batch) |
maxOffsetsPerTrigger |
long
|
Rate limit on maximum number of offsets processed per trigger interval. The specified total number of offsets will be proportionally split across topicPartitions of different volume. (Streaming) |
minOffsetsPerTrigger |
long
|
Minimum number of offsets to be processed per trigger interval. The specified total number of offsets will be proportionally split across topicPartitions of different volume. (Streaming) |
failOnDataLoss |
bool
|
Whether to fail the query when it's possible that data is lost (e.g., topics are deleted, or offsets are out of range). This may be a false alarm. You can disable it when it doesn't work as you expected. |
minPartitions |
int
|
Desired minimum number of partitions to read from Kafka. By default, Spark has a 1-1 mapping of topicPartitions to Spark partitions consuming from Kafka. (Streaming and Batch) |
includeHeaders |
bool
|
Whether to include the Kafka headers in the row. (Streaming and Batch) |
Starting Timestamp Offset Note
If Kafka doesn't return the matched offset, the behavior will follow to the value of the option startingOffsetsByTimestampStrategy
.
startingTimestamp
takes precedence over startingOffsetsByTimestamp
and startingOffsets.
For streaming queries, this only applies when a new query is started, and that resuming will always pick up from where the query left off. Newly discovered partitions during a query will start at earliest.
Ending Timestamp Offset Note
If Kafka doesn't return the matched offset, the offset will be set to latest.
endingOffsetsByTimestamp
takes precedence over endingOffsets
.
Source code in src/sdk/python/rtdip_sdk/pipelines/sources/spark/kafka_eventhub.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
|
system_type()
staticmethod
Attributes:
Name | Type | Description |
---|---|---|
SystemType |
Environment
|
Requires PYSPARK |
Source code in src/sdk/python/rtdip_sdk/pipelines/sources/spark/kafka_eventhub.py
191 192 193 194 195 196 197 |
|
read_batch()
Reads batch data from Kafka.
Source code in src/sdk/python/rtdip_sdk/pipelines/sources/spark/kafka_eventhub.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
|
read_stream()
Reads streaming data from Kafka.
Source code in src/sdk/python/rtdip_sdk/pipelines/sources/spark/kafka_eventhub.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
|