-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate.py
54 lines (45 loc) · 1.54 KB
/
populate.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
import psycopg2
import random
from datetime import datetime, timedelta
conn_sharded_partitioned = psycopg2.connect(
host="localhost",
port="5433",
dbname="high_volume_sharded_partitioned_db",
user="user",
password="password"
)
conn_regular = psycopg2.connect(
host="localhost",
port="5434",
dbname="high_volume_regular_db",
user="user",
password="password"
)
def generate_random_activity_data():
activities = ['login', 'logout', 'purchase', 'view_item']
user_id = random.randint(1, 1000000)
activity = random.choice(activities)
start_date = datetime(2024, 1, 1)
end_date = datetime(2024, 3, 1)
delta = end_date - start_date
random_days = random.randint(0, delta.days)
activity_date = start_date + timedelta(days=random_days)
return user_id, activity, activity_date
cur_sharded_partitioned = conn_sharded_partitioned.cursor()
cur_regular = conn_regular.cursor()
for _ in range(100000):
user_id, activity, activity_date = generate_random_activity_data()
cur_sharded_partitioned.execute(
"INSERT INTO user_activity_sharded_partitioned (user_id, activity, activity_date) VALUES (%s, %s, %s)",
(user_id, activity, activity_date)
)
cur_regular.execute(
"INSERT INTO user_activity_regular (user_id, activity, activity_date) VALUES (%s, %s, %s)",
(user_id, activity, activity_date)
)
conn_sharded_partitioned.commit()
conn_regular.commit()
cur_sharded_partitioned.close()
cur_regular.close()
conn_sharded_partitioned.close()
conn_regular.close()