|
| 1 | +import pandas as pd |
| 2 | +import duckdb |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import sys |
| 6 | +import numpy as np |
| 7 | +import logging |
| 8 | +from datetime import datetime, timedelta |
| 9 | + |
| 10 | + |
| 11 | +def generate_random_dataset(size=10): |
| 12 | + # Generate dates for the last 30 days |
| 13 | + end_date = datetime.now() |
| 14 | + start_date = end_date - timedelta(days=30) |
| 15 | + dates = pd.date_range(start=start_date, end=end_date, periods=size) |
| 16 | + |
| 17 | + data = { |
| 18 | + 'id': range(1, size + 1), |
| 19 | + 'date': dates, |
| 20 | + 'category': np.random.choice(['Low', 'Medium', 'High'], size), |
| 21 | + 'department': np.random.choice(['Sales', 'Marketing', 'Engineering', 'Support'], size), |
| 22 | + 'is_active': np.random.choice([True, False], size, p=[0.8, 0.2]), |
| 23 | + 'score': np.random.uniform(0, 100, size).round(2), |
| 24 | + } |
| 25 | + |
| 26 | + return pd.DataFrame(data) |
| 27 | + |
| 28 | + |
| 29 | +def execute(): |
| 30 | + logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
| 31 | + logger = logging.getLogger(__name__) |
| 32 | + |
| 33 | + parser = argparse.ArgumentParser(description='Generate and store random dataset in DuckDB') |
| 34 | + parser.add_argument('--db-path', type=str, required=True, help='Path to DuckDB database file') |
| 35 | + parser.add_argument( |
| 36 | + '--credential-config-path', type=str, required=True, help='Path string containing credential configuration' |
| 37 | + ) |
| 38 | + args = parser.parse_args() |
| 39 | + credential_file = args.credential_config_path |
| 40 | + |
| 41 | + if not credential_file.endswith('credentials.yml'): |
| 42 | + msg = "Credential config file must have 'credentials.yml' extension" |
| 43 | + # This is the output format expected by the pipeline.py which orchestrates the execution of this script |
| 44 | + print(json.dumps({"status": "error", "message": msg}), file=sys.stderr) |
| 45 | + raise ValueError("Credential config file must have 'credentials.yml' extension") |
| 46 | + |
| 47 | + try: |
| 48 | + df = generate_random_dataset() |
| 49 | + logger.info(f'DataFrame columns: {df.columns}') |
| 50 | + # Connect to DuckDB |
| 51 | + conn = duckdb.connect(args.db_path) |
| 52 | + |
| 53 | + # Create table with appropriate schema |
| 54 | + conn.execute( |
| 55 | + """ |
| 56 | + CREATE OR REPLACE TABLE random_data ( |
| 57 | + id INTEGER, |
| 58 | + date TIMESTAMP, |
| 59 | + category VARCHAR, |
| 60 | + department VARCHAR, |
| 61 | + is_active BOOLEAN, |
| 62 | + score DOUBLE |
| 63 | + ) |
| 64 | + """ |
| 65 | + ) |
| 66 | + |
| 67 | + conn.execute("INSERT INTO random_data SELECT * FROM df") |
| 68 | + conn.close() |
| 69 | + # This is the output format expected by the pipeline.py which orchestrates the execution of this script |
| 70 | + print(json.dumps({"status": "success", "message": "Data loaded successfully"})) |
| 71 | + |
| 72 | + except Exception as e: |
| 73 | + print(json.dumps({"status": "error", "message": str(e)}), file=sys.stderr) |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + execute() |
0 commit comments