DATASTAGE SCENARIO: REVERSE THE WORD

DATASTAGE-SCENARIO-GET-REVERSE-OF-THE-WORD

In this DataStage scenario, we need to get the output as reverse letters in a given word.

Input Data:

almost
big
datastage
etl
navy

Output Data:

tsomla
gib
egatsatad
tle
yvan

Solution:

Read the data using sequential file stage.

datastage-scenario-based-question-input-data-words

We can achieve the required output by using 2 transformer stages like below.

datastage-scenario-based-question-word-reverse-design

In first transformer stage, define the stage variables like shown below.

scount = Len (link.word)

Len() is a function to calculate the length of a field. So scount holds the value ‘6’ for ‘almost’ word and ‘3’ for ‘big’ word and so on.
In loop condition, define

@ITERATION <=scount

define loop variable ‘rev’ as

link.word[scount+1-@ITERATION,1]

datastage-scenario-based-question-word-reverse-first-tranformer

If you are a DataStage user, you’ll probably know that there is a new “Loop Condition” section (since DataStage 8.5 version) on the transformer canvas.

You will notice there is a loop test (which is a “While” condition) that controls how many times the output logic should be iterated over.

The logic that has been shown includes a new variable named “@ITERATION” which is a counter indicating what pass through the loop this is.

One other item that will appear new are the Loop variables – basically the same as stage variables, but these get evaluated each time through the loop.

The rest of this logic is old like basic substringing and calculations that we do in derivations area.

After this first transformer stage, your data would look like:

datastage-scenario-based-question-word-reverse-after-tranformer

In second transformer stage, define the stage variables like shown below.

NumSavedRows=SavedInputRecord()
IsBreak=LastRowInGroup(lnk_pivot_out.word)
FinalWord=IF IsBreak THEN AddingWord:lnk_pivot_out.RevLetter ELSE “”
AddingWord=IF IsBreak THEN “” ELSE AddingWord:lnk_pivot_out.RevLetter ELSE “”
NumRows=IF IsBreak THEN NumSavedRows ELSE 0

SaveInputRecord():

This function saves the entire record in cache and returns the number of records that are currently stored in cache, starting from 1.

LastRowInGroup(InputColumn):

You can call this function when processing an input row, and the function returns TRUE if this row is the last row in the input data.

And Loop condition as

@ITERATION <=NumRows

In Constraints

@ITERATION=1

datastage-scenario-based-question-word-reverse-second-tranformer

At the same time, run this transformer stage in Sequential mode by having Inline sort on ‘word’ and ‘seq’ as keys. And keep ‘seq’ sort in ascending order.

Run the job.

datastage-scenario-based-question-word-reverse-second-tranformer

Your final output would be like:

datastage-scenario-based-question-output-data-words

Comments

comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: