KafkaDataset
The KafkaDataset
class is a subclass of the AbstractDataset
class.
aineko.datasets.kafka.KafkaDataset
KafkaDataset(
name: str, params: Dict[str, Any], test: bool = False
)
Bases: AbstractDataset
Kafka dataset.
Dataset Storage Layer is a Kafka topic.
Dataset Query Layer is a Kafka Consumer and Producer.
read
method consumes from a Kakfa topic.
write
method produces to a Kafka topic.
create
method creates the dataset topic in the Kafka cluster.
initialize
method can be used to create a consumer or producer.
delete
method deletes the dataset topic in the Kafka cluster.
exists
method checks if the dataset topic exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the dataset |
required |
params |
Dict[str, Any]
|
dataset configuration parameters |
required |
test |
bool
|
whether the dataset should be initialized in test mode |
False
|
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
name of the dataset |
topic_name |
str
|
name of the Kafka topic |
params |
dict
|
dataset configuration parameters |
credentials |
KafkaCredentials
|
Kafka credentials |
_consumer |
Consumer
|
Kafka consumer |
_producer |
Producer
|
Kafka producer |
_admin_client |
AdminClient
|
Kafka AdminClient |
cached |
bool
|
True if the consumer has been polled, False otherwise |
location |
str
|
location of the dataset |
consumer_name |
str
|
name of the consumer |
Raises:
Type | Description |
---|---|
KafkaDatasetError
|
if an error occurs while creating the dataset |
Initialize the dataset.
Source code in aineko/datasets/kafka.py
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 |
|
cached
instance-attribute
cached = False
consumer_name
instance-attribute
consumer_name: Optional[str] = None
credentials
instance-attribute
credentials = KafkaCredentials(
**get("kafka_credentials", {})
)
location
instance-attribute
location = _update_location()
source_node
instance-attribute
source_node: str
source_pipeline
instance-attribute
source_pipeline: str
consume_all
consume_all(end_message: Union[str, bool] = False) -> list
Reads all messages from the dataset until a specific one is found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
end_message |
Union[str, bool]
|
Message to trigger the completion of consumption |
False
|
Returns:
Type | Description |
---|---|
list
|
list of messages from the dataset |
Source code in aineko/datasets/kafka.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
|
create
create(
dataset_prefix: Optional[str] = None,
) -> DatasetCreationStatus
Create the dataset storage layer kafka topic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_prefix |
Optional[str]
|
Optional prefix for dataset name. |
None
|
Returns:
Type | Description |
---|---|
DatasetCreationStatus
|
status of dataset creation. |
Source code in aineko/datasets/kafka.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
delete
delete() -> None
Delete the dataset topic from the Kafka cluster.
Raises:
Type | Description |
---|---|
KafkaDatasetError
|
if an error occurs while deleting the topic |
Source code in aineko/datasets/kafka.py
248 249 250 251 252 253 254 255 256 257 258 259 |
|
exists
exists() -> bool
Check if the dataset exists.
Returns:
Type | Description |
---|---|
bool
|
True if the dataset topic exists, False otherwise |
Source code in aineko/datasets/kafka.py
366 367 368 369 370 371 372 |
|
initialize
initialize(
create: Literal["consumer", "producer"],
node_name: str,
pipeline_name: str,
prefix: Optional[str] = None,
has_pipeline_prefix: bool = False,
) -> None
Create query layer reader or writer for the dataset.
This method initializes a producer or consumer for the Kafka dataset,
depending on the value of the create
parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
create |
Literal['consumer', 'producer']
|
whether to create a consumer or producer for the dataset |
required |
node_name |
str
|
name of the node |
required |
pipeline_name |
str
|
name of the pipeline that the node belongs to |
required |
prefix |
Optional[str]
|
prefix for the dataset topic |
None
|
has_pipeline_prefix |
bool
|
Whether the dataset topic has a pipeline prefix |
False
|
Raises:
Type | Description |
---|---|
KafkaDatasetError
|
if an error occurs while creating the consumer or producer |
Source code in aineko/datasets/kafka.py
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 |
|
last
last(timeout: int = 1) -> Dict
Consumes the last message from the dataset.
Wraps the _consume_message(how="last")
method. It implements a
block that waits until a message is received before returning it.
This method ensures that the consumed message is always the most
recent message. If the consumer is slower than the producer, messages
might be skipped. If the consumer is faster than the producer,
messages might be repeated.
This is useful when the timeout is short and you expect the consumer
to often return None
.
Note: The timeout must be greater than 0 to prevent overwhelming the broker with requests to update the offset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout |
int
|
seconds to poll for a response from kafka broker. Must be >0. |
1
|
Returns:
Type | Description |
---|---|
Dict
|
message from the dataset |
Raises:
Type | Description |
---|---|
ValueError
|
if timeout is <= 0 |
Source code in aineko/datasets/kafka.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
|
next
next() -> Dict
Consumes the next message from the dataset.
Wraps the _consume_message(how="next")
method. It implements a
block that waits until a message is received before returning it.
This method ensures that every message is consumed, but the consumed
message may not be the most recent message if the consumer is slower
than the producer.
This is useful when the timeout is short and you expect the consumer
to often return None
.
Returns:
Type | Description |
---|---|
Dict
|
message from the dataset |
Source code in aineko/datasets/kafka.py
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
|
read
read(
how: Literal["next", "last"],
timeout: Optional[float] = None,
block: bool = False,
) -> Optional[Dict]
Read the dataset message via the query layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
how |
Literal['next', 'last']
|
how to read the message "next": read the next message in the queue ":last": read the last message in the queue |
required |
timeout |
Optional[float]
|
seconds to poll for a response from kafka broker. If using how="last", set to bigger than 0. |
None
|
block |
bool
|
if True, block until a message is received |
False
|
Returns:
Type | Description |
---|---|
Optional[Dict]
|
message from the dataset |
Raises:
Type | Description |
---|---|
ValueError
|
if how is not "next" or "last" |
Source code in aineko/datasets/kafka.py
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 |
|
setup_test_mode
setup_test_mode(
source_node: str,
source_pipeline: str,
input_values: Optional[List[dict]] = None,
) -> None
Sets up the dataset for testing.
The dataset is set up to return the input values when any reading method is called. Input values should be a list of dicts where the dict is the actual message payload. The dataset will handle the metadata for the messages. (timestamp, source_node, source_pipeline, etc.)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_node |
str
|
name of the source node |
required |
source_pipeline |
str
|
name of the source pipeline |
required |
input_values |
Optional[List[dict]]
|
list of input values to be used for testing |
None
|
Raises:
Type | Description |
---|---|
DatasetError
|
if the dataset is not initialized with the test flag |
Source code in aineko/datasets/kafka.py
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 |
|
write
write(msg: Dict, key: Optional[str] = None) -> None
Produce a message to the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
Dict
|
message to produce to the dataset |
required |
key |
Optional[str]
|
key to use for the message |
None
|
Raises:
Type | Description |
---|---|
KafkaDatasetError
|
if an error occurs while writing to the topic |
Source code in aineko/datasets/kafka.py
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 |
|