ODI - Groovy скрипт запускается в ODI "родной" groovy, но не в ODI. - PullRequest
0 голосов
/ 16 марта 2020

У меня есть сценарий Groovy, который отлично работает из ODI - Инструменты> Groovy Сценарий и кнопка RUN, но не запускается (ошибки) при сохранении в процедуре ODI.

Groovy сценарий :

//name of the project
projectName = "DL_GENERATE_MAPPINGS"
//name of the folder
projectFolderName = "GENERATED_FOLDER1"
//name of the mapping
mappingName = "S2R_GENERATED_MAPPING1"
//name of the model for source
sourceModelName = "LDWH_ORACLE_SRC_CDF_OWNER"
//name of the model for target
targetModelName = "LDWH_HIVE_RAW"
//name of the source datastore
sourceDatastoreName = "DWH_ACCOUNTS_DIM"
//name of the target datastore
targetDatastoreName = "dwh_acc_dim_raw"
//filter condition
filterCondition = "1=1";


// function createExpressions help us by mapping source to target in one-to-one method
enum MatchTypes {
    EQUALS,
    SRCENDSWITH,
    TGTENDSWITH,
    SRCSTARTSWITH,
    TGTSTARTSWITH
}
enum MatchCaseTypes {
    MATCH,
    IGNORECASE
}

def createExpressions(component, conPoint, matchType, matchCaseType) {
    atts = null
    if (conPoint != null) atts = conPoint.getUpstreamInScopeAttributes()
    else atts = component.getUpstreamLeafAttributes(component)
    tatts = component.getAttributes()
    for (MapAttribute tgt_attr: tatts) {
        attr_str = tgt_attr.getName()
        if (matchCaseType == MatchCaseTypes.IGNORECASE) {
            attr_str = attr_str.toLowerCase()
        }
        sourceCol = null;
        for (MapAttribute src_attr: atts) {
            src_attr_str = src_attr.getName()
            if (matchCaseType == MatchCaseTypes.IGNORECASE) {
                src_attr_str = src_attr_str.toLowerCase()
            }
            if ((matchType == MatchTypes.SRCENDSWITH && src_attr_str.endsWith(attr_str)) ||
                (matchType == MatchTypes.SRCSTARTSWITH && src_attr_str.startsWith(attr_str)) ||
                (matchType == MatchTypes.TGTSTARTSWITH && attr_str.startsWith(src_attr_str)) ||
                (matchType == MatchTypes.TGTENDSWITH && attr_str.endsWith(src_attr_str)) ||
                (matchType == MatchTypes.EQUALS && attr_str.equals(src_attr_str))) {
                sourceCol = src_attr
                break
            }
        }
        if (sourceCol != null && conPoint != null) tgt_attr.setExpression(conPoint, sourceCol, null)
        else if (attr_str != null && (attr_str == "t_dl_cob_dt" || attr_str == "t_dl_ins_dt" || attr_str == "t_dl_cti")) {
            tgt_attr.setExpression(sourceCol);
            tgt_attr.setKeyIndicator(false);
            tgt_attr.setUpdateIndicator(false);
            tgt_attr.setInsertIndicator(false);
        } else if (sourceCol != null) tgt_attr.setExpression(sourceCol);
    }
}
// end of function createExpressions
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


//set expression to the component
def createExp(comp, tgtTable, propertyName, expressionText) {
    DatastoreComponent.findAttributeForColumn(comp, tgtTable.getColumn(propertyName)).setExpressionText(expressionText)
}

//delete mapping with the same name
def removeMapping(folder, map_name) {
    txnDef = new DefaultTransactionDefinition()
    tm = odiInstance.getTransactionManager()
    tme = odiInstance.getTransactionalEntityManager()
    txnStatus = tm.getTransaction(txnDef)
    try {
        Mapping map = ((IMappingFinder) tme.getFinder(Mapping.class)).findByName(folder, map_name)
        if (map != null) {
            odiInstance.getTransactionalEntityManager().remove(map);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    tm.commit(txnStatus)
}

//looking for a project and folder
def find_folder(project_code, folder_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_code)

    //if there is no project, create new one
    if (project == null) {
        project = new OdiProject(project_code, project_code)
        tme.persist(project)
    }
    //if there is no folder, create new one
    folderColl = ff.findByName(folder_name, project_code)
    OdiFolder folder = null
    if (folderColl.size() == 1)
        folder = folderColl.iterator().next()
    if (folder == null) {
        folder = new OdiFolder(project, folder_name)
        tme.persist(folder)
    }
    tm.commit(txnStatus)
    return folder
}

//name of the project and the folder
folder = find_folder(projectName, projectFolderName)
//delete old mapping
removeMapping(folder, mappingName)

txnDef = new DefaultTransactionDefinition()
tm = odiInstance.getTransactionManager()
tme = odiInstance.getTransactionalEntityManager()
txnStatus = tm.getTransaction(txnDef)

dsf = (IOdiDataStoreFinder) tme.getFinder(OdiDataStore.class)
mapf = (IMappingFinder) tme.getFinder(Mapping.class)

//create new mapping
map = new Mapping(mappingName, folder);
tme.persist(map);


//insert source table
boundSrcToMap = dsf.findByName(sourceDatastoreName, sourceModelName)
comp_emp = new DatastoreComponent(map, boundSrcToMap)

//insert target table
boundTrgToMap = dsf.findByName(targetDatastoreName, targetModelName)
comp_tgtemp = new DatastoreComponent(map, boundTrgToMap)

//create a new filter object in map
comp_filter = new FilterComponent(map, "FILTER_1")

//link source table to filter
comp_emp.connectTo(comp_filter)

//link source with target
comp_filter.connectTo(comp_tgtemp)

//add filter expression
comp_filter.setFilterCondition(filterCondition)

//map columns one to one
createExpressions(comp_tgtemp, null, MatchTypes.EQUALS, MatchCaseTypes.IGNORECASE);

//create daily layer design
map.createPhysicalDesign("DailyLayer");
map.createPhysicalDesign("CorrectionLayer");

tme.persist(map)
tm.commit(txnStatus)

Ошибки при запуске в процедуре ODI:

ODI-1217: Session test (196738) fails with return code 7000.
ODI-1226: Step test fails after 1 attempt(s).
ODI-1232: Procedure test execution fails.
ODI-1590: The execution of the script failed. 
Caused By: org.apache.bsf.BSFException: exception from Groovy: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
_New_Task_: 142: unable to resolve class Mapping 
 @ line 142, column 7.
   map = new Mapping(mappingName, folder); 
         ^

_New_Task_: 148: unable to resolve class DatastoreComponent 
 @ line 148, column 12.
   comp_emp = new DatastoreComponent(map, boundSrcToMap) 
              ^

_New_Task_: 152: unable to resolve class DatastoreComponent 
 @ line 152, column 15.
   comp_tgtemp = new DatastoreComponent(map, boundTrgToMap) 
                 ^

_New_Task_: 155: unable to resolve class FilterComponent 
 @ line 155, column 15.
   comp_filter = new FilterComponent(map, "FILTER_1") 
                 ^

_New_Task_: 38: unable to resolve class MapAttribute 
 @ line 38, column 5.
       for (MapAttribute tgt_attr: tatts) { 
       ^

_New_Task_: 44: unable to resolve class MapAttribute 
 @ line 44, column 9.
           for (MapAttribute src_attr: atts) { 
           ^

_New_Task_: 90: unable to resolve class Mapping 
 @ line 90, column 17.
           Mapping map = ((IMappingFinder) tme.getFinder(Mapping.class)).findByName(folder, map_name) 
                   ^

_New_Task_: 112: unable to resolve class OdiProject 
 @ line 112, column 19.
           project = new OdiProject(project_code, project_code) 
                     ^

_New_Task_: 117: unable to resolve class OdiFolder 
 @ line 117, column 15.
       OdiFolder folder = null 
                 ^

_New_Task_: 121: unable to resolve class OdiFolder 
 @ line 121, column 18.
           folder = new OdiFolder(project, folder_name) 
                    ^

10 errors

    at org.codehaus.groovy.bsf.GroovyEngine.exec(GroovyEngine.java:114)
    at com.sunopsis.dwg.codeinterpretor.SnpScriptingInterpretor.execInBSFEngine(SnpScriptingInterpretor.java:396)
    at com.sunopsis.dwg.codeinterpretor.SnpScriptingInterpretor.exec(SnpScriptingInterpretor.java:247)
    at oracle.odi.runtime.agent.execution.interpreter.SessionTaskScriptingInterpretor.scripting(SessionTaskScriptingInterpretor.java:190)
    at oracle.odi.runtime.agent.execution.SessionTask.scripting(SessionTask.java:105)
    at oracle.odi.runtime.agent.execution.cmd.ScriptingExecutor.execute(ScriptingExecutor.java:49)
    at oracle.odi.runtime.agent.execution.cmd.ScriptingExecutor.execute(ScriptingExecutor.java:21)
    at oracle.odi.runtime.agent.execution.TaskExecutionHandler.handleTask(TaskExecutionHandler.java:52)
    at oracle.odi.runtime.agent.execution.SessionTask.processTask(SessionTask.java:206)
    at oracle.odi.runtime.agent.execution.SessionTask.doExecuteTask(SessionTask.java:117)
    at oracle.odi.runtime.agent.execution.AbstractSessionTask.execute(AbstractSessionTask.java:886)
    at oracle.odi.runtime.agent.execution.SessionExecutor$SerialTrain.runTasks(SessionExecutor.java:2225)
    at oracle.odi.runtime.agent.execution.SessionExecutor.executeSession(SessionExecutor.java:610)
    at oracle.odi.runtime.agent.processor.TaskExecutorAgentRequestProcessor$1.doAction(TaskExecutorAgentRequestProcessor.java:718)
    at oracle.odi.runtime.agent.processor.TaskExecutorAgentRequestProcessor$1.doAction(TaskExecutorAgentRequestProcessor.java:611)
    at oracle.odi.core.persistence.dwgobject.DwgObjectTemplate.execute(DwgObjectTemplate.java:203)
    at oracle.odi.runtime.agent.processor.TaskExecutorAgentRequestProcessor.doProcessStartAgentTask(TaskExecutorAgentRequestProcessor.java:800)
    at oracle.odi.runtime.agent.processor.impl.StartSessRequestProcessor.access$1400(StartSessRequestProcessor.java:74)
    at oracle.odi.runtime.agent.processor.impl.StartSessRequestProcessor$StartSessTask.doExecute(StartSessRequestProcessor.java:702)
    at oracle.odi.runtime.agent.processor.task.AgentTask.execute(AgentTask.java:180)
    at oracle.odi.runtime.agent.support.DefaultAgentTaskExecutor$2.run(DefaultAgentTaskExecutor.java:108)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at oracle.odi.runtime.agent.execution.job.OdiJob.call(OdiJob.java:73)
    at oracle.odi.runtime.agent.execution.job.OdiJob.call(OdiJob.java:73)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
_New_Task_: 142: unable to resolve class Mapping 
 @ line 142, column 7.
   map = new Mapping(mappingName, folder); 
         ^

_New_Task_: 148: unable to resolve class DatastoreComponent 
 @ line 148, column 12.
   comp_emp = new DatastoreComponent(map, boundSrcToMap) 
              ^

_New_Task_: 152: unable to resolve class DatastoreComponent 
 @ line 152, column 15.
   comp_tgtemp = new DatastoreComponent(map, boundTrgToMap) 
                 ^

_New_Task_: 155: unable to resolve class FilterComponent 
 @ line 155, column 15.
   comp_filter = new FilterComponent(map, "FILTER_1") 
                 ^

_New_Task_: 38: unable to resolve class MapAttribute 
 @ line 38, column 5.
       for (MapAttribute tgt_attr: tatts) { 
       ^

_New_Task_: 44: unable to resolve class MapAttribute 
 @ line 44, column 9.
           for (MapAttribute src_attr: atts) { 
           ^

_New_Task_: 90: unable to resolve class Mapping 
 @ line 90, column 17.
           Mapping map = ((IMappingFinder) tme.getFinder(Mapping.class)).findByName(folder, map_name) 
                   ^

_New_Task_: 112: unable to resolve class OdiProject 
 @ line 112, column 19.
           project = new OdiProject(project_code, project_code) 
                     ^

_New_Task_: 117: unable to resolve class OdiFolder 
 @ line 117, column 15.
       OdiFolder folder = null 
                 ^

_New_Task_: 121: unable to resolve class OdiFolder 
 @ line 121, column 18.
           folder = new OdiFolder(project, folder_name) 
                    ^

10 errors

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:554)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:584)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:604)
    at org.codehaus.groovy.bsf.GroovyEngine.exec(GroovyEngine.java:112)
    ... 27 more

Я понимаю ошибки .. похоже, что процедура ODI не распознает некоторые Groovy классы ... но я не понимаю почему.

1 Ответ

0 голосов
/ 20 марта 2020

Вам необходимо импортировать все эти классы. Я думаю, что это работает, когда вы выполняете его непосредственно через ODI Studio, потому что он уже импортирован самой ODI Studio.

Я бы посоветовал вам поместить раздел импорта в самый верх вашего скрипта, перед кодом Дэвида Аллана для обработки autopping.

Добавьте хотя бы следующие:

import oracle.odi.domain.project.OdiProject;
import oracle.odi.domain.project.OdiFolder;
import oracle.odi.core.persistence.transaction.ITransactionStatus;
import oracle.odi.core.persistence.transaction.ITransactionManager;
import oracle.odi.core.persistence.transaction.ITransactionDefinition;
import oracle.odi.core.persistence.transaction.support.DefaultTransactionDefinition;
import oracle.odi.domain.mapping.Mapping;
import oracle.odi.domain.mapping.MapComponent;
import oracle.odi.domain.mapping.MapAttribute;
import oracle.odi.domain.mapping.component.DatastoreComponent;
import oracle.odi.domain.mapping.component.FilterComponent;
import oracle.odi.domain.mapping.component.DataStorageDelegate;
...