dynamodb_table_seeder.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. # Simple dynamodb seeder script
  9. #
  10. # Writes x random values into the example dynamodb in the example stack, with an id item<number>
  11. # to ensure table has values to experiment with
  12. import argparse
  13. import sys
  14. import logging
  15. import random
  16. import time
  17. from typing import List
  18. import boto3
  19. from boto3.dynamodb.table import TableResource
  20. from botocore.exceptions import ClientError
  21. logging.basicConfig(stream=sys.stdout, format="%(asctime)s %(process)d %(message)s")
  22. logger = logging.getLogger()
  23. logger.setLevel(logging.INFO)
  24. DEFAULT_ENTRIES = 10
  25. def _write_table_data(table: boto3.dynamodb.table.TableResource, table_data: List):
  26. """
  27. Write a list of items to a DynamoDB table using the batch_writer.
  28. Each item must contain at least the keys required by the schema, which for the example
  29. is just 'id'
  30. :param table: The table to fill
  31. :param table_data: The data to put in the table.
  32. """
  33. try:
  34. with table.batch_writer() as writer:
  35. for item in table_data:
  36. writer.put_item(Item=item)
  37. logger.info(f'Loaded data into table {table.name}')
  38. except ClientError:
  39. logger.exception(f'Failed to load data into table {table.name}')
  40. raise
  41. def generate_entries(table_name: str, num_entries: int, session: boto3.Session) -> None:
  42. """
  43. Populates a number of entries with simple random data
  44. :param table_name: The name of the table to write data into
  45. :param num_entries: The number of entries to create
  46. :param session: An existing boto3 session to use to create a dynamodb client from
  47. """
  48. dynamodb = session.resource('dynamodb')
  49. table = dynamodb.Table(table_name)
  50. if num_entries < 0:
  51. num_entries = DEFAULT_ENTRIES
  52. items = []
  53. for entry in range(0, num_entries):
  54. item = {
  55. 'id': f'Item{entry}',
  56. 'value': random.randrange(10000),
  57. 'timestamp': int(time.time())
  58. }
  59. logger.info(f'Generated {item}')
  60. items.append(item)
  61. _write_table_data(table, items)
  62. logger.info(f'Wrote {num_entries} values to {table_name}')
  63. if __name__ == "__main__":
  64. parser = argparse.ArgumentParser(description='Pre-populate a DynamoDB Table')
  65. parser.add_argument("--table_name", action="store", required=True)
  66. parser.add_argument("--region", action="store", dest='region', default="us-west-2")
  67. args = parser.parse_args()
  68. _session = boto3.Session(region_name=args.region)
  69. generate_entries(table_name=args.table_name, num_entries=10, session=_session)