|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import argparse |
| 5 | +import boto3 |
| 6 | +import configparser |
| 7 | +import logging.config |
| 8 | +import os.path |
| 9 | +from botocore.exceptions import ClientError, ParamValidationError |
| 10 | +import datetime |
| 11 | +total_records_in_file = 0 |
| 12 | +total_inserted_records = 0 |
| 13 | +total_failed_records = 0 |
| 14 | + |
| 15 | + |
| 16 | +def increment_error_count(): |
| 17 | + global total_failed_records |
| 18 | + total_failed_records += 1 |
| 19 | + |
| 20 | + |
| 21 | +def load_data(input_file, dynamo_table): |
| 22 | + try: |
| 23 | + with open(input_file) as f: |
| 24 | + for line in f: |
| 25 | + global total_records_in_file |
| 26 | + total_records_in_file += 1 |
| 27 | + |
| 28 | + # get prepared item |
| 29 | + item = prepare_item(line) |
| 30 | + |
| 31 | + if item: |
| 32 | + insert_data_in_dynamodb(item, dynamo_table) |
| 33 | + else: |
| 34 | + logging.error("Skipped adding line due to error " + str(line)) |
| 35 | + increment_error_count() |
| 36 | + except Exception as e: |
| 37 | + print(e) |
| 38 | + |
| 39 | + |
| 40 | +def insert_data_in_dynamodb(item, table): |
| 41 | + try: |
| 42 | + logging.debug("inserting " + str(item)) |
| 43 | + table.put_item(Item=item) |
| 44 | + global total_inserted_records |
| 45 | + total_inserted_records += 1 |
| 46 | + if total_inserted_records % 100 == 0: |
| 47 | + print(".",end="") |
| 48 | + |
| 49 | + except ParamValidationError as e: |
| 50 | + increment_error_count() |
| 51 | + logging.error("Parameter validation error: %s" % e) |
| 52 | + |
| 53 | + except ClientError as e: |
| 54 | + increment_error_count() |
| 55 | + if e.response['Error']['Code'] == 'EntityAlreadyExists': |
| 56 | + logging.error("Password already exists in Database") |
| 57 | + else: |
| 58 | + logging.error(e.response['ResponseMetadata']['RequestId']) |
| 59 | + logging.error(e.response['Error']['Message']) |
| 60 | + |
| 61 | + |
| 62 | +def prepare_item(line): |
| 63 | + item = None |
| 64 | + try: |
| 65 | + attr_values = line.strip().split(':') |
| 66 | + pass_hash = attr_values[0] |
| 67 | + count = attr_values[1] |
| 68 | + item = { |
| 69 | + 'pwd_hash': pass_hash, |
| 70 | + 'count': parse_int(count) |
| 71 | + } |
| 72 | + except Exception as e: |
| 73 | + logging.error("Error occurred while parsing " + str(attr_values)) |
| 74 | + logging.error(e) |
| 75 | + return item |
| 76 | + |
| 77 | + |
| 78 | +def parse_int(value): |
| 79 | + try: |
| 80 | + return int(value) |
| 81 | + except ValueError: |
| 82 | + pass |
| 83 | + # in case int cannot be parsed 0 will be returned as count |
| 84 | + return 0 |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + parser = argparse.ArgumentParser(description="DynamoDb data loader") |
| 90 | + # parser.add_argument('-f', dest='input_file_path',default="10.million.10.txt", |
| 91 | + parser.add_argument('-f', dest='input_file_path',default="input-10.txt", |
| 92 | + help='please provide path of input file to load data') |
| 93 | + parser.add_argument('-c', dest='config_file_path', |
| 94 | + default='resources/config.properties', |
| 95 | + help='please provide path of config.properties file') |
| 96 | + parser.add_argument('-l', dest='log_config_file_path', |
| 97 | + default='resources/logging.properties', |
| 98 | + help='please provide path of logging.properties file') |
| 99 | + |
| 100 | + config = configparser.ConfigParser() |
| 101 | + inputs = parser.parse_args() |
| 102 | + |
| 103 | + if inputs.input_file_path is None: |
| 104 | + print("Please use -f to pass input file path") |
| 105 | + quit() |
| 106 | + |
| 107 | + if not os.path.isfile(inputs.config_file_path) or not os.path.isfile( |
| 108 | + inputs.log_config_file_path): |
| 109 | + print( |
| 110 | + "Please provide valid config file path for arg -c `config.properties` and arg -l 'logging.properties") |
| 111 | + quit() |
| 112 | + |
| 113 | + try: |
| 114 | + config.read([inputs.config_file_path]) |
| 115 | + logging.config.fileConfig(inputs.log_config_file_path) |
| 116 | + except Exception as ex: |
| 117 | + print("invalid path for configuration files") |
| 118 | + logging.error(ex) |
| 119 | + quit() |
| 120 | + |
| 121 | + endpoint = config['dynamodb']['endpoint_url'] |
| 122 | + region = config['dynamodb']['region'] |
| 123 | + table_name = config['dynamodb']['table_name'] |
| 124 | + |
| 125 | + dynamodb = boto3.resource('dynamodb', region_name=region, |
| 126 | + endpoint_url=endpoint) |
| 127 | + table = dynamodb.Table(table_name) |
| 128 | + start_time = datetime.datetime.now() |
| 129 | + load_data(inputs.input_file_path, table) |
| 130 | + |
| 131 | + end_time = datetime.datetime.now() |
| 132 | + time_to_upload = (end_time - start_time) |
| 133 | + logging.info( |
| 134 | + "***** Consolidated report of data insertion for the input file {}".format( |
| 135 | + inputs.input_file_path)) |
| 136 | + |
| 137 | + logging.info("Number of records {}".format(total_records_in_file)) |
| 138 | + logging.info( |
| 139 | + "Number of records inserted {}".format(total_inserted_records)) |
| 140 | + logging.info( |
| 141 | + "Number of records failed to insert {}".format(total_failed_records)) |
| 142 | + logging.info( "Time Started: {}".format(start_time)) |
| 143 | + logging.info( "Time Ended {}".format(end_time)) |
| 144 | + logging.info( "Time taken {}".format(time_to_upload)) |
| 145 | + os.rename(inputs.input_file_path, inputs.input_file_path + "-done" ) |
| 146 | + |
| 147 | + # logging.info( "Upload rate per second is {}".format(total_inserted_records//time_to_upload)) |
| 148 | + |
0 commit comments