73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Script to generate fake telemetry data for Cassandra
|
|
Generates approximately 1GB of data
|
|
"""
|
|
|
|
from cassandra.cluster import Cluster
|
|
import random
|
|
from datetime import datetime, timedelta
|
|
import uuid
|
|
import sys
|
|
|
|
# Database connection details
|
|
DB_HOST = "10.0.21.51"
|
|
DB_PORT = "9042"
|
|
KEYSPACE = "telemetry"
|
|
TABLE_NAME = "device_metrics"
|
|
|
|
# Data generation settings
|
|
import os
|
|
TARGET_ROWS = int(os.getenv("GEN_ROWS", "5000")) # light, configurable via GEN_ROWS
|
|
BATCH_SIZE = min(5000, max(500, TARGET_ROWS))
|
|
|
|
# Sample data
|
|
METRIC_TYPES = ["temperature", "humidity", "pressure", "voltage", "current"]
|
|
DEVICE_PREFIX = "device-"
|
|
|
|
def generate_fake_device_metric():
|
|
"""Generate a single fake device metric"""
|
|
device_id = f"{DEVICE_PREFIX}{random.randint(1, 50000)}"
|
|
|
|
# Random timestamp within the last year
|
|
days_ago = random.randint(0, 365)
|
|
metric_ts = datetime.now() - timedelta(days=days_ago, hours=random.randint(0, 23),
|
|
minutes=random.randint(0, 59))
|
|
|
|
metric_type = random.choice(METRIC_TYPES)
|
|
metric_value = round(random.uniform(0.0, 100.0), 4)
|
|
|
|
# Generate a long payload field
|
|
payload = "X" * 200
|
|
|
|
return (device_id, metric_ts, metric_type, metric_value, payload)
|
|
|
|
def main():
|
|
print(f"Connecting to Cassandra at {DB_HOST}:{DB_PORT}...")
|
|
|
|
cluster = Cluster([DB_HOST], port=DB_PORT)
|
|
session = cluster.connect()
|
|
|
|
print(f"Generating {TARGET_ROWS} device metrics...")
|
|
print(f"Batch size: {BATCH_SIZE}")
|
|
|
|
insert_stmt = session.prepare(
|
|
f"INSERT INTO {KEYSPACE}.{TABLE_NAME} (device_id, metric_ts, metric_type, metric_value, payload) "
|
|
f"VALUES (?, ?, ?, ?, ?)"
|
|
)
|
|
|
|
total_generated = 0
|
|
for i in range(TARGET_ROWS):
|
|
session.execute(insert_stmt, generate_fake_device_metric())
|
|
total_generated += 1
|
|
if total_generated % 1000 == 0:
|
|
print(f"Generated {total_generated} rows...")
|
|
|
|
session.shutdown()
|
|
cluster.shutdown()
|
|
|
|
print(f"Completed! Generated {total_generated} device metrics.")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|