Untitled
unknown
plain_text
2 years ago
5.1 kB
9
Indexable
import pandas as pd
import mysql.connector
import json
from base64 import b64decode as base_b64decode
import logging
from pandas import read_sql as pd_read_sql
from sqlalchemy import create_engine
import sys
releaseId = '275.2'
opId = 'HOB'
buId = 'DEFAULT'
replicationTarget = 'SOURCE'
replicationJobId = 'REP_990_234'
json_file_path = "/app/scripts/PPM_Release_Management/Product_Catalog_ETL/config/ppm_reply.json"
sql_log_file = f"/app/scripts/PPM_Release_Management/Product_Catalog_ETL/logs/{replicationJobId}ppm_reply.sql"
log_file = f'/app/scripts/PPM_Release_Management/Product_Catalog_ETL/logs/{replicationJobId}ppm_reply.log'
# Set up logging
logging.basicConfig(
filename=log_file,
level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
# Open SQL log file for writing
sql_log_file = open(sql_log_file, "w")
# Function to write SQL query to the log file
def write_sql(query_info):
sql_log_file.write(query_info)
sql_log_file.write('\n')
try:
# Function to establish a database connection
def connect_to_database(json_data, replicationTarget):
try:
encrypt = json_data.get(replicationTarget, {}).get('ENCRYPT')
host = json_data.get(replicationTarget, {}).get('DB_HOST')
port = json_data.get(replicationTarget, {}).get('DB_PORT')
user = json_data.get(replicationTarget, {}).get('DB_USER')
db_type = json_data.get(replicationTarget, {}).get('DB_TYPE')
schema = json_data.get(replicationTarget, {}).get('DB_SCHEMA')
if encrypt == 'Y':
password = base_b64decode(json_data.get(replicationTarget, {}).get('DB_PASSWORD')).decode('utf-8')
else:
password = json_data.get(replicationTarget, {}).get('DB_PASSWORD')
if db_type == 'MYSQL':
connection_string = f"mysql+mysqlconnector://{user}:{password}@{host}:{port}/{schema}"
engine = create_engine(connection_string)
logging.info(f"Connected to MySQL database server {replicationTarget}: {host}:{port}")
return engine, schema
except (mysql.connector.Error, Exception) as e:
logging.error(f"An error occurred while connecting to the database: {str(e)}")
raise e
try:
# Read JSON data from file
with open(json_file_path) as json_file:
json_data = json.load(json_file)
except FileNotFoundError:
print("File not found: " + json_file_path)
try:
# Connect to PPM_PC database
engine_ppm, schema_ppm = connect_to_database(json_data, 'PPM_PC')
# Fetch data from the etl_ppm_replication_master table
primary_query = f"SELECT * FROM {schema_ppm}.etl_ppm_replication_master WHERE eprm_catalog='PC_EXT' AND eprm_enabled_flg='Y'"
df = pd_read_sql(primary_query, con=engine_ppm)
# Connect to source database
engine_source, schema_source = connect_to_database(json_data, replicationTarget)
# Connect to target database
replicationTarget_EXT = replicationTarget + '_EXT'
engine_target, schema_target = connect_to_database(json_data, replicationTarget_EXT)
for _, row in df.iterrows():
eprm_table_name = row['eprm_table_name']
if eprm_table_name != 'PKG_PRD_FED_EXT_ATTRS':
source_query = f"SELECT * FROM {schema_source}.{eprm_table_name} WHERE release_id='{releaseId}' AND op_id='{opId}' AND bu_id='{buId}'"
try:
source_df = pd_read_sql(source_query, con=engine_source)
if not source_df.empty:
target_table = f"{schema_target}.{eprm_table_name}"
source_df.to_sql(name=target_table, con=engine_target, if_exists='append', index=False)
num_records = len(source_df)
query_info = f"-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ STATUS|{replicationTarget}| TABLE| {eprm_table_name}\n"
query_info += f"-- #Query: Inserted {num_records} records into {target_table}\n"
write_sql(query_info)
print(f"Inserted {num_records} records into {target_table}")
logging.info(f"Inserted {num_records} records into {target_table}")
except mysql.connector.Error as err:
print(f"Error occurred while executing the query: {source_query}: {err}")
logging.info(f"Error occurred while executing the query: {source_query}: {err}")
except Exception as e:
print("Error - {} . Line No - {} ".format(str(e), str(sys.exc_info()[-1].tb_lineno)))
print("An Error occurred while constructing dataframe:", str(e))
except mysql.connector.Error as err:
print("Error - {} . Line No - {} ".format(str(exp), str(sys.exc_info()[-1].tb_lineno)))
print(f"An error occurred: {err}")
logging.info(f"An error occurred: {err}")
finally:
if sql_log_file:
sql_log_file.close()
Editor is loading...