Introduction
This article shows how to Create custom functions in Data Services and provides a solution for finding the character count within a string.
Scenario
Let’s Consider below source data.
String |
AAABBBCCCDDDEEE |
AAAAAAABBCCDDDE |
AAAAAAAAAAAAABB |
Our goal is to find out the number of occurrences of every character within a string. In this example, we will find count of occurrence of character "A","B","C","D" and "E". We will create a custom function to acheive the desired result.
Desired Output:
Count of A | Count of B | Count of C | Count of D | Count of E |
3 | 3 | 3 | 3 | 3 |
7 | 2 | 2 | 3 | 1 |
13 | 2 | 0 | 0 | 0 |
Implementation:
.
Step 1 : Login to Data Service Designer.
Click Custom Function(in Local Object Library) -> Right Click Custom Function and select "New".
Step 2 : Fill Properties
Enter the Function Name Function Description & Click Next.
Step 3 : Create parameters
1) $Input – Column Name in the source schema that holds the string
2) $Value – This variable will store the character for which count is to be calculated
Creating Parameter: $input.
Right Click Parameters & Click Insert to get the below Screen
Enter Name : $input
Data Type : Varchar ( Source Field Data Type)
Parameter Type : Input
Length : 50 ( Maximum Length of our Source Field will be 50)
Similarly Create one more field of $Value of type varchar(1) and parameter Type as Input.
Step 4 :
Below Code is used to find the occurrences of given character in a String
Custom Function Code:
# To find the occurance of character in a String
Return nvl( length($Input),0)-nvl(length(replace_substr($Input,$Value,'')),0);
Step 5 : Creating Job to Execute the Custom Function
Step 6 : Double Click the Query Transformation and create new output column as shown below.
Output Column Should be a Type Int. Because the Value Return by Custom Function Will be of type int.
Click Ok and next Click in the Query Transformation.
You will get the below screen.
Click Next.
These are two input Parameter we have defined in our custom function.
$input is used to hold the column name from the input schema.
$Values is used to hold the character for which count is to be calculated.
Double Click the Table Name Task_1.
Pass the Column Name as Input To the Custom Function.
Press ok. Assign ‘A’ as value to $Value. Because here our aim is to find the occurrences of ‘A’ in the Column
VAR_TYPE ( Column Name)
Similarly Create other output Columns in Query Transformation To find the count of B,C,D and E character in a String.
$input will be Same for all the Output Column.
$Value will be Changed as ‘B’, ‘C’ for corresponding Output Column as shown Below.
Output Column 1 : Count of A, $input = VAR_TYPE, $Value = ‘A’
Output Column 2 : Count of B, $input = VAR_TYPE, $Value = ‘B’
Output Column 3 : Count of C, $input = VAR_TYPE, $Value = ‘C’
Output Column 4 : Count of D, $input = VAR_TYPE, $Value = ‘D’
Output Column 5 : Count of E, $input = VAR_TYPE, $Value = ‘E’
Step 8 : Let’s Validate and Execute the Job.
After Successfull job execution, the target template table will hold values as below.
Final Output: