Untitled

mail@pastecode.io avatar
unknown
python
5 months ago
902 B
1
Indexable
Never
import json
import boto3
import threading


def batch_write_items(items, table_name):
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(table_name)

    try:
        table.batch_write_item(RequestItems={table_name: items})
    except Exception as e:
        print(f'Error writing items to DynamoDB: {e}')


def read_file(file_path):
    with open(file_path, 'r') as f:
        items = []
        for line in f:
            items.append(json.loads(line))

    return items


def main():
    file_path = 'items.json'
    table_name = 'my_table'
    items = read_file(file_path)

    threads = []
    for i in range(10):
        thread = threading.Thread(target=batch_write_items, args=(items[i::10], table_name))
        threads.append(thread)

    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()


if __name__ == '__main__':
    main()