-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk.py
66 lines (54 loc) · 1.87 KB
/
bulk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import json
import random
import string
import psycopg2
# Adjust these values according to your requirement
NUM_USERS = 10000 # Number of users to generate
DATA_MULTIPLIER = 80 # Multiplier to increase the size of the JSON data
# Function to generate random data for a user
def generate_user_data():
user_id = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8))
name = ''.join(random.choices(string.ascii_letters, k=10))
age = random.randint(18, 60)
email = f"{name.lower()}@example.com"
# Add more fields as needed
return {
"id": user_id,
"name": name,
"age": age,
"email": email,
# Add more fields as needed
}
# Function to create a JSON file with multiple users
def create_json_file(filename):
users = [generate_user_data() for _ in range(NUM_USERS)]
data = {"data": users * DATA_MULTIPLIER}
with open(filename, 'w') as file:
json.dump(data, file)
def insert_large_record_into_table(record_data):
# Connect to the database
connection = psycopg2.connect(
dbname='account_onboarding_db',
user='postgres',
password='postgres',
host='ec2-65-1-98-7.ap-south-1.compute.amazonaws.com',
port='5888'
)
cursor = connection.cursor()
# Assuming your table has a column 'large_record' of type 'bytea'
query = f"INSERT INTO public.json_dump (large_record) VALUES (%s);"
cursor.execute(query, (record_data,))
print("Inserted")
connection.commit()
cursor.close()
connection.close()
def read_large_record(filename):
with open(filename, 'rb') as file:
return file.read()
if __name__ == "__main__":
json_filename = "large_json_data.json"
table_name = "users"
create_json_file(json_filename)
file = read_large_record(json_filename)
print("Adding into Table")
insert_large_record_into_table(file)