在groovy中自动映射ODI并进行表达

时间:2015-09-25 13:10:33

标签: dynamic groovy automation mapping oracle-data-integrator

这是我的第一个问题,希望你能帮助我。我在Groovy(在Oracle Data Integrator 12c中)创建一个脚本来自动化映射。这是我的prodecure的描述: 1步:删除旧映射(如果存在)。 2步:寻找项目和文件夹(如果不存在:创建新文件夹)。 3步:创建新的映射 4步:实现源表和目标表 5步:创建表达式 第6步:链接每一栏

现在我的问题:有人可以帮我用动态表达式制作这个脚本吗?像这样: 第1步:获取目标列的数据类型 第2步:在表达式中获取正确的数据类型 第3步:将错误类型(总是Varchar)更改为正确的类型(数字或日期或仍然是Varchar) 第4步:链接每一列

我的障碍:我从来没有用groovy和Java做过什么我不是很好。所以我不可能让这种动态。我的脚本中的几乎所有内容都是从一些互联网站点放在一起的。找到一些了解我的问题的人会很高兴。我认为对于所有将从OWB变为ODI的人来说,这将是一个很好的脚本。

谢谢!

//Von ODI Studio erstellt
//
//name of the project
projectName = "SRC_TO_TRG"
//name of the folder
ordnerName = "FEN_TEST"
//name of the mapping
mappingName = "MAP1_FF_TO_TRG"
//name of the model
modelName = "DB_FEN"
//name of the source datastore
sourceDatastoreName = "SRC_TEST_FEN"
//name of the target datastore
targetDatastoreName = "TRG_TEST_FEN"

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,ordnerName)
//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
  boundTo_emp = dsf.findByName(sourceDatastoreName, modelName)
  comp_emp = new DatastoreComponent(map, boundTo_emp)

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

 //create expression-operator  
  comp_expression = new ExpressionComponent(map, "EXPRESSION")

// define expression
  comp_expression.addExpression("LAND_KM",     "TO_NUMBER(SRC_TEST_FEN.LAND_KM)", null,null,null);
  comp_expression.addExpression("DATE_OF_ELECTION",     "TO_DATE(SRC_TEST_FEN.DATE_OF_ELECTION, 'DD.MM.YYYY')", null,null,null);
//weitere Transformationen anhängen möglich   

//link source table with expression
  comp_emp.connectTo(comp_expression)

//link expression with target table
  comp_expression.connectTo(comp_tgtemp)

  createExp(comp_tgtemp, boundTo_tgtemp, "ABBR", "SRC_TEST_FEN.ABBR")
  createExp(comp_tgtemp, boundTo_tgtemp, "NAME", "SRC_TEST_FEN.NAME")
  createExp(comp_tgtemp, boundTo_tgtemp, "LAND_KM", "EXPRESSION.LAND_KM")
  createExp(comp_tgtemp, boundTo_tgtemp, "DATE_OF_ELECTION",     "EXPRESSION.DATE_OF_ELECTION")

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

1 个答案:

答案 0 :(得分:2)

您可以将数据类型作为方法addExpression的第三个参数传递。 您还可以将大小和比例作为第四和第五个参数传递。

例如,对于LAND_KM表达式,请将此行替换为:

MapAttribute map_attr = DatastoreComponent.findAttributeForColumn(comp_tgtemp,boundTo_tgtemp.getColumn("LAND_KM"))
comp_expression.addExpression("LAND_KM", "TO_NUMBER(SRC_TEST_FEN.LAND_KM)", map_attr.getDataType(),map_attr.getSize(),map_attr.getScale());

由于findAttributeForColumn,它检索LAND_KM的目标列,然后检索数据类型,大小和比例,并在Expression组件中添加新表达式时使用它。

如果你想根据名字自动映射它,David Allan在官方Oracle博客上写了一篇关于如何做的帖子,并提供了他的代码:https://blogs.oracle.com/dataintegration/entry/odi_12c_mapping_sdk_auto