DatasetConsumer
The DatasetConsumer
class is used to consume from Kafka
topics.
aineko.DatasetConsumer
Wrapper class for Kafka consumer object.
DatasetConsumer objects are designed to consume messages from a single dataset and will consume the next unconsumed message in the queue.
When accessing kafka topics, prefixes will automatically be added to the
dataset name as part of namespacing. For datasets defined in the pipeline
config, has_pipeline_prefix
will be set to True
, so a dataset named
my_dataset
will point to a topic named my_pipeline.my_dataset
.
Optionally, a custom prefix can be provided that will apply to all datasets.
In the above example, if the prefix is set to test
, the topic name will
be test.my_pipeline.my_dataset
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name |
str
|
name of the dataset |
required |
node_name |
str
|
name of the node that is consuming the dataset |
required |
pipeline_name |
str
|
name of the pipeline |
required |
dataset_config |
Dict[str, Any]
|
dataset config |
required |
bootstrap_servers |
Optional[str]
|
bootstrap_servers to connect to (e.g. "1.2.3.4:9092") |
None
|
prefix |
Optional[str]
|
prefix for topic name ( |
None
|
has_pipeline_prefix |
bool
|
whether the dataset name has pipeline name prefix |
False
|
Attributes:
Name | Type | Description |
---|---|---|
consumer |
Kafka consumer object |
|
cached |
if the high watermark offset has been cached (updated when message consumed) |
Methods:
Name | Description |
---|---|
consume |
reads a message from the dataset |
Source code in aineko/core/dataset.py
40 41 42 43 44 45 46 47 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 |
|
__init__(dataset_name, node_name, pipeline_name, dataset_config, bootstrap_servers=None, prefix=None, has_pipeline_prefix=False)
Initialize the consumer.
Source code in aineko/core/dataset.py
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 |
|
consume(how='next', timeout=None)
Polls a message from the dataset.
If the consume method is last but the method encounters an error trying to udpdate the offset to latest, it will poll and return None.
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 |
'next'
|
timeout |
Optional[float]
|
seconds to poll for a response from kafka broker. If using how="last", set to bigger than 0. |
None
|
Returns:
Type | Description |
---|---|
Optional[dict]
|
message from the dataset |
Raises:
Type | Description |
---|---|
ValueError
|
if how is not "next" or "last" |
Source code in aineko/core/dataset.py
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 |
|
consume_all(end_message=False)
Reads all messages from the dataset until a specific one is found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
end_message |
str | bool
|
Message to trigger the completion of consumption |
False
|
Returns:
Type | Description |
---|---|
list
|
list of messages from the dataset |
Source code in aineko/core/dataset.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
last(timeout=1)
Consumes the last message from the dataset.
Wraps the consume(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/core/dataset.py
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 |
|
next()
Consumes the next message from the dataset.
Wraps the consume(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/core/dataset.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
|