|
1 |
| -def my_fn(): |
2 |
| - return 1 |
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import logging |
| 19 | +import typing |
| 20 | +import apache_beam as beam |
| 21 | +from apache_beam import metrics |
| 22 | +from apache_beam.pvalue import PCollection |
| 23 | + |
| 24 | +from dynamodb_pyio.boto3_client import DynamoDBClient |
| 25 | +from dynamodb_pyio.options import DynamoDBOptions |
| 26 | + |
| 27 | + |
| 28 | +__all__ = ["WriteToDynamoDB"] |
| 29 | + |
| 30 | + |
| 31 | +class _DynamoDBWriteFn(beam.DoFn): |
| 32 | + """Create the connector can send messages in batch to an Amazon DynamoDB table. |
| 33 | +
|
| 34 | + Args: |
| 35 | + table_name (str): Amazon DynamoDB table name. |
| 36 | + dedup_pkeys (list): List of keys to be used for de-duplicating records in buffer. |
| 37 | + options (Union[DynamoDBOptions, dict]): Options to create a boto3 dynamodb client. |
| 38 | + """ |
| 39 | + |
| 40 | + total_elements_count = metrics.Metrics.counter( |
| 41 | + "_DynamoDBWriteFn", "total_elements_count" |
| 42 | + ) |
| 43 | + |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + table_name: str, |
| 47 | + dedup_pkeys: list, |
| 48 | + options: typing.Union[DynamoDBOptions, dict], |
| 49 | + ): |
| 50 | + """Constructor of _DynamoDBWriteFn |
| 51 | +
|
| 52 | + Args: |
| 53 | + table_name (str): Amazon DynamoDB table name. |
| 54 | + dedup_pkeys (list): List of keys to be used for de-duplicating records in buffer. |
| 55 | + options (Union[DynamoDBOptions, dict]): Options to create a boto3 dynamodb client. |
| 56 | + """ |
| 57 | + super().__init__() |
| 58 | + self.table_name = table_name |
| 59 | + self.dedup_pkeys = dedup_pkeys |
| 60 | + self.options = options |
| 61 | + |
| 62 | + def start_bundle(self): |
| 63 | + self.client = DynamoDBClient(self.options) |
| 64 | + |
| 65 | + def process(self, element): |
| 66 | + if isinstance(element, tuple): |
| 67 | + element = element[1] |
| 68 | + self.client.put_items_batch(element, self.table_name, self.dedup_pkeys) |
| 69 | + self.total_elements_count.inc(len(element)) |
| 70 | + logging.info(f"total {len(element)} elements processed...") |
| 71 | + |
| 72 | + |
| 73 | +class WriteToDynamoDB(beam.PTransform): |
| 74 | + """A transform that puts records to an Amazon DynamoDB table. |
| 75 | +
|
| 76 | + Takes an input PCollection and put them in batch using the boto3 package. |
| 77 | + For more information, visit the `Boto3 Documentation <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb/table/batch_writer.html>`__. |
| 78 | +
|
| 79 | + Note that, if the PCollection element is a tuple (i.e. keyed stream), only the value is used to put records in batch. |
| 80 | +
|
| 81 | + Args: |
| 82 | + table_name (str): Amazon DynamoDB table name. |
| 83 | + dedup_pkeys (list, Optional): List of keys to be used for de-duplicating records in buffer. |
| 84 | + """ |
| 85 | + |
| 86 | + def __init__(self, table_name: str, dedup_pkeys: list = None): |
| 87 | + """Constructor of the transform that puts records into an Amazon DynamoDB table. |
| 88 | +
|
| 89 | + Args: |
| 90 | + table_name (str): Amazon DynamoDB table name. |
| 91 | + dedup_pkeys (list, Optional): List of keys to be used for de-duplicating items in buffer. |
| 92 | + """ |
| 93 | + super().__init__() |
| 94 | + self.table_name = table_name |
| 95 | + self.dedup_pkeys = dedup_pkeys |
| 96 | + |
| 97 | + def expand(self, pcoll: PCollection): |
| 98 | + options = pcoll.pipeline.options.view_as(DynamoDBOptions) |
| 99 | + return pcoll | beam.ParDo( |
| 100 | + _DynamoDBWriteFn(self.table_name, self.dedup_pkeys, options) |
| 101 | + ) |
0 commit comments