Created
May 6, 2023 13:30
-
-
Save michalpulpan/7f555eb50df45643824ac11b05199daa to your computer and use it in GitHub Desktop.
Generate Books, Reviews, Loans and Users into Aerospike db
This file contains hidden or 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 random | |
| import string | |
| import datetime | |
| import aerospike | |
| from aerospike import exception as ex | |
| config = {"hosts": [("127.0.0.1", 3000)]} | |
| def random_string(length): | |
| return "".join(random.choices(string.ascii_letters + string.digits, k=length)) | |
| def generate_books(n): | |
| books = [] | |
| for i in range(n): | |
| book = { | |
| "book_id": i + 1, | |
| "title": random_string(10), | |
| "author": random_string(8), | |
| "pub_year": random.randint(1900, 2022), | |
| "page_count": random.randint(50, 1000), | |
| } | |
| books.append(book) | |
| return books | |
| def generate_users(n): | |
| users = [] | |
| for i in range(n): | |
| user = { | |
| "user_id": i + 1, | |
| "first_name": random_string(6), | |
| "last_name": random_string(8), | |
| "address": random_string(20), | |
| "email": random_string(6) + "@example.com", | |
| } | |
| users.append(user) | |
| return users | |
| def generate_loans(n, num_books, num_users): | |
| loans = [] | |
| for i in range(n): | |
| loan = { | |
| "loan_id": i + 1, | |
| "loan_date": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), | |
| "return_date": ( | |
| datetime.datetime.now() + datetime.timedelta(days=random.randint(7, 30)) | |
| ).strftime("%Y-%m-%d %H:%M:%S"), | |
| "notes": random_string(20), | |
| "book_id": random.randint(1, num_books), | |
| "user_id": random.randint(1, num_users), | |
| } | |
| loans.append(loan) | |
| return loans | |
| def generate_ratings(n, num_books, num_users): | |
| ratings = [] | |
| for i in range(n): | |
| rating = { | |
| "rating_id": i + 1, | |
| "rating": random.randint(1, 5), | |
| "notes": random_string(20), | |
| "book_id": random.randint(1, num_books), | |
| "user_id": random.randint(1, num_users), | |
| } | |
| ratings.append(rating) | |
| return ratings | |
| def insert_records(client, namespace, set_name, records, id_name="id"): | |
| for record in records: | |
| key = (namespace, set_name, str(record[id_name])) | |
| client.put(key, record) | |
| if __name__ == "__main__": | |
| num_books = 10 | |
| num_users = 5 | |
| num_loans = 20 | |
| num_ratings = 30 | |
| books = generate_books(num_books) | |
| users = generate_users(num_users) | |
| loans = generate_loans(num_loans, num_books, num_users) | |
| ratings = generate_ratings(num_ratings, num_books, num_users) | |
| try: | |
| client = aerospike.client(config).connect() | |
| insert_records(client, "test", "Books", books, "book_id") | |
| insert_records(client, "test", "Users", users, "user_id") | |
| insert_records(client, "test", "Loans", loans, "loan_id") | |
| insert_records(client, "test", "Ratings", ratings, "rating_id") | |
| except ex.AerospikeError as e: | |
| print("Error: {0} [{1}]".format(e.msg, e.code)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment