@@ -327,66 +327,8 @@ You can create your own partial batch processor from scratch by inheriting the `
327
327
328
328
You can then use this class as a context manager, or pass it to ` batch_processor ` to use as a decorator on your Lambda handler function.
329
329
330
- ``` python hl_lines="3 9 24 30 37 57" title="Creating a custom batch processor"
331
- from random import randint
332
-
333
- from aws_lambda_powertools.utilities.batch import BasePartialProcessor, batch_processor
334
- import boto3
335
- import os
336
-
337
- table_name = os.getenv(" TABLE_NAME" , " table_not_found" )
338
-
339
- class MyPartialProcessor (BasePartialProcessor ):
340
- """
341
- Process a record and stores successful results at a Amazon DynamoDB Table
342
-
343
- Parameters
344
- ----------
345
- table_name: str
346
- DynamoDB table name to write results to
347
- """
348
-
349
- def __init__ (self , table_name : str ):
350
- self .table_name = table_name
351
-
352
- super ().__init__ ()
353
-
354
- def _prepare (self ):
355
- # It's called once, *before* processing
356
- # Creates table resource and clean previous results
357
- self .ddb_table = boto3.resource(" dynamodb" ).Table(self .table_name)
358
- self .success_messages.clear()
359
-
360
- def _clean (self ):
361
- # It's called once, *after* closing processing all records (closing the context manager)
362
- # Here we're sending, at once, all successful messages to a ddb table
363
- with self .ddb_table.batch_writer() as batch:
364
- for result in self .success_messages:
365
- batch.put_item(Item = result)
366
-
367
- def _process_record (self , record ):
368
- # It handles how your record is processed
369
- # Here we're keeping the status of each run
370
- # where self.handler is the record_handler function passed as an argument
371
- try :
372
- result = self .handler(record) # record_handler passed to decorator/context manager
373
- return self .success_handler(record, result)
374
- except Exception as exc:
375
- return self .failure_handler(record, exc)
376
-
377
- def success_handler (self , record ):
378
- entry = (" success" , result, record)
379
- message = {" age" : result}
380
- self .success_messages.append(message)
381
- return entry
382
-
383
-
384
- def record_handler (record ):
385
- return randint(0 , 100 )
386
-
387
- @batch_processor (record_handler = record_handler, processor = MyPartialProcessor(table_name))
388
- def lambda_handler (event , context ):
389
- return {" statusCode" : 200 }
330
+ ``` python hl_lines="9 16 31 37 44 55 68" title="Creating a custom batch processor"
331
+ -- 8 < -- " examples/batch_processing/src/custom_partial_processor.py"
390
332
```
391
333
392
334
### Caveats
0 commit comments