Skip to content

Instantly share code, notes, and snippets.

@anbento0490
Created July 19, 2021 18:40
Show Gist options
  • Save anbento0490/05f718cf4797a8d9536d00cb2e9704cb to your computer and use it in GitHub Desktop.
Save anbento0490/05f718cf4797a8d9536d00cb2e9704cb to your computer and use it in GitHub Desktop.
import boto3
import json
import random
import time
import uuid
from faker import Faker
from dataclasses import dataclass, field
s3_resource = boto3.resource('s3')
#s3_resource.create_bucket(Bucket='data-stream-dump',CreateBucketConfiguration={'LocationConstraint': 'eu-west-2'})
bucket_name = 'data-stream-dump'
faker = Faker()
currencies = ['USD', 'GBP', 'EUR']
@dataclass
class Transaction:
username: str = field(default_factory=faker.user_name)
currency: str = field(default_factory= lambda: currencies[random.randint(0,len(currencies)-1)])
amount: str = field(default_factory=lambda: random.randint(100, 200000))
def serialize(self):
return dict(
{
"username": self.username,
"currency": self.currency,
"amount": self.amount
}
)
def Producer(file_string):
filename = file_string
with open(filename, 'a') as file:
json.dump(Transaction().serialize(), file)
file.write(",\n")
s3_resource.Bucket(bucket_name).upload_file(Filename=filename, Key=filename)
for i in range(101):
Producer(f'transactions_{str(uuid.uuid4())}.json')
print('iteration:', i)
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment