BefExtract 이벤트 스크립트 사용을 보여주는 추가 예

다음 이벤트 스크립트는 타겟 테이블에서 데이터를 지우는 데이터베이스 호출을 수행한 다음, sqlldr을 사용하여 EPM에서 추출된 데이터 세트로 타겟 테이블을 채우는 방법을 보여줍니다. 이 예에서는 클라우드 데이터베이스를 sqlldr로 채우는 방법을 보여주지만 다른 제3자 Oracle 이외 데이터베이스를 채우는 데 동일한 기술을 사용할 수 있습니다. 이전 예와 마찬가지로, 이 스크립트는 예로 제공되며 결함이 없다고 보증되지 않으므로, 사용자는 스크립트와 관련된 질문 또는 이슈에 대해 Oracle Support에 서비스 요청을 제출할 수 없습니다.

기본 쓰기 되돌림 프로세스는 개별 삽입 명령문을 사용하여 데이터를 타겟 데이터베이스에 게시하고, 쓰기 되돌림 데이터 세트가 큰 경우 기본 프로세스를 사용하는 대신 특정 데이터베이스 유틸리티를 사용하여 대량 데이터를 로드하는 것이 좋습니다. 이 예에서 스크립트는 Oracle sqlldr 유틸리티를 호출하는 방법을 보여주며, 이 유틸리티는 OTN에서 다운로드되었고 설치되어 이벤트 스크립트에 액세스할 수 있다고 가정합니다.

#------------------------------------------------------------------------------------------#
# befExport.py
# This script is used to perform database operations as part of the writeback process
# The script also uses sqlldr, and this must be installed prior to executing the script
# References to sqlldr and the control file will be different from this script and should
# be updated to reference the location where sqlldr is installed in your environment. 
#-----------------------------------------#
# Housekeeping and startup                # 
#-----------------------------------------#
from java.sql import DriverManager, SQLException
import subprocess

def main():
  #---------------------------------------------------#
  # Print integration context details to the log file #
  #---------------------------------------------------#
  agentAPI.logInfo("#---------------------------------#") 
  agentAPI.logInfo("# Delete data in table MC_WB_TEST ")
  agentAPI.logInfo("# Location:    " + agentContext["LOCATION"])
  agentAPI.logInfo("# Integration: " + agentContext["INTEGRATION"])
  agentAPI.logInfo("# Data File:   " + agentContext["WRITEBACK_DATA_FILE"])
  agentAPI.logInfo("#---------------------------------#")

  #---------------------------------------#
  # Retrieve user, password, and JDBC URL #
  #---------------------------------------#
  cred = agentAPI.getConnectionDetails()
  url = cred.getJDBCUrl()
  user = cred.getUserName()
  password = cred.getPassword()
  agentAPI.logInfo("# Connection Details")
  agentAPI.logInfo("# URL:  " + url)
  agentAPI.logInfo("# User: " + user)

  #---------------------------------------------------------------------------#
  # Open connection to the database and execute SQL                           #
  # This step deletes existing data before loading new data                   #
  # For large tables it is recommended to truncate and the recreate the table #
  #---------------------------------------------------------------------------#
  cnx = DriverManager.getConnection(url, user, password)
  agentAPI.logInfo("# Successfully connected to Oracle Cloud DB using DriverManager")
  stmt = cnx.createStatement()
  stmt.executeQuery('Delete from MC_WB_TEST')  
  agentAPI.logInfo("# Deleted all rows from MC_WB_TEST #")
  agentAPI.logInfo("#----------------------------------#")
  
  #-------------------------------------------------#
  # Define the SQL*Loader command and its arguments #
  #-------------------------------------------------#
  agentAPI.logInfo("# Starting SQLLDR")
  
  #----------------------------------------#
  # Replace \ with \\ in the filename path #
  #----------------------------------------#
  data_file = agentContext["WRITEBACK_DATA_FILE"]
  new_file = data_file.replace("\\", "\\\\")
  
  #---------------------------------------------#
  # Specify control file and include \\ in path #
  #---------------------------------------------#
  ctr_file = "C:\\EPMAgent\\bin\\MyData\\scripts\\instantclient\\load.ctl"

  #--------------------------------------------------------#
  # Specify user, password and tnsnames.ora entry          #
  # The format is user/password@<SID from tnsnames.ora>    #
  # for cloud databases.                                   #
  #--------------------------------------------------------#
  user = user + "/" + password + "@mcebs19c_medium"

  #------------------------#
  # Specify path to sqlldr #
  #------------------------#
  sqlldr_path = "C:\\EPMAgent\\bin\\MyData\\scripts\\instantclient\\sqlldr"
    
  #----------------------#
  # Build sqlldr command #
  #----------------------#
  sqlldr_command = [
    sqlldr_path,
    "userid=" + user,  
    "control=" + ctr_file,
    "data=" + new_file
  ]

  #----------------------------#
  # Run the SQL*Loader command #
  #----------------------------#
  with open("sqlldr_output.txt", "w") as fout:
    exit_code = subprocess.call(sqlldr_command, stdout=fout, shell=True)
  agentAPI.logInfo("# exit code: " + str(exit_code))
  agentAPI.logInfo("#---------------------------------#")

  #-------------------------------------------------------------#
  # Skip additional agent sql processing                        #
  # This ensures that the default insert processing is bypassed #
  #-------------------------------------------------------------#
  agentAPI.skipAction('true')

  #----------------------------------------------#
  # Raise an error if needed as part of          #
  # the process.  This will halt the processing  #
  # of the write-back process                    #                      
  #----------------------------------------------#
  #agentAPI.logInfo("# Manually raising a ValueError to stop the process")
  #raise ValueError("This is a manually raised ValueError")

  return

#------------------------------------------------------------#
# Check for location and process if needed                   #
# The same script is used for data load and write-back,      #
# so make sure that each specific case is correctly captured #
#------------------------------------------------------------#
#
if agentContext["LOCATION"] == "1WB_TEST":
  main()
else:   
  agentAPI.logInfo("#--------------------------------------#")
  agentAPI.logInfo("# Location: " + str(agentContext["LOCATION"]))
  agentAPI.logInfo("# Script not required, exiting now...")
  agentAPI.logInfo("#--------------------------------------#")