-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_data_to_snowflake.py
172 lines (152 loc) · 5.12 KB
/
fake_data_to_snowflake.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
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
import os
from faker import Faker
import pandas as pd
import uuid
import random
from datetime import datetime, timedelta
from logging_config import logger
from snowflake_loader import SnowflakeDataLoader
def generate_data(number_of_rows: int) -> pd.DataFrame:
"""
Generate fake data for testing purposes.
Args:
number_of_rows (int): The number of rows of fake data to generate.
Returns:
pandas.DataFrame: A DataFrame containing the generated fake data.
"""
# randmize the number of rows
number_of_rows += random.randint(-100, 100)
logger.info(f"Generating {number_of_rows} rows of fake data")
fake = Faker()
now = datetime.now()
data = {
"name": [fake.name() for _ in range(number_of_rows)],
"email": [fake.email() for _ in range(number_of_rows)],
"address": [fake.address() for _ in range(number_of_rows)],
"ordered_at_utc": [
now - timedelta(minutes=random.randint(0, 120))
for _ in range(number_of_rows)
],
"extracted_at_utc": [now for _ in range(number_of_rows)],
"sales_order_id": [str(uuid.uuid4()) for _ in range(number_of_rows)],
}
df = pd.DataFrame(data)
logger.info(f"Generated {number_of_rows} rows of fake data")
return df
def data_to_snowflake(
df: pd.DataFrame,
database: str,
schema: str,
user: str,
password: str,
account: str,
role: str,
warehouse: str,
rsa_key: str,
table: str = "fake_sales_orders",
) -> None:
"""
Load data from a pandas DataFrame to Snowflake using SnowpipeLoader or SnowflakeDfLoader.
Args:
df (pd.DataFrame): The pandas DataFrame containing the data to be loaded.
database (str, optional): The name of the Snowflake database. Defaults to the value of the `database` variable.
schema (str, optional): The name of the Snowflake schema. Defaults to the value of the `schema` variable.
user (str, optional): The Snowflake user name. Defaults to the value of the `user` variable.
password (str, optional): The Snowflake password. Defaults to the value of the `password` variable.
account (str, optional): The Snowflake account name. Defaults to the value of the `account` variable.
role (str, optional): The Snowflake role name. Defaults to the value of the `role` variable.
warehouse (str, optional): The Snowflake warehouse name. Defaults to the value of the `warehouse` variable.
table (str, optional): The name of the Snowflake table to load the data into. Defaults to "fake_sales_orders".
Returns:
None
Raises:
Exception: If an error occurs during the data loading process.
Notes:
This function first tries to load the data using the SnowpipeLoader, which does not require a warehouse.
If the SnowpipeLoader fails, it falls back to using the SnowflakeDfLoader, which uses a warehouse.
The function does not check if the table exists because the purpose is to avoid using a warehouse.
TODO: Instead of the current try-except block, use the "show tables" command to check if the table exists.
"""
loader = SnowflakeDataLoader(
df,
database=database,
schema=schema,
user=user,
password=password,
account=account,
role=role,
table=table,
rsa_key=rsa_key,
)
try:
loader.load_using_snowpipe()
except Exception as e:
logger.error(f"Error: {e}")
try:
loader.load_using_write_pandas(warehouse=warehouse)
except Exception as e:
logger.error(f"Error: {e}")
def main(
number_of_rows: int,
database: str,
schema: str,
user: str,
password: str,
account: str,
role: str,
warehouse: str,
rsa_key: str,
) -> None:
"""
Entry point of the script.
Args:
number_of_rows (int): The number of rows to generate in the fake data.
"""
df = generate_data(number_of_rows)
# upload_to_snowflake(df)
try:
data_to_snowflake(
df,
database=database,
schema=schema,
user=user,
password=password,
account=account,
role=role,
warehouse=warehouse,
table="fake_sales_orders",
rsa_key=rsa_key,
)
logger.info("Process complete")
except Exception as e:
logger.error(f"Error: {e}")
logger.error("Process failed")
raise e
if __name__ == "__main__":
# for testing locally
from config import (
user,
password,
account,
warehouse,
database,
schema,
role,
region,
secret_name,
)
from SecretsManager import SecretsManager
secrets_manager = SecretsManager(secret_name=secret_name, region=region)
secret = secrets_manager.get_secret()
rsa_key = os.getenv("rsa_key")
main(
number_of_rows=1000,
user=user,
password=password,
account=account,
warehouse=warehouse,
database=database,
schema=schema,
role=role,
rsa_key=rsa_key,
)