ds-blog-icon.png

THE DATA DOWNLOAD

As a business intelligence consulting company, we pride ourselves on being able
to deliver on our projects as well as provide good quality content to our readers.

Informatica PowerCenter Directory Listing Using a Java Transformation

Posted by Jerry Perez on Apr 25, 2016 1:53:10 PM

Sometimes as developers we find ourselves locked out of the Informatica PowerCenter environment preventing us from verifying if a particular folder, target, source or parameter file exists in the PowerMart directory. Using a Java Transformation provides a potential solution to this problem. The following will explain how to build a Java Transformation that takes as input the PowerMart root directory or any directory the Informatica PowerCenter account has access to and return information about all the files within the directory and its sub-directories as rows.

Java Transformation

First, create a Java Transformation with the Input and Output ports illustrated on (A.1). When asked the transformation type make sure Active is selected.

Informatica PowerCenter image 1

An active Java Transformation will allow for multiple output rows to be generated from a single input row which is the behavior need to generate all directory file entries from the root directory.

Java Libraries

Now let’s begin coding, we need to import the necessary Java libraries that will facilitate the file and directory structure object information as well as a few other libraries. In the Java Transformation select the Java Code tab then the Import Packages sub-tab and add the following:

import java.io.File; // Used to facilitate file and directory object representations

import java.util.ArrayList; // Used to create a collection object to store all file and directory objects

import java.text.DecimalFormat; // Used to format file and directory objects size

Select the Helper Code sub-tab to add the functions that will traverse the directory folder and retrieve the information for each file in the path.

 

// FilePathCollection holds a list of file paths

ArrayList<String> FilePathCollection = new ArrayList<String>();

 

// Navigate a file path directory folder recursively to traverse all its sub-folders

public void traverse( String filePath ) {

File path = new File( filePath ); // Create a File Data Structure (path) from the filePath provided

File[] fileList = path.listFiles(); // Collect a listing (fileList) of all the files found in the path

if ( fileList == null ) return; // If the fileList is empty return control to the calling function block

for ( File fileObject : fileList ) { // For every fileObject found in the fileList

if ( fileObject.isDirectory() ) { // If the fileObject is of type Directory

traverse( fileObject.getAbsolutePath() ); // Call the traverse function and pass the fileObject (of type Directory)

}

else { // If the fileObject is NOT of type Directory (of type File)

FilePathCollection.add( fileObject.getPath() ); // Add the fileObject Path to the FilePathCollection (of type File)

}

}

}

 

// Return the filePath's directory path (without the name just the path)

public String getFileDirectory( String filePath ) {

File fileObject = new File( filePath ); // Create a File Data Structure (path) from the filePath provided

return fileObject.getParent(); // Return the file path

}

 

// Return the filePath's file name (without the path or file type extension just the name)

public String getFileName( String filePath ) {

File fileObject = new File( filePath ); // Create a File Data Structure (path) from the filePath provided

String name = fileObject.getName(); // Get the name of the fileObject

String nameWoExtension = null; // Set the name without extension to null

int dotPosition = name.lastIndexOf( '.' ); // Get the last position of charater '.'

if ( dotPosition > 0 ) { // If the dotPosition is greater than 0

nameWoExtension = name.substring( 0, dotPosition ); // Set name without extension to the name up to the dotPosition

}

// Return the name without the extension if is not null otherwise return the name

return (nameWoExtension != null) ? nameWoExtension : name;

}

 

// Return the file extension of a file path

public String getFileExtension( String filePath ) {

File fileObject = new File( filePath ); // Create a File Data Structure (path) from the filePath provided

String extension = ""; // Set the extension variable to empty

String name = fileObject.getName(); // Get the name of the fileObject

int dotPosition = name.lastIndexOf( '.' ); // Get the last position of charater '.'

if ( dotPosition > 0 ) { // If the dotPosition is greater than 0

// Set extension to the name from one position after the dotPosition to the end

extension = name.substring( dotPosition + 1 );

}

return extension; // Return the extension

}

 

// Return the size in kilobytes of a file path

public String getFileSize( String filePath ) {

// Set the character format that will be used for the size of the file

DecimalFormat sizeStringFormat = new DecimalFormat("###,###,###.####");

File fileObject = new File( filePath ); // Create a File Data Structure (path) from the filePath provided

// Return the size of the file path using the sizeStringFormat ( size provided in bytes, dividing by 1024 will result in kilobytes)

return sizeStringFormat.format( (fileObject.length() / 1024.00 ) );

}

 

Now we wrap it up by adding the code to generate rows for each of the file entries found in the directory structure. Select the On Input Row sub-tab to add the following:

 

String rootDir = ROOT_PATH; // Set the rootDir variable to the input port ROOT_PATH

 

if( !isNull( "ROOT_PATH" ) ) { // If ROOT_PATH is not null then proceed

// Call the traverst function and pass it the rootDir variable to collect all direcory file entries

traverse( rootDir );

// For Every fileObject in the FilePathCollection (loop through all fileObjects)

for ( String fileObject : FilePathCollection ) {

// Get the File Directory and assign it to FILE_DIR

FILE_DIR = getFileDirectory( fileObject );

// Get the File Name and assign it to FILE_NAME

FILE_NAME = getFileName( fileObject );

// Get the File Extension and assign it to FILE_EXT

FILE_EXT = getFileExtension( fileObject );

// Get the File Size and assign it to FILE_SIZE_KB

FILE_SIZE_KB = getFileSize( fileObject );

// Generate and output the data row collected for the current fileObject

generateRow();

}

}

 

Now the Java Expression has everything it needs to generate all the file directory entries from the PowerMart directory or any directory the Informatica PowerCenter account has access to.

NOTE: In case the path to the PowerMart root directory is unknown a parameter at the mapping level can be created and assigned an initial value of the root directory variable used by Informatica PowerCenter, an Expression can be then used to pass the variable to the input port of the Java Transformation as follows:

  • In the mapping where the Java Transformation is to be used, select Parameters and Variables… from the Mappings menu bar.
  • Add a new variable and name it something like $$ROOT_DIR, set it as a type of Parameter, as data type of string and with a precision long enough to hold a directory name, 256 for example. Set the Initial value: as $PMTempDir.
  • Create an Expression with an output port that uses the variable $$ROOT_DIR and pass it to the Java Expression.

Topics: Blog, Informatica PowerCenter

Written by Jerry Perez