-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_data.py
More file actions
474 lines (380 loc) · 15.1 KB
/
Copy pathgenerate_data.py
File metadata and controls
474 lines (380 loc) · 15.1 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
"""
StreamDW: Sample Data Generator
File: data/generate_data.py
Generates realistic sample data for the streaming platform data warehouse.
Inserts directly into MySQL and also saves CSVs as backup.
Usage:
python data/generate_data.py
Requirements:
pip install faker mysql-connector-python
"""
import os
import csv
import uuid
import random
from datetime import datetime, timedelta
from faker import Faker
import mysql.connector
# ============================================================
# CONFIGURATION
# ============================================================
DB_CONFIG = {
"host": "localhost",
"user": "root",
"password": "", # WampServer default = empty
"database": "stream_dw",
}
# Data volumes (small but complete)
NUM_USERS = 500
NUM_CONTENT = 200
NUM_SUBSCRIPTIONS = 2000 # multiple per user (upgrades, cancellations)
NUM_VIEWING_EVENTS = 50000
# Date range for the data
DATE_START = datetime(2023, 1, 1)
DATE_END = datetime(2025, 12, 31)
# Seed for reproducibility
SEED = 42
random.seed(SEED)
fake = Faker()
Faker.seed(SEED)
# ============================================================
# HELPER DATA
# ============================================================
COUNTRIES = [
"USA", "GBR", "CAN", "DEU", "FRA", "BRA", "JPN", "AUS",
"IND", "TUR", "MEX", "KOR", "ITA", "ESP", "NLD", "SWE",
"NOR", "POL", "ARG", "COL", "EGY", "SAU", "IDN", "THA",
]
PLAN_TYPES = ["free_trial", "basic", "standard", "premium"]
PLAN_PRICES = {
"free_trial": 0.00,
"basic": 7.99,
"standard": 13.99,
"premium": 19.99,
}
SUB_STATUSES = ["active", "cancelled", "expired", "paused"]
CONTENT_TYPES = ["movie", "series", "documentary", "short"]
GENRES = [
"Action", "Comedy", "Drama", "Horror", "Sci-Fi", "Romance",
"Thriller", "Documentary", "Animation", "Fantasy", "Crime",
"Mystery", "Adventure", "Family", "Music",
]
RATINGS = ["G", "PG", "PG-13", "R", "TV-MA", "TV-14", "TV-PG"]
DEVICE_TYPES = ["smart_tv", "mobile", "tablet", "desktop", "console"]
EVENT_TYPES = ["play", "pause", "resume", "complete", "abandon"]
# Content title templates for realism
TITLE_ADJECTIVES = [
"Dark", "Lost", "Silent", "Burning", "Frozen", "Hidden",
"Golden", "Broken", "Wild", "Crimson", "Midnight", "Electric",
"Savage", "Eternal", "Sacred", "Hollow", "Twisted", "Shattered",
"Fading", "Rising", "Distant", "Neon", "Velvet", "Iron",
]
TITLE_NOUNS = [
"Kingdom", "Signal", "Waters", "Forest", "Legacy", "Protocol",
"Horizon", "Echoes", "Shadows", "Circuit", "Requiem", "Empire",
"Passage", "Frontier", "Chronicle", "Harbor", "Junction", "Cipher",
"Vanguard", "Asylum", "Dominion", "Paradox", "Odyssey", "Verdict",
]
def generate_title():
"""Generate a realistic-sounding content title."""
pattern = random.choice(["adj_noun", "the_adj_noun", "noun_of_noun", "single"])
if pattern == "adj_noun":
return f"{random.choice(TITLE_ADJECTIVES)} {random.choice(TITLE_NOUNS)}"
elif pattern == "the_adj_noun":
return f"The {random.choice(TITLE_ADJECTIVES)} {random.choice(TITLE_NOUNS)}"
elif pattern == "noun_of_noun":
return f"{random.choice(TITLE_NOUNS)} of {random.choice(TITLE_NOUNS)}"
else:
return random.choice(TITLE_NOUNS)
def random_date(start, end):
"""Generate a random datetime between start and end."""
delta = end - start
random_days = random.randint(0, delta.days)
random_seconds = random.randint(0, 86399)
return start + timedelta(days=random_days, seconds=random_seconds)
# ============================================================
# DATA GENERATORS
# ============================================================
def generate_users():
"""Generate raw user records."""
print(f" Generating {NUM_USERS} users...")
users = []
used_emails = set()
for _ in range(NUM_USERS):
user_id = str(uuid.uuid4())
email = fake.email()
# Intentionally allow ~3% duplicate emails (dirty data)
if random.random() < 0.03 and used_emails:
email = random.choice(list(used_emails))
used_emails.add(email)
username = fake.user_name()
country = random.choice(COUNTRIES)
signup_date = random_date(DATE_START, DATE_END)
# birth_year: mostly valid, but ~5% are bad values
if random.random() < 0.05:
birth_year = random.choice([1800, 2030, 0, None, 1900])
else:
birth_year = random.randint(1960, 2005)
users.append((user_id, email, username, country, signup_date, birth_year))
return users
def generate_content():
"""Generate raw content catalog."""
print(f" Generating {NUM_CONTENT} content items...")
content = []
used_titles = set()
for _ in range(NUM_CONTENT):
content_id = str(uuid.uuid4())
# Ensure unique titles
title = generate_title()
while title in used_titles:
title = generate_title()
used_titles.add(title)
content_type = random.choice(CONTENT_TYPES)
genre = random.choice(GENRES)
release_year = random.randint(2015, 2025)
# Duration depends on content type
if content_type == "movie":
duration = random.randint(80, 180)
elif content_type == "series":
duration = random.randint(25, 60) # avg episode length
elif content_type == "documentary":
duration = random.randint(45, 120)
else: # short
duration = random.randint(5, 25)
rating = random.choice(RATINGS)
is_original = random.random() < 0.30 # 30% are originals
# Production cost: originals cost more
if is_original:
cost = round(random.uniform(500_000, 50_000_000), 2)
else:
cost = round(random.uniform(10_000, 2_000_000), 2)
content.append((
content_id, title, content_type, genre, release_year,
duration, rating, is_original, cost
))
return content
def generate_subscriptions(users):
"""Generate raw subscription records."""
print(f" Generating {NUM_SUBSCRIPTIONS} subscriptions...")
subs = []
user_ids = [u[0] for u in users]
user_signups = {u[0]: u[4] for u in users} # user_id -> signup_date
for _ in range(NUM_SUBSCRIPTIONS):
sub_id = str(uuid.uuid4())
user_id = random.choice(user_ids)
plan_type = random.choices(
PLAN_TYPES,
weights=[15, 35, 35, 15], # most are basic/standard
k=1
)[0]
# Start date: after user signup
signup = user_signups[user_id]
start_date = random_date(signup, DATE_END).date()
# Status distribution
status = random.choices(
SUB_STATUSES,
weights=[45, 25, 20, 10], # 45% active
k=1
)[0]
# End date: only if not active
if status == "active":
end_date = None
else:
days_active = random.randint(14, 365)
end_date = start_date + timedelta(days=days_active)
if end_date > DATE_END.date():
end_date = DATE_END.date()
price = PLAN_PRICES[plan_type]
subs.append((sub_id, user_id, plan_type, status, start_date, end_date, price))
return subs
def generate_viewing_events(users, content):
"""Generate raw viewing event records."""
print(f" Generating {NUM_VIEWING_EVENTS} viewing events...")
events = []
user_ids = [u[0] for u in users]
content_data = [(c[0], c[5]) for c in content] # (content_id, duration_minutes)
# Some users are heavy watchers, some are light
heavy_users = random.sample(user_ids, k=int(NUM_USERS * 0.2))
for _ in range(NUM_VIEWING_EVENTS):
event_id = str(uuid.uuid4())
# 60% of events come from heavy users (20% of users)
if random.random() < 0.6:
user_id = random.choice(heavy_users)
else:
user_id = random.choice(user_ids)
# Content selection: some content is more popular
content_id, content_duration = random.choice(content_data)
# Event type distribution
event_type = random.choices(
EVENT_TYPES,
weights=[40, 15, 15, 20, 10], # 40% play, 20% complete
k=1
)[0]
device_type = random.choices(
DEVICE_TYPES,
weights=[30, 30, 15, 15, 10], # TV and mobile dominant
k=1
)[0]
# Watch duration depends on event type
max_seconds = content_duration * 60
if event_type == "complete":
watch_sec = max_seconds
elif event_type == "abandon":
watch_sec = random.randint(30, int(max_seconds * 0.3))
elif event_type == "play":
watch_sec = random.randint(60, int(max_seconds * 0.8))
else: # pause, resume
watch_sec = random.randint(10, int(max_seconds * 0.5))
watch_sec = max(watch_sec, 1) # ensure positive
# Timestamp with realistic viewing patterns
event_ts = random_date(DATE_START, DATE_END)
# Skew toward evening hours (more realistic)
hour = event_ts.hour
if random.random() < 0.6:
# Push toward 18:00-23:59
new_hour = random.randint(18, 23)
event_ts = event_ts.replace(hour=new_hour)
events.append((
event_id, user_id, content_id, event_type,
device_type, watch_sec, event_ts
))
return events
# ============================================================
# DATABASE INSERT
# ============================================================
def insert_data(conn, users, content, subscriptions, events):
"""Insert all generated data into MySQL."""
cursor = conn.cursor()
print("\n[Inserting into MySQL]")
# Users
print(" Inserting users...")
cursor.executemany(
"""INSERT INTO raw_users
(user_id, email, username, country, signup_date, birth_year)
VALUES (%s, %s, %s, %s, %s, %s)""",
users
)
conn.commit()
print(f" ✓ {cursor.rowcount} users inserted")
# Content
print(" Inserting content...")
cursor.executemany(
"""INSERT INTO raw_content
(content_id, title, content_type, genre, release_year,
duration_minutes, rating, is_original, production_cost_usd)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)""",
content
)
conn.commit()
print(f" ✓ {cursor.rowcount} content items inserted")
# Subscriptions
print(" Inserting subscriptions...")
cursor.executemany(
"""INSERT INTO raw_subscriptions
(subscription_id, user_id, plan_type, status,
start_date, end_date, monthly_price)
VALUES (%s, %s, %s, %s, %s, %s, %s)""",
subscriptions
)
conn.commit()
print(f" ✓ {cursor.rowcount} subscriptions inserted")
# Viewing events (batch insert for performance)
print(" Inserting viewing events (this may take a moment)...")
BATCH_SIZE = 5000
for i in range(0, len(events), BATCH_SIZE):
batch = events[i:i + BATCH_SIZE]
cursor.executemany(
"""INSERT INTO raw_viewing_events
(event_id, user_id, content_id, event_type,
device_type, watch_duration_sec, event_timestamp)
VALUES (%s, %s, %s, %s, %s, %s, %s)""",
batch
)
conn.commit()
print(f" ... {min(i + BATCH_SIZE, len(events)):,}/{len(events):,}")
print(f" ✓ {len(events):,} viewing events inserted")
cursor.close()
# ============================================================
# CSV BACKUP
# ============================================================
def save_csvs(users, content, subscriptions, events):
"""Save generated data as CSV files for portability."""
csv_dir = os.path.join(os.path.dirname(__file__), "csv")
os.makedirs(csv_dir, exist_ok=True)
print("\n[Saving CSV backups]")
with open(os.path.join(csv_dir, "raw_users.csv"), "w", newline="", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(["user_id", "email", "username", "country", "signup_date", "birth_year"])
w.writerows(users)
print(f" ✓ csv/raw_users.csv")
with open(os.path.join(csv_dir, "raw_content.csv"), "w", newline="", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(["content_id", "title", "content_type", "genre", "release_year",
"duration_minutes", "rating", "is_original", "production_cost_usd"])
w.writerows(content)
print(f" ✓ csv/raw_content.csv")
with open(os.path.join(csv_dir, "raw_subscriptions.csv"), "w", newline="", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(["subscription_id", "user_id", "plan_type", "status",
"start_date", "end_date", "monthly_price"])
w.writerows(subscriptions)
print(f" ✓ csv/raw_subscriptions.csv")
with open(os.path.join(csv_dir, "raw_viewing_events.csv"), "w", newline="", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(["event_id", "user_id", "content_id", "event_type",
"device_type", "watch_duration_sec", "event_timestamp"])
w.writerows(events)
print(f" ✓ csv/raw_viewing_events.csv")
# ============================================================
# MAIN
# ============================================================
def main():
print("=" * 60)
print(" StreamDW — Sample Data Generator")
print("=" * 60)
# Generate data
print("\n[Generating data]")
users = generate_users()
content = generate_content()
subscriptions = generate_subscriptions(users)
events = generate_viewing_events(users, content)
# Connect to MySQL
print("\n[Connecting to MySQL]")
try:
conn = mysql.connector.connect(**DB_CONFIG)
print(f" ✓ Connected to {DB_CONFIG['database']}")
except mysql.connector.Error as err:
print(f" ✗ Connection failed: {err}")
print(" Check DB_CONFIG at the top of this script.")
print(" Make sure WampServer is running and stream_dw database exists.")
return
# Insert into MySQL
insert_data(conn, users, content, subscriptions, events)
# Save CSV backups
save_csvs(users, content, subscriptions, events)
# Summary
print("\n" + "=" * 60)
print(" ✓ DATA GENERATION COMPLETE")
print("=" * 60)
print(f" Users: {len(users):>10,}")
print(f" Content: {len(content):>10,}")
print(f" Subscriptions: {len(subscriptions):>10,}")
print(f" Viewing Events: {len(events):>10,}")
print(f" Total rows: {sum([len(users), len(content), len(subscriptions), len(events)]):>10,}")
print("=" * 60)
# Verify in MySQL
cursor = conn.cursor()
cursor.execute("""
SELECT TABLE_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'stream_dw'
ORDER BY TABLE_NAME
""")
print("\n MySQL row counts (approximate):")
for table, rows in cursor.fetchall():
print(f" {table:<30} {rows:>10,}")
cursor.close()
conn.close()
print("\n Done! Your staging layer is loaded. 🎬")
if __name__ == "__main__":
main()