6

这是我的第一个问题,希望你能帮助我。我在 Groovy(在 Oracle Data Integrator 12c 中)制作了一个脚本来自动化映射。这是我的程序的描述: 1 步:删除旧映射(如果存在)。第二步:寻找项目和文件夹(如果不存在:新建一个)。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)
4

1 回答 1

4

您可以将 Datatype 作为方法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

于 2015-09-25T15:17:46.137 回答