Informatica 8.01之后提供以下6 种 API PowerExchange API, Transformation API,Operations API,Design API,Custom Function API。 用以实现不同的自动化功能。 下面提供一下Design API的代码,用以实现自动连接Oracle数据库,自动创建Source, Target, Mapping, Session和 Workflow。在自动创建ODS的方面比较有用。
Java 创建Mapping分为以下几步:
1.createRepository
2.createFolder
3.createSources
4.createTarget
5.createMappings
6.createSession
7.createWorkflow
8.generateOutput
代码示例:
package com.informatica.powercenter.sdk.mapfwk.samples;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Vector;
import com.informatica.powercenter.sdk.mapfwk.core.ConnectionInfo;
import com.informatica.powercenter.sdk.mapfwk.core.ConnectionPropsConstants;
import com.informatica.powercenter.sdk.mapfwk.core.Field;
import com.informatica.powercenter.sdk.mapfwk.core.Folder;
import com.informatica.powercenter.sdk.mapfwk.core.MapFwkOutputContext;
import com.informatica.powercenter.sdk.mapfwk.core.Mapping;
import com.informatica.powercenter.sdk.mapfwk.core.OutputSet;
import com.informatica.powercenter.sdk.mapfwk.core.RepoPropsConstant;
import com.informatica.powercenter.sdk.mapfwk.core.Repository;
import com.informatica.powercenter.sdk.mapfwk.core.RowSet;
import com.informatica.powercenter.sdk.mapfwk.core.Session;
import com.informatica.powercenter.sdk.mapfwk.core.SessionPropsConstants;
import com.informatica.powercenter.sdk.mapfwk.core.Source;
import com.informatica.powercenter.sdk.mapfwk.core.SourceTargetTypes;
import com.informatica.powercenter.sdk.mapfwk.core.Target;
import com.informatica.powercenter.sdk.mapfwk.core.TransformHelper;
import com.informatica.powercenter.sdk.mapfwk.core.Workflow;
public class TEST1 {
protected Repository rep;
protected Folder folder;
protected Session session;
protected Workflow workflow;
protected String mapFileName;
protected Mapping mapping;
protected int runMode = 0;
protected Source src;
protected Target tgt;
public static void main(String[] args) throws Exception {
try {
TEST1 test1 = new TEST1();
test1.createRepository();
test1.createFolder();
test1.createSources();
test1.createTarget();
test1.createMappings();
test1.createSession();
test1.createWorkflow();
test1.generateOutput();
} catch (Exception e) {
e.printStackTrace();
System.err.println( "Exception is: " + e.getMessage() );
}
}
protected void createRepository() {
rep = new Repository("PowerCenter", "PowerCenter", "");
}
protected void createFolder() {
folder = new Folder("TEST", "TEST", "" );
rep.addFolder(folder);
}
protected void createSources() throws Exception {
src = this.createDBSource();
folder.addSource(src);
}
protected void createTarget() {
tgt = this.createRelationalTarget(SourceTargetTypes.RELATIONAL_TYPE_ORACLE, "EMP");
}
public Source createDBSource() throws Exception{
Connection con = DBConnection.getConnection();
/* Class.forName(DBConnection.DB_DRIVER);
Connection con = DriverManager.getConnection(DBConnection.DB_URL,DBConnection.DB_USERNAME,DBConnection.DB_PASSWORD); */
DatabaseMetaData metadata = con.getMetaData();
ResultSet rs1 = metadata.getColumns(null, "SCOTT", "EMP", "%");
ResultSet rs2 = metadata.getPrimaryKeys(null, null, "EMP");
ResultSet rs3 = metadata.getImportedKeys(null, null, "EMP");
Vector fields = new Vector();
ArrayList<String> PKlist = new ArrayList<String>();
ArrayList<String> FKlist = new ArrayList<String>();
while(rs3.next()){
FKlist.add(rs3.getString("FKCOLUMN_NAME"));
}
while(rs2.next()){
PKlist.add(rs2.getString("COLUMN_NAME"));
}
while(rs1.next()){
String COLUMN_NAME = rs1.getString("COLUMN_NAME");
String TYPE_NAME = rs1.getString("TYPE_NAME").toLowerCase();
String COLUMN_SIZE = rs1.getString("COLUMN_SIZE");
String DECIMAL_DIGITS = rs1.getString("DECIMAL_DIGITS");
int IS_NULLABLE = rs1.getInt("NULLABLE");
if(DECIMAL_DIGITS == null) {DECIMAL_DIGITS = "";}
int Key = 0;
if(PKlist.contains(COLUMN_NAME) && !FKlist.contains(COLUMN_NAME)) {Key = 1;}
if(!PKlist.contains(COLUMN_NAME) && FKlist.contains(COLUMN_NAME)) {Key = 2;}
if(PKlist.contains(COLUMN_NAME) && FKlist.contains(COLUMN_NAME)) {Key = 3;}
boolean notNull;
System.out.println(Key);
if(IS_NULLABLE == 0) notNull = true; else notNull = false;
Field field = new Field(COLUMN_NAME,COLUMN_NAME, "", TYPE_NAME, COLUMN_SIZE, DECIMAL_DIGITS, Key, Field.FIELDTYPE_SOURCE, notNull);
fields.add(field);
}
ConnectionInfo conInfo = new ConnectionInfo(SourceTargetTypes.RELATIONAL_TYPE_ORACLE);
conInfo.getConnProps().setProperty(ConnectionPropsConstants.CONNECTIONNAME, "orcl");
Source Source = new Source( "EMP", "EMP", "", "EMP", conInfo);
Source.setFields(fields);
folder.addSource(src);
return Source;
}
protected Target createRelationalTarget( int type, String name ) {
ConnectionInfo conInfo = new ConnectionInfo(SourceTargetTypes.RELATIONAL_TYPE_ORACLE);
conInfo.getConnProps().setProperty(ConnectionPropsConstants.CONNECTIONNAME, "orcl");
Target target = new Target(name, name, name,name, conInfo);
return target;
}
protected void setMapFileName(Mapping mapping) {
StringBuffer buff = new StringBuffer();
buff.append(System.getProperty("user.dir"));
buff.append(java.io.File.separatorChar);
buff.append(mapping.getName());
buff.append(".xml");
mapFileName = buff.toString();
}
protected void createMappings() throws Exception {
this.mapping = new Mapping("TEST", "TEST", "");
setMapFileName(mapping);
TransformHelper helper = new TransformHelper(mapping);
OutputSet outSet = helper.sourceQualifier(src);
RowSet rs = (RowSet) outSet.getRowSets().get(0);
RowSet filterRS = (RowSet) helper.filter(rs, "EMPNO != NULL", "FILTRANS").getRowSets().get(0);
mapping.writeTarget(filterRS, tgt);
folder.addMapping(mapping);
}
protected void createSession() throws Exception {
session = new Session("S_"+mapping.getName(), "S_"+mapping.getName(),"");
session.getProperties().setProperty(SessionPropsConstants.TREAT_SOURCE_ROWS_AS, "Data driven");
session.getProperties().setProperty("CONNECTIONREFERENCE", "orcl");
session.setMapping(this.mapping);
}
protected void createWorkflow() throws Exception {
workflow = new Workflow("WF_"+mapping.getName(), "WF_"+mapping.getName(), "");
workflow.addSession(session);
workflow.getProperties().setProperty("SERVERNAME", "PowerCenter_Integration_Service");
folder.addWorkFlow(workflow);
System.out.println(workflow.getProperties());
}
public void generateOutput() throws Exception {
MapFwkOutputContext outputContext = new MapFwkOutputContext(
MapFwkOutputContext.OUTPUT_FORMAT_XML, MapFwkOutputContext.OUTPUT_TARGET_FILE,
mapFileName);
try {
intializeLocalProps();
}
catch (IOException ioExcp) {
System.err.println( "Error reading pcconfig.properties file." );
System.err.println( "The properties file should be in directory where Mapping Framework is installed.");
System.exit( 0 );
}
boolean doImport = false;
if (runMode >= 1) doImport = true;
rep.save(outputContext, doImport);
System.out.println( "Mapping generated in " + mapFileName );
if (runMode == 2){
rep.runWorkflow(workflow.getName());
}
}
protected ConnectionInfo getRelationalConnInfo( int dbType, String dbName ) {
ConnectionInfo connInfo = null;
connInfo = new ConnectionInfo( dbType );
connInfo.getConnProps().setProperty( ConnectionPropsConstants.DBNAME, dbName );
return connInfo;
}
protected void intializeLocalProps() throws IOException {
Properties properties = new Properties();
String filename = "pcconfig.properties";
InputStream propStream = getClass().getClassLoader().getResourceAsStream(filename);
if ( propStream != null ) {
properties.load( propStream );
rep.getProperties().setProperty(RepoPropsConstant.PC_CLIENT_INSTALL_PATH, properties.getProperty(RepoPropsConstant.PC_CLIENT_INSTALL_PATH));
rep.getProperties().setProperty(RepoPropsConstant.TARGET_FOLDER_NAME, properties.getProperty(RepoPropsConstant.TARGET_FOLDER_NAME));
rep.getProperties().setProperty(RepoPropsConstant.TARGET_REPO_NAME, properties.getProperty(RepoPropsConstant.TARGET_REPO_NAME));
rep.getProperties().setProperty(RepoPropsConstant.REPO_SERVER_HOST, properties.getProperty(RepoPropsConstant.REPO_SERVER_HOST));
rep.getProperties().setProperty(RepoPropsConstant.ADMIN_PASSWORD, properties.getProperty(RepoPropsConstant.ADMIN_PASSWORD));
rep.getProperties().setProperty(RepoPropsConstant.ADMIN_USERNAME, properties.getProperty(RepoPropsConstant.ADMIN_USERNAME));
rep.getProperties().setProperty(RepoPropsConstant.REPO_SERVER_PORT, properties.getProperty(RepoPropsConstant.REPO_SERVER_PORT));
rep.getProperties().setProperty(RepoPropsConstant.SERVER_PORT, properties.getProperty(RepoPropsConstant.SERVER_PORT));
rep.getProperties().setProperty(RepoPropsConstant.DATABASETYPE, properties.getProperty(RepoPropsConstant.DATABASETYPE));
}
else {
throw new IOException( "pcconfig.properties file not found.");
}
}
protected ConnectionInfo getFlatFileConnectionInfo() {
ConnectionInfo infoProps = new ConnectionInfo( SourceTargetTypes.FLATFILE_TYPE );
infoProps.getConnProps().setProperty(ConnectionPropsConstants.FLATFILE_SKIPROWS,"1");
infoProps.getConnProps().setProperty(ConnectionPropsConstants.FLATFILE_DELIMITERS,";");
infoProps.getConnProps().setProperty(ConnectionPropsConstants.DATETIME_FORMAT,"A 21 yyyy/mm/dd hh24:mi:ss");
infoProps.getConnProps().setProperty(ConnectionPropsConstants.FLATFILE_QUOTE_CHARACTER,"DOUBLE");
return infoProps;
}
}