This article focuses on creating a project using groovy in ODI and adding a mapping for a table transformation. To create a Groovy script, you can open an edit window from the ODI Studio menu: Tool->groovy-> new script. Before executing the script below, verify that there is already an EMP table in the model.
Usersrc
Userdest
import oracle.odi.domain.project.OdiProject
Import Oracle.odi.domain.project.finder.IOdiProjectFinder
Import Oracle.odi.domain.model.finder.IOdiDataStoreFinder
Import Oracle.odi.domain.project.finder.IOdiFolderFinder
Import Oracle.odi.domain.project.finder.IOdiKMFinder
Import Oracle.odi.domain.mapping.finder.IMappingFinder
Import Oracle.odi.domain.adapter.project.IKnowledgeModule.ProcessingType
Import Oracle.odi.domain.model.OdiDataStore
Import Oracle.odi.core.persistence.transaction.support.DefaultTransactionDefinition
def setexpr (comp, tgttable, PropertyName, Expressiontext) {
Datastorecomponent.findattributeforcolumn (Comp,tgttable.getcolumn (PropertyName)). SetExpressionText ( Expressiontext)
}
// Project Creation function
def CreateProject (project_name, project_folder_name) {
Project_code = project_name
Txndef = new Defaulttransactiondefinition ();
TM = Odiinstance.gettransactionmanager ()
Tme = Odiinstance.gettransactionalentitymanager ()
Txnstatus = Tm.gettransaction (txndef)
PF = (iodiprojectfinder) tme.getfinder (odiproject.class)
FF = (iodifolderfinder) tme.getfinder (OdiFolder.class)
Project = Pf.findbycode (project_name)
if (project! = null) {
println "project already Exists. Project Creation skipped "
}
else{
Project = new Odiproject (project_name, project_name)
Tme.persist ( Project)
folder = new Odifolder (project, Project_folder_name)
Tme.persist (folder)
Tm.commit (txnstatus)
println "Project Created successfully"
}
}
// Map Creation Function
def createmapping (Project_name,project_folder_name,mymap) {
Txndef = new Defaulttransactiondefinition ()
TM = Odiinstance.gettransactionmanager ()
TME = Odiinstance.gettransactionalentitymanager ()
Txnstatus = Tm.gettransaction (txndef)
PF = (Iodiprojectfinder) tme.getfinder (Odiproject.class)
FF = (iodifolderfinder) tme.getfinder (Odifolder.class)
Project = Pf.findbycode (PROJECT_NAME)
Foldercoll = Ff.findbyname (Project_folder_name, PROJECT_NAME)
Odifolder folder = null
if (foldercoll.size () = = 1)
folder = Foldercoll.iterator (). Next ()
DSF = (iodidatastorefinder) tme.getfinder (Odidatastore.class)
MAPF = (imappingfinder) tme.getfinder (Mapping.class)
Mapping map = (MAPF). Findbyname (folder, MyMap)
if (map!=null) {
println "Map already Exists. Map Creation skipped "
}
else{
Map = new Mapping (MyMap, folder)
Tme.persist (map)
Ds_source = Dsf.findbyname ("EMP", "USERSRC")
Ds_src_comp = new Datastorecomponent (map, Ds_source)
Ds_target = Dsf.findbyname ("EMP", "Userdest")
Ds_tgt_comp = new Datastorecomponent (map, Ds_target)
ds_src_comp.connectto (Ds_tgt_comp)
setexpr (Ds_tgt_comp, Ds_target, "EMPNO", "EMP. EMPNO ")
setexpr (Ds_tgt_comp, Ds_target," ename "," EMP. Ename ")
setexpr (Ds_tgt_comp, Ds_target," JOB "," EMP. ") JOB ")
setexpr (Ds_tgt_comp, Ds_target," MGR "," EMP. MGR ")
setexpr (Ds_tgt_comp, Ds_target," HireDate "," EMP. HireDate ")
setexpr (Ds_tgt_comp, Ds_target," SAL "," EMP. SAL ")
setexpr (Ds_tgt_comp, Ds_target," COMM "," Emp.comm ")
setexpr (Ds_tgt_comp, Ds_target," DEPTNO "," EMP. DEPTNO ")
Deploymentspec = Map.getdeploymentspec (0)
node = Deploymentspec.findnode (Ds_tgt_comp)
println Deploymentspec.getexecutionunits ()
APS = Deploymentspec.getallapnodes ()
TGTs = Deploymentspec.gettargetnodes ()
IKMF = (iodikmfinder) tme.getfinder (Odiikm.class)
ins_ikm = Ikmf.findbyname ("ikm Oracle Insert");
LKMF = (iodikmfinder) tme.getfinder (Odilkm.class)
sql_lkm = Lkmf.findbyname ("Lkm Oracle to Oracle Pull (DB Link)");
API = Aps.iterator ()
Ap_node = Api.next ()
AP_NODE.SETLKM (SQL_LKM)
Ap_node.getoptionvalue (Processingtype.target, "Add_driving_site_hint"). SetValue ("true")
Tme.persist (map)
Tm.commit (Txnstatus)
println "Mapping Created successfully"
}
}
Create map End
// Execution
CreateProject ("Pro_scott", "First Folder")
CreateMapping ("Pro_scott", "First Folder", "new_mapping")
Click Execute script to see the generated project name (Pro_scott) and map name ( New_mapping)
Create a project using groovy script in ODI 12c