|
| 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 typing |
| 19 | +import boto3 |
| 20 | +from apache_beam.options import pipeline_options |
| 21 | + |
| 22 | +from dynamodb_pyio.options import DynamoDBOptions |
| 23 | + |
| 24 | +__all__ = ["DynamoDBClient", "DynamoDBClientError"] |
| 25 | + |
| 26 | + |
| 27 | +def get_http_error_code(exc): |
| 28 | + if hasattr(exc, "response"): |
| 29 | + return exc.response.get("ResponseMetadata", {}).get("HTTPStatusCode") |
| 30 | + return None |
| 31 | + |
| 32 | + |
| 33 | +class DynamoDBClientError(Exception): |
| 34 | + def __init__(self, message=None, code=None): |
| 35 | + self.message = message |
| 36 | + self.code = code |
| 37 | + |
| 38 | + |
| 39 | +class DynamoDBClient(object): |
| 40 | + """ |
| 41 | + Wrapper for boto3 library. |
| 42 | + """ |
| 43 | + |
| 44 | + def __init__(self, options: typing.Union[DynamoDBOptions, dict]): |
| 45 | + """Constructor of the DynamoDBClient. |
| 46 | +
|
| 47 | + Args: |
| 48 | + options (Union[DynamoDBOptions, dict]): Options to create a boto3 DynamoDB client. |
| 49 | + """ |
| 50 | + assert boto3 is not None, "Missing boto3 requirement" |
| 51 | + if isinstance(options, pipeline_options.PipelineOptions): |
| 52 | + options = options.view_as(DynamoDBOptions) |
| 53 | + access_key_id = options.aws_access_key_id |
| 54 | + secret_access_key = options.aws_secret_access_key |
| 55 | + session_token = options.aws_session_token |
| 56 | + endpoint_url = options.endpoint_url |
| 57 | + use_ssl = not options.disable_ssl |
| 58 | + region_name = options.region_name |
| 59 | + api_version = options.api_version |
| 60 | + verify = options.verify |
| 61 | + else: |
| 62 | + access_key_id = options.get("aws_access_key_id") |
| 63 | + secret_access_key = options.get("aws_secret_access_key") |
| 64 | + session_token = options.get("aws_session_token") |
| 65 | + endpoint_url = options.get("endpoint_url") |
| 66 | + use_ssl = not options.get("disable_ssl", False) |
| 67 | + region_name = options.get("region_name") |
| 68 | + api_version = options.get("api_version") |
| 69 | + verify = options.get("verify") |
| 70 | + |
| 71 | + session = boto3.session.Session() |
| 72 | + self.resource = session.resource( |
| 73 | + service_name="dynamodb", |
| 74 | + region_name=region_name, |
| 75 | + api_version=api_version, |
| 76 | + use_ssl=use_ssl, |
| 77 | + verify=verify, |
| 78 | + endpoint_url=endpoint_url, |
| 79 | + aws_access_key_id=access_key_id, |
| 80 | + aws_secret_access_key=secret_access_key, |
| 81 | + aws_session_token=session_token, |
| 82 | + ) |
| 83 | + |
| 84 | + def put_items_batch(self, records: list, table_name: str, dedup_pkeys: list = None): |
| 85 | + """Put records to an Amazon DynamoDB table using a batch writer object. |
| 86 | +
|
| 87 | + Args: |
| 88 | + records (list): Records to send into an Amazon SQS queue. |
| 89 | + table_name (str): Amazon DynamoDB table name. |
| 90 | + dedup_pkeys (list, Optional): List of keys to be used for de-duplicating items in buffer. |
| 91 | +
|
| 92 | + Raises: |
| 93 | + DynamoDBClientError: DynamoDB client error. |
| 94 | +
|
| 95 | + Returns: |
| 96 | + (Object): Boto3 response message. |
| 97 | + """ |
| 98 | + |
| 99 | + if not isinstance(records, list): |
| 100 | + raise DynamoDBClientError("Records should be a list.") |
| 101 | + try: |
| 102 | + table = self.resource.Table(table_name) |
| 103 | + with table.batch_writer(overwrite_by_pkeys=dedup_pkeys or []) as batch: |
| 104 | + for record in records: |
| 105 | + batch.put_item(Item=record) |
| 106 | + except Exception as e: |
| 107 | + raise DynamoDBClientError(str(e), get_http_error_code(e)) |
0 commit comments