Login_Sup Import Data
This commit is contained in:
@@ -11,6 +11,10 @@ ALTER TABLE DaburIndia_BI.Promotion
|
|||||||
MODIFY COLUMN store_id Int32
|
MODIFY COLUMN store_id Int32
|
||||||
COMMENT 'Unique store identifier';
|
COMMENT 'Unique store identifier';
|
||||||
|
|
||||||
|
ALTER TABLE DaburIndia_BI_New.Promotion
|
||||||
|
MODIFY COLUMN visit_date Date32
|
||||||
|
COMMENT 'Date of store visit';
|
||||||
|
|
||||||
ALTER TABLE DaburIndia_BI.Promotion
|
ALTER TABLE DaburIndia_BI.Promotion
|
||||||
MODIFY COLUMN employee_id Int32
|
MODIFY COLUMN employee_id Int32
|
||||||
COMMENT 'Employee unique identifier';
|
COMMENT 'Employee unique identifier';
|
||||||
@@ -104,3 +108,7 @@ ALTER TABLE DaburIndia_BI_New.Promotion
|
|||||||
MODIFY COLUMN stock Nullable(String)
|
MODIFY COLUMN stock Nullable(String)
|
||||||
COMMENT 'Stock availability status';
|
COMMENT 'Stock availability status';
|
||||||
|
|
||||||
|
ALTER TABLE DaburIndia_BI_New.Promotion
|
||||||
|
MODIFY COLUMN PromoQuestionId Nullable(Int32)
|
||||||
|
COMMENT 'Promotion question identifier';
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
ALTER TABLE DaburIndia_BI_New.SOS_OneApp
|
||||||
|
MODIFY COLUMN pk Int64
|
||||||
|
COMMENT 'Primary key identifier';
|
||||||
|
|
||||||
|
ALTER TABLE DaburIndia_BI_New.SOS_OneApp
|
||||||
|
MODIFY COLUMN project_id Int32
|
||||||
|
COMMENT 'Project unique identifier';
|
||||||
|
|
||||||
|
ALTER TABLE DaburIndia_BI_New.SOS_OneApp
|
||||||
|
MODIFY COLUMN MID Nullable(Int64)
|
||||||
|
COMMENT 'Unique master transaction ID (depends on combination of employee_id, store_id, and visit_date)';
|
||||||
|
|
||||||
|
ALTER TABLE DaburIndia_BI_New.SOS_OneApp
|
||||||
|
MODIFY COLUMN store_id Int32
|
||||||
|
COMMENT 'Unique store identifier';
|
||||||
|
|
||||||
|
ALTER TABLE DaburIndia_BI_New.SOS_OneApp
|
||||||
|
MODIFY COLUMN visit_date Date32
|
||||||
|
COMMENT 'Date of store visit';
|
||||||
|
|
||||||
|
ALTER TABLE DaburIndia_BI_New.SOS_OneApp
|
||||||
|
MODIFY COLUMN storetype_id Nullable(Int32)
|
||||||
|
COMMENT 'Store type identifier';
|
||||||
|
|
||||||
@@ -0,0 +1,722 @@
|
|||||||
|
import pyodbc
|
||||||
|
import pandas as pd
|
||||||
|
import clickhouse_connect
|
||||||
|
import numpy as np
|
||||||
|
from datetime import datetime
|
||||||
|
import traceback
|
||||||
|
import warnings
|
||||||
|
import time
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# IGNORE WARNINGS
|
||||||
|
# =========================================================
|
||||||
|
warnings.filterwarnings(
|
||||||
|
'ignore',
|
||||||
|
'pandas only supports SQLAlchemy connectable'
|
||||||
|
)
|
||||||
|
|
||||||
|
print("\n================================================")
|
||||||
|
print("ETL STARTED :", datetime.now())
|
||||||
|
print("================================================")
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# SQL SERVER CONNECTION STRING
|
||||||
|
# =========================================================
|
||||||
|
SQL_CONN_STR = (
|
||||||
|
'DRIVER={ODBC Driver 17 for SQL Server};'
|
||||||
|
'SERVER=10.200.25.65;'
|
||||||
|
'DATABASE=CPMIndiaBusinessInsight;'
|
||||||
|
'UID=bsgteam_test;'
|
||||||
|
'PWD=B$gt3@m#00512;'
|
||||||
|
'TrustServerCertificate=yes;'
|
||||||
|
'Connection Timeout=60;'
|
||||||
|
)
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# CLICKHOUSE CONFIG
|
||||||
|
# =========================================================
|
||||||
|
CH_CONFIG = {
|
||||||
|
'host': '172.188.12.194',
|
||||||
|
'port': 8123,
|
||||||
|
'username': 'default',
|
||||||
|
'password': 'dipanshu_k',
|
||||||
|
'database': 'DaburIndia_BI'
|
||||||
|
}
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# TABLE DETAILS
|
||||||
|
# =========================================================
|
||||||
|
TABLE_NAME = 'Login_Sup'
|
||||||
|
PROJECTId = 41654
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# SETTINGS
|
||||||
|
# =========================================================
|
||||||
|
TRUNCATE_BEFORE_LOAD = True
|
||||||
|
table_truncated = False
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# CHUNK SIZE
|
||||||
|
# =========================================================
|
||||||
|
chunk_size = 25000
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# CONNECT SQL SERVER
|
||||||
|
# =========================================================
|
||||||
|
def connect_sql():
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
conn = pyodbc.connect(
|
||||||
|
SQL_CONN_STR,
|
||||||
|
autocommit=True
|
||||||
|
)
|
||||||
|
|
||||||
|
print("Connected SQL Server")
|
||||||
|
|
||||||
|
return conn
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
print("SQL CONNECTION FAILED")
|
||||||
|
print(str(e))
|
||||||
|
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# CONNECT CLICKHOUSE
|
||||||
|
# =========================================================
|
||||||
|
def connect_clickhouse():
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
client = clickhouse_connect.get_client(
|
||||||
|
**CH_CONFIG
|
||||||
|
)
|
||||||
|
|
||||||
|
print("Connected ClickHouse")
|
||||||
|
|
||||||
|
return client
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
print("CLICKHOUSE CONNECTION FAILED")
|
||||||
|
print(str(e))
|
||||||
|
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# CLEAN DATAFRAME
|
||||||
|
# =========================================================
|
||||||
|
def clean_dataframe(df):
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
# =============================================
|
||||||
|
# Replace NaN
|
||||||
|
# =============================================
|
||||||
|
df = df.replace({np.nan: None})
|
||||||
|
|
||||||
|
# =============================================
|
||||||
|
# Process Columns
|
||||||
|
# =============================================
|
||||||
|
for col in df.columns:
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
print(f"\nCleaning Column : {col}")
|
||||||
|
|
||||||
|
col_lower = col.lower()
|
||||||
|
|
||||||
|
# =====================================
|
||||||
|
# DATE COLUMNS
|
||||||
|
# =====================================
|
||||||
|
if (
|
||||||
|
'date' in col_lower
|
||||||
|
and
|
||||||
|
'time' not in col_lower
|
||||||
|
):
|
||||||
|
|
||||||
|
print(f"Date Column : {col}")
|
||||||
|
|
||||||
|
df[col] = pd.to_datetime(
|
||||||
|
df[col],
|
||||||
|
errors='coerce'
|
||||||
|
).dt.date
|
||||||
|
|
||||||
|
cleaned_dates = []
|
||||||
|
|
||||||
|
for val in df[col]:
|
||||||
|
|
||||||
|
if pd.isnull(val):
|
||||||
|
|
||||||
|
cleaned_dates.append(None)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
cleaned_dates.append(val)
|
||||||
|
|
||||||
|
df[col] = cleaned_dates
|
||||||
|
|
||||||
|
# =====================================
|
||||||
|
# TIME COLUMNS
|
||||||
|
# =====================================
|
||||||
|
elif 'time' in col_lower:
|
||||||
|
|
||||||
|
print(f"Time Column : {col}")
|
||||||
|
|
||||||
|
cleaned_times = []
|
||||||
|
|
||||||
|
for val in df[col]:
|
||||||
|
|
||||||
|
if pd.isnull(val):
|
||||||
|
|
||||||
|
cleaned_times.append(None)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
# KEEP AS STRING
|
||||||
|
cleaned_times.append(
|
||||||
|
str(val).strip()
|
||||||
|
)
|
||||||
|
|
||||||
|
df[col] = cleaned_times
|
||||||
|
|
||||||
|
# =====================================
|
||||||
|
# DATETIME COLUMNS
|
||||||
|
# =====================================
|
||||||
|
elif any(
|
||||||
|
x in col_lower
|
||||||
|
for x in [
|
||||||
|
'createdon',
|
||||||
|
'modifiedon',
|
||||||
|
'datetime',
|
||||||
|
'timestamp'
|
||||||
|
]
|
||||||
|
):
|
||||||
|
|
||||||
|
print(f"Datetime Column : {col}")
|
||||||
|
|
||||||
|
df[col] = pd.to_datetime(
|
||||||
|
df[col],
|
||||||
|
errors='coerce'
|
||||||
|
)
|
||||||
|
|
||||||
|
cleaned_datetime = []
|
||||||
|
|
||||||
|
for val in df[col]:
|
||||||
|
|
||||||
|
if pd.isnull(val):
|
||||||
|
|
||||||
|
cleaned_datetime.append(None)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
cleaned_datetime.append(
|
||||||
|
val.to_pydatetime()
|
||||||
|
)
|
||||||
|
|
||||||
|
df[col] = cleaned_datetime
|
||||||
|
|
||||||
|
# =====================================
|
||||||
|
# INTEGER COLUMNS
|
||||||
|
# =====================================
|
||||||
|
elif pd.api.types.is_integer_dtype(df[col]):
|
||||||
|
|
||||||
|
print(f"Integer Column : {col}")
|
||||||
|
|
||||||
|
df[col] = pd.to_numeric(
|
||||||
|
df[col],
|
||||||
|
errors='coerce'
|
||||||
|
)
|
||||||
|
|
||||||
|
df[col] = df[col].apply(
|
||||||
|
lambda x:
|
||||||
|
int(x)
|
||||||
|
if pd.notnull(x)
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
|
||||||
|
# =====================================
|
||||||
|
# FLOAT COLUMNS
|
||||||
|
# =====================================
|
||||||
|
elif pd.api.types.is_float_dtype(df[col]):
|
||||||
|
|
||||||
|
print(f"Float Column : {col}")
|
||||||
|
|
||||||
|
df[col] = pd.to_numeric(
|
||||||
|
df[col],
|
||||||
|
errors='coerce'
|
||||||
|
)
|
||||||
|
|
||||||
|
df[col] = df[col].apply(
|
||||||
|
lambda x:
|
||||||
|
float(x)
|
||||||
|
if pd.notnull(x)
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
|
||||||
|
# =====================================
|
||||||
|
# OBJECT / STRING COLUMNS
|
||||||
|
# =====================================
|
||||||
|
else:
|
||||||
|
|
||||||
|
print(f"String/Object Column : {col}")
|
||||||
|
|
||||||
|
cleaned = []
|
||||||
|
|
||||||
|
for val in df[col]:
|
||||||
|
|
||||||
|
# NULL
|
||||||
|
if pd.isnull(val):
|
||||||
|
|
||||||
|
cleaned.append(None)
|
||||||
|
|
||||||
|
# DATETIME
|
||||||
|
elif isinstance(
|
||||||
|
val,
|
||||||
|
(
|
||||||
|
datetime,
|
||||||
|
pd.Timestamp
|
||||||
|
)
|
||||||
|
):
|
||||||
|
|
||||||
|
if isinstance(
|
||||||
|
val,
|
||||||
|
pd.Timestamp
|
||||||
|
):
|
||||||
|
|
||||||
|
cleaned.append(
|
||||||
|
val.to_pydatetime()
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
cleaned.append(val)
|
||||||
|
|
||||||
|
# INTEGER
|
||||||
|
elif isinstance(
|
||||||
|
val,
|
||||||
|
(
|
||||||
|
int,
|
||||||
|
np.integer
|
||||||
|
)
|
||||||
|
):
|
||||||
|
|
||||||
|
cleaned.append(
|
||||||
|
int(val)
|
||||||
|
)
|
||||||
|
|
||||||
|
# FLOAT
|
||||||
|
elif isinstance(
|
||||||
|
val,
|
||||||
|
(
|
||||||
|
float,
|
||||||
|
np.floating
|
||||||
|
)
|
||||||
|
):
|
||||||
|
|
||||||
|
if np.isnan(val):
|
||||||
|
|
||||||
|
cleaned.append(None)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
cleaned.append(
|
||||||
|
float(val)
|
||||||
|
)
|
||||||
|
|
||||||
|
# BOOLEAN
|
||||||
|
elif isinstance(
|
||||||
|
val,
|
||||||
|
bool
|
||||||
|
):
|
||||||
|
|
||||||
|
cleaned.append(
|
||||||
|
int(val)
|
||||||
|
)
|
||||||
|
|
||||||
|
# BYTES
|
||||||
|
elif isinstance(
|
||||||
|
val,
|
||||||
|
(
|
||||||
|
bytes,
|
||||||
|
bytearray
|
||||||
|
)
|
||||||
|
):
|
||||||
|
|
||||||
|
cleaned.append(
|
||||||
|
val.hex()
|
||||||
|
)
|
||||||
|
|
||||||
|
# STRING
|
||||||
|
else:
|
||||||
|
|
||||||
|
cleaned.append(
|
||||||
|
str(val).strip()
|
||||||
|
)
|
||||||
|
|
||||||
|
df[col] = cleaned
|
||||||
|
|
||||||
|
except Exception as col_error:
|
||||||
|
|
||||||
|
print("\n================================")
|
||||||
|
print(f"COLUMN FAILED : {col}")
|
||||||
|
print(str(col_error))
|
||||||
|
print(traceback.format_exc())
|
||||||
|
print("================================")
|
||||||
|
|
||||||
|
return df
|
||||||
|
|
||||||
|
except Exception as clean_error:
|
||||||
|
|
||||||
|
print("\n================================")
|
||||||
|
print("DATA CLEAN FAILED")
|
||||||
|
print(str(clean_error))
|
||||||
|
print(traceback.format_exc())
|
||||||
|
print("================================")
|
||||||
|
|
||||||
|
return df
|
||||||
|
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# MAIN PROCESS
|
||||||
|
# =========================================================
|
||||||
|
try:
|
||||||
|
|
||||||
|
# =====================================================
|
||||||
|
# CONNECT SQL SERVER
|
||||||
|
# =====================================================
|
||||||
|
sql_conn = connect_sql()
|
||||||
|
|
||||||
|
# =====================================================
|
||||||
|
# CONNECT CLICKHOUSE
|
||||||
|
# =====================================================
|
||||||
|
ch_client = connect_clickhouse()
|
||||||
|
|
||||||
|
# =====================================================
|
||||||
|
# QUERY
|
||||||
|
# =====================================================
|
||||||
|
query = f"""
|
||||||
|
SELECT *
|
||||||
|
FROM dbo.[{TABLE_NAME}]
|
||||||
|
WHERE PROJECTId = {PROJECTId}
|
||||||
|
"""
|
||||||
|
|
||||||
|
print("\nExecuting Query")
|
||||||
|
print(query)
|
||||||
|
|
||||||
|
# =====================================================
|
||||||
|
# RETRY SETTINGS
|
||||||
|
# =====================================================
|
||||||
|
retry_count = 0
|
||||||
|
max_retry = 5
|
||||||
|
|
||||||
|
# =====================================================
|
||||||
|
# MAIN RETRY LOOP
|
||||||
|
# =====================================================
|
||||||
|
while retry_count < max_retry:
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
# =============================================
|
||||||
|
# READ SQL DATA
|
||||||
|
# =============================================
|
||||||
|
for chunk in pd.read_sql(
|
||||||
|
query,
|
||||||
|
sql_conn,
|
||||||
|
chunksize=chunk_size
|
||||||
|
):
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
print("\n================================")
|
||||||
|
print(
|
||||||
|
f"Processing Rows : "
|
||||||
|
f"{len(chunk)}"
|
||||||
|
)
|
||||||
|
print("================================")
|
||||||
|
|
||||||
|
# =====================================
|
||||||
|
# CLEAN DATA
|
||||||
|
# =====================================
|
||||||
|
chunk = clean_dataframe(chunk)
|
||||||
|
|
||||||
|
# =====================================
|
||||||
|
# DEBUG DATATYPES
|
||||||
|
# =====================================
|
||||||
|
print("\nCOLUMN DATATYPES")
|
||||||
|
print(chunk.dtypes)
|
||||||
|
|
||||||
|
print("\nCOLUMN SAMPLE TYPES")
|
||||||
|
|
||||||
|
for col in chunk.columns:
|
||||||
|
|
||||||
|
sample = chunk[col].dropna()
|
||||||
|
|
||||||
|
if len(sample) > 0:
|
||||||
|
|
||||||
|
print(
|
||||||
|
col,
|
||||||
|
type(sample.iloc[0]),
|
||||||
|
sample.iloc[0]
|
||||||
|
)
|
||||||
|
|
||||||
|
# =====================================
|
||||||
|
# DEBUG TIME COLUMN
|
||||||
|
# =====================================
|
||||||
|
if 'logintime' in chunk.columns:
|
||||||
|
|
||||||
|
print("\nLOGINTIME SAMPLE")
|
||||||
|
|
||||||
|
print(
|
||||||
|
chunk['logintime']
|
||||||
|
.head(5)
|
||||||
|
)
|
||||||
|
|
||||||
|
print(
|
||||||
|
chunk['logintime']
|
||||||
|
.apply(type)
|
||||||
|
.value_counts()
|
||||||
|
)
|
||||||
|
|
||||||
|
# =====================================
|
||||||
|
# TRUNCATE TABLE FIRST TIME ONLY
|
||||||
|
# =====================================
|
||||||
|
if (
|
||||||
|
TRUNCATE_BEFORE_LOAD
|
||||||
|
and
|
||||||
|
not table_truncated
|
||||||
|
):
|
||||||
|
|
||||||
|
print("\n================================")
|
||||||
|
print(
|
||||||
|
f"TRUNCATING TABLE : "
|
||||||
|
f"{TABLE_NAME}"
|
||||||
|
)
|
||||||
|
print("================================")
|
||||||
|
|
||||||
|
truncate_query = f"""
|
||||||
|
TRUNCATE TABLE
|
||||||
|
`{CH_CONFIG['database']}`.`{TABLE_NAME}`
|
||||||
|
"""
|
||||||
|
|
||||||
|
ch_client.command(
|
||||||
|
truncate_query
|
||||||
|
)
|
||||||
|
|
||||||
|
print(
|
||||||
|
"TABLE TRUNCATED SUCCESSFULLY"
|
||||||
|
)
|
||||||
|
|
||||||
|
table_truncated = True
|
||||||
|
|
||||||
|
# =====================================
|
||||||
|
# INSERT INTO CLICKHOUSE
|
||||||
|
# =====================================
|
||||||
|
print(
|
||||||
|
"\nINSERTING INTO CLICKHOUSE..."
|
||||||
|
)
|
||||||
|
|
||||||
|
ch_client.insert_df(
|
||||||
|
table=TABLE_NAME,
|
||||||
|
df=chunk,
|
||||||
|
database=CH_CONFIG['database']
|
||||||
|
)
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"INSERTED : "
|
||||||
|
f"{len(chunk)} ROWS"
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as insert_error:
|
||||||
|
|
||||||
|
print("\n================================")
|
||||||
|
print("INSERT FAILED")
|
||||||
|
print("================================")
|
||||||
|
|
||||||
|
print(str(insert_error))
|
||||||
|
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
# =================================
|
||||||
|
# SAVE BAD CHUNK
|
||||||
|
# =================================
|
||||||
|
try:
|
||||||
|
|
||||||
|
error_file = (
|
||||||
|
f"error_chunk_"
|
||||||
|
f"{TABLE_NAME}.csv"
|
||||||
|
)
|
||||||
|
|
||||||
|
chunk.to_csv(
|
||||||
|
error_file,
|
||||||
|
index=False
|
||||||
|
)
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"BAD CHUNK SAVED : "
|
||||||
|
f"{error_file}"
|
||||||
|
)
|
||||||
|
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# =================================
|
||||||
|
# SAVE LOG
|
||||||
|
# =================================
|
||||||
|
with open(
|
||||||
|
"insert_error.log",
|
||||||
|
"a",
|
||||||
|
encoding="utf-8"
|
||||||
|
) as log:
|
||||||
|
|
||||||
|
log.write(
|
||||||
|
"\n\n================================"
|
||||||
|
)
|
||||||
|
|
||||||
|
log.write(
|
||||||
|
f"\nTIME : "
|
||||||
|
f"{datetime.now()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
log.write(
|
||||||
|
f"\nTABLE : "
|
||||||
|
f"{TABLE_NAME}"
|
||||||
|
)
|
||||||
|
|
||||||
|
log.write(
|
||||||
|
f"\nERROR : "
|
||||||
|
f"{str(insert_error)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
log.write(
|
||||||
|
f"\nTRACEBACK :\n"
|
||||||
|
f"{traceback.format_exc()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
log.write(
|
||||||
|
"\n================================"
|
||||||
|
)
|
||||||
|
|
||||||
|
continue
|
||||||
|
|
||||||
|
# =============================================
|
||||||
|
# SUCCESS
|
||||||
|
# =============================================
|
||||||
|
break
|
||||||
|
|
||||||
|
# =================================================
|
||||||
|
# SQL CONNECTION FAILURE
|
||||||
|
# =================================================
|
||||||
|
except pyodbc.OperationalError as op_error:
|
||||||
|
|
||||||
|
retry_count += 1
|
||||||
|
|
||||||
|
print("\n================================")
|
||||||
|
print(
|
||||||
|
f"SQL CONNECTION LOST "
|
||||||
|
f"- RETRY {retry_count}"
|
||||||
|
)
|
||||||
|
print("================================")
|
||||||
|
|
||||||
|
print(str(op_error))
|
||||||
|
|
||||||
|
time.sleep(10)
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
sql_conn.close()
|
||||||
|
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
sql_conn = connect_sql()
|
||||||
|
|
||||||
|
# =================================================
|
||||||
|
# OTHER ERROR
|
||||||
|
# =================================================
|
||||||
|
except Exception as loop_error:
|
||||||
|
|
||||||
|
print("\n================================")
|
||||||
|
print("MAIN LOOP ERROR")
|
||||||
|
print("================================")
|
||||||
|
|
||||||
|
print(str(loop_error))
|
||||||
|
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
break
|
||||||
|
|
||||||
|
print("\n================================")
|
||||||
|
print("ETL COMPLETED SUCCESSFULLY")
|
||||||
|
print("================================")
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# MAIN ERROR
|
||||||
|
# =========================================================
|
||||||
|
except Exception as main_error:
|
||||||
|
|
||||||
|
print("\n================================")
|
||||||
|
print("MAIN ERROR")
|
||||||
|
print("================================")
|
||||||
|
|
||||||
|
print(str(main_error))
|
||||||
|
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
with open(
|
||||||
|
"main_error.log",
|
||||||
|
"a",
|
||||||
|
encoding="utf-8"
|
||||||
|
) as log:
|
||||||
|
|
||||||
|
log.write(
|
||||||
|
"\n\n================================"
|
||||||
|
)
|
||||||
|
|
||||||
|
log.write(
|
||||||
|
f"\nTIME : {datetime.now()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
log.write(
|
||||||
|
f"\nERROR : {str(main_error)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
log.write(
|
||||||
|
f"\nTRACEBACK :\n"
|
||||||
|
f"{traceback.format_exc()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
log.write(
|
||||||
|
"\n================================"
|
||||||
|
)
|
||||||
|
|
||||||
|
# =========================================================
|
||||||
|
# CLOSE CONNECTIONS
|
||||||
|
# =========================================================
|
||||||
|
finally:
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
sql_conn.close()
|
||||||
|
|
||||||
|
print("\nSQL SERVER CONNECTION CLOSED")
|
||||||
|
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
ch_client.close()
|
||||||
|
|
||||||
|
print("CLICKHOUSE CONNECTION CLOSED")
|
||||||
|
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
print("\n================================================")
|
||||||
|
print("ETL FINISHED :", datetime.now())
|
||||||
|
print("================================================")
|
||||||
@@ -17419,4 +17419,169 @@ Traceback (most recent call last):
|
|||||||
~~^~~~~
|
~~^~~~~
|
||||||
TypeError: unsupported operand type(s) for -: 'str' and 'datetime.date'
|
TypeError: unsupported operand type(s) for -: 'str' and 'datetime.date'
|
||||||
|
|
||||||
|
================================
|
||||||
|
|
||||||
|
================================
|
||||||
|
TIME : 2026-05-26 15:51:00.931896
|
||||||
|
TABLE : Login_Sup
|
||||||
|
ERROR : Invalid None value in non-Nullable column, column name: `logintime`
|
||||||
|
TRACEBACK :
|
||||||
|
Traceback (most recent call last):
|
||||||
|
File "d:\Python Code\Login_Sup_Import.py", line 427, in <module>
|
||||||
|
ch_client.insert_df(
|
||||||
|
~~~~~~~~~~~~~~~~~~~^
|
||||||
|
table=TABLE_NAME,
|
||||||
|
^^^^^^^^^^^^^^^^^
|
||||||
|
df=chunk,
|
||||||
|
^^^^^^^^^
|
||||||
|
database=CH_CONFIG['database']
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
)
|
||||||
|
^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\client.py", line 1013, in insert_df
|
||||||
|
return self.insert(table,
|
||||||
|
~~~~~~~~~~~^^^^^^^
|
||||||
|
df,
|
||||||
|
^^^
|
||||||
|
...<5 lines>...
|
||||||
|
transport_settings=transport_settings,
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
context=context)
|
||||||
|
^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\client.py", line 978, in insert
|
||||||
|
return self.data_insert(context)
|
||||||
|
~~~~~~~~~~~~~~~~^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\httpclient.py", line 347, in data_insert
|
||||||
|
response = self._raw_request(block_gen, params, headers, error_handler=error_handler, server_wait=False)
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\httpclient.py", line 569, in _raw_request
|
||||||
|
error_handler(response)
|
||||||
|
~~~~~~~~~~~~~^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\httpclient.py", line 331, in error_handler
|
||||||
|
raise ex
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\transform.py", line 114, in chunk_gen
|
||||||
|
col_type.write_column(data, output, context)
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\datatypes\base.py", line 216, in write_column
|
||||||
|
self.write_column_data(column, dest, ctx)
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\datatypes\base.py", line 231, in write_column_data
|
||||||
|
self._write_column_binary(column, dest, ctx)
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\datatypes\string.py", line 51, in _write_column_binary
|
||||||
|
handle_error(data_conv.write_str_col(column, self.nullable, encoding, dest), ctx)
|
||||||
|
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\errors.py", line 17, in handle_error
|
||||||
|
raise DataError(msg)
|
||||||
|
clickhouse_connect.driver.exceptions.DataError: Invalid None value in non-Nullable column, column name: `logintime`
|
||||||
|
|
||||||
|
================================
|
||||||
|
|
||||||
|
================================
|
||||||
|
TIME : 2026-05-26 15:54:06.037126
|
||||||
|
TABLE : Login_Sup
|
||||||
|
ERROR : Invalid None value in non-Nullable column, column name: `UpdateBy`
|
||||||
|
TRACEBACK :
|
||||||
|
Traceback (most recent call last):
|
||||||
|
File "d:\Python Code\Login_Sup_Import.py", line 427, in <module>
|
||||||
|
ch_client.insert_df(
|
||||||
|
~~~~~~~~~~~~~~~~~~~^
|
||||||
|
table=TABLE_NAME,
|
||||||
|
^^^^^^^^^^^^^^^^^
|
||||||
|
df=chunk,
|
||||||
|
^^^^^^^^^
|
||||||
|
database=CH_CONFIG['database']
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
)
|
||||||
|
^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\client.py", line 1013, in insert_df
|
||||||
|
return self.insert(table,
|
||||||
|
~~~~~~~~~~~^^^^^^^
|
||||||
|
df,
|
||||||
|
^^^
|
||||||
|
...<5 lines>...
|
||||||
|
transport_settings=transport_settings,
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
context=context)
|
||||||
|
^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\client.py", line 978, in insert
|
||||||
|
return self.data_insert(context)
|
||||||
|
~~~~~~~~~~~~~~~~^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\httpclient.py", line 347, in data_insert
|
||||||
|
response = self._raw_request(block_gen, params, headers, error_handler=error_handler, server_wait=False)
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\httpclient.py", line 569, in _raw_request
|
||||||
|
error_handler(response)
|
||||||
|
~~~~~~~~~~~~~^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\httpclient.py", line 331, in error_handler
|
||||||
|
raise ex
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\transform.py", line 114, in chunk_gen
|
||||||
|
col_type.write_column(data, output, context)
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\datatypes\base.py", line 216, in write_column
|
||||||
|
self.write_column_data(column, dest, ctx)
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\datatypes\base.py", line 231, in write_column_data
|
||||||
|
self._write_column_binary(column, dest, ctx)
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\datatypes\string.py", line 51, in _write_column_binary
|
||||||
|
handle_error(data_conv.write_str_col(column, self.nullable, encoding, dest), ctx)
|
||||||
|
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\errors.py", line 17, in handle_error
|
||||||
|
raise DataError(msg)
|
||||||
|
clickhouse_connect.driver.exceptions.DataError: Invalid None value in non-Nullable column, column name: `UpdateBy`
|
||||||
|
|
||||||
|
================================
|
||||||
|
|
||||||
|
================================
|
||||||
|
TIME : 2026-05-26 16:00:28.826267
|
||||||
|
TABLE : Login_Sup
|
||||||
|
ERROR : Invalid None value in non-Nullable column, column name: `UpdateBy`
|
||||||
|
TRACEBACK :
|
||||||
|
Traceback (most recent call last):
|
||||||
|
File "d:\Python Code\Login_Sup_Import.py", line 427, in <module>
|
||||||
|
ch_client.insert_df(
|
||||||
|
~~~~~~~~~~~~~~~~~~~^
|
||||||
|
table=TABLE_NAME,
|
||||||
|
^^^^^^^^^^^^^^^^^
|
||||||
|
df=chunk,
|
||||||
|
^^^^^^^^^
|
||||||
|
database=CH_CONFIG['database']
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
)
|
||||||
|
^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\client.py", line 1013, in insert_df
|
||||||
|
return self.insert(table,
|
||||||
|
~~~~~~~~~~~^^^^^^^
|
||||||
|
df,
|
||||||
|
^^^
|
||||||
|
...<5 lines>...
|
||||||
|
transport_settings=transport_settings,
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
context=context)
|
||||||
|
^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\client.py", line 978, in insert
|
||||||
|
return self.data_insert(context)
|
||||||
|
~~~~~~~~~~~~~~~~^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\httpclient.py", line 347, in data_insert
|
||||||
|
response = self._raw_request(block_gen, params, headers, error_handler=error_handler, server_wait=False)
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\httpclient.py", line 569, in _raw_request
|
||||||
|
error_handler(response)
|
||||||
|
~~~~~~~~~~~~~^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\httpclient.py", line 331, in error_handler
|
||||||
|
raise ex
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\transform.py", line 114, in chunk_gen
|
||||||
|
col_type.write_column(data, output, context)
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\datatypes\base.py", line 216, in write_column
|
||||||
|
self.write_column_data(column, dest, ctx)
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\datatypes\base.py", line 231, in write_column_data
|
||||||
|
self._write_column_binary(column, dest, ctx)
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\datatypes\string.py", line 51, in _write_column_binary
|
||||||
|
handle_error(data_conv.write_str_col(column, self.nullable, encoding, dest), ctx)
|
||||||
|
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
File "C:\Users\dipanshuk\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\clickhouse_connect\driver\errors.py", line 17, in handle_error
|
||||||
|
raise DataError(msg)
|
||||||
|
clickhouse_connect.driver.exceptions.DataError: Invalid None value in non-Nullable column, column name: `UpdateBy`
|
||||||
|
|
||||||
================================
|
================================
|
||||||
Reference in New Issue
Block a user