Created
July 22, 2023 22:24
-
-
Save PixelJunkie33/6961a19de911c8e49331d6f953932eec to your computer and use it in GitHub Desktop.
Generating Testing Data, Pushing the Mock Data to DynomoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import random | |
import uuid | |
from faker import Faker | |
fake = Faker() | |
# Replace with your AWS credentials | |
aws_access_key_id = 'YOUR_ACCESS_KEY' | |
aws_secret_access_key = 'YOUR_SECRET_KEY' | |
region_name = 'YOUR_REGION_NAME' # e.g., 'us-east-1' | |
# Replace with the names of your DynamoDB tables | |
table_name_users = 'User' | |
table_name_mypets = 'Mypet' | |
table_name_posts = 'Post' | |
def create_dynamodb_resource(): | |
return boto3.resource('dynamodb', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=region_name) | |
def create_fake_users(num_users): | |
users = [] | |
for _ in range(num_users): | |
user_data = { | |
'id': str(uuid.uuid4()), | |
'name': fake.name(), | |
'username': fake.user_name(), | |
} | |
users.append(user_data) | |
return users | |
def create_fake_pets(num_pets, num_users): | |
pets = [] | |
for _ in range(num_pets): | |
pet_data = { | |
'id': str(uuid.uuid4()), | |
'name': fake.first_name(), | |
'age': random.randint(1, 15), | |
'user': str(random.randint(1, num_users)), | |
} | |
pets.append(pet_data) | |
return pets | |
def create_fake_posts(num_posts, num_users): | |
posts = [] | |
for _ in range(num_posts): | |
post_data = { | |
'id': str(uuid.uuid4()), | |
'title': fake.sentence(nb_words=5), | |
'content': fake.paragraph(nb_sentences=3), | |
'user': str(random.randint(1, num_users)), | |
} | |
posts.append(post_data) | |
return posts | |
def push_data_to_dynamodb(table_name, data_list): | |
dynamodb = create_dynamodb_resource() | |
table = dynamodb.Table(table_name) | |
with table.batch_writer() as batch: | |
for item in data_list: | |
batch.put_item(Item=item) | |
def main(): | |
num_users = 20 | |
num_pets = 40 | |
num_posts = 80 | |
users_data = create_fake_users(num_users) | |
pets_data = create_fake_pets(num_pets, num_users) | |
posts_data = create_fake_posts(num_posts, num_users) | |
push_data_to_dynamodb(table_name_users, users_data) | |
push_data_to_dynamodb(table_name_mypets, pets_data) | |
push_data_to_dynamodb(table_name_posts, posts_data) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment