Skip to Content
0

Looking for a help to write UDF for sort and group records from the source

Mar 14 at 04:18 AM

75

avatar image
Former Member

I would like to generate a target structure based on the source. in the source two records with the first 4 fields are same then considered to be header and then remaining are details.in the target two DeviceItems should be generated.the below will be the output.

Could you please provide suggestions for the below udf?

List<String> list = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
String key = "";
for (int i = 0; i < var1.length; i++) {
if (!var1[i].equals(ResultList.CC)) {
sb.setLength(0);
if (key.equals(""))
{
result1.addValue("");
result1.addValue(ResultList.CC);
result2.addValue(var1[i]);
result2.addValue(ResultList.CC);
result3.addValue(var2[i]);
result3.addValue(ResultList.CC);
result4.addValue(var3[i]);
result4.addValue(ResultList.CC);
result5.addValue(var4[i]);
result5.addValue(ResultList.CC);
result6.addValue("");
result7.addValue(var5[i]);
result7.addValue(ResultList.CC);
result8.addValue(var6[i]);
result8.addValue(ResultList.CC);
result9.addValue(var7[i]);
result9.addValue(ResultList.CC);
result10.addValue(var8[i]);
result10.addValue(ResultList.CC);
}
key = sb.append(var1[i]).append(var2[i]).append(var3[i]).append(var5[i]).toString();

if (list.contains(key))
{
//result6.addValue("");
result7.addValue(var5[i]);
result7.addValue(ResultList.CC);
result8.addValue(var6[i]);
result8.addValue(ResultList.CC);
result9.addValue(var7[i]);
result9.addValue(ResultList.CC);
result10.addValue(var8[i]);
result10.addValue(ResultList.CC);

}
else {
list.add(key);
result1.addValue("");
result1.addValue(ResultList.CC);
result2.addValue(var1[i]);
result2.addValue(ResultList.CC);
result3.addValue(var2[i]);
result3.addValue(ResultList.CC);
result4.addValue(var3[i]);
result4.addValue(ResultList.CC);
result5.addValue(var4[i]);
result5.addValue(ResultList.CC);
result6.addValue("");
result7.addValue(var5[i]);
result7.addValue(ResultList.CC);
result8.addValue(var6[i]);
result8.addValue(ResultList.CC);
result9.addValue(var7[i]);
result9.addValue(ResultList.CC);
result10.addValue(var8[i]);
result10.addValue(ResultList.CC);
}
}
}

Thank you

Praveen

hwrwf.png (28.5 kB)
emyxz.png (8.6 kB)
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Evgeniy Kolmakov Mar 15 at 09:55 AM
1

Hi Praveen!

Regards, Evgeniy.


Show 7 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Evgeniy,

Oh! my good, Thanks for your efforts. you are one of the best developers, I used to read each thread answered by you from SCN.I have done mapping my own. Could you please check and provide your valuable comments?

I would like to just learn myself rather copied. Thank you very much.

Look forward for your comments.

Thanks

Praveen

mapping1.jpg (113.3 kB)
mapping2.jpg (69.3 kB)
0

Hi Praveen!

Thank you very much for your kind feedback!

Step 2: I guess you can't directly map header fields. Its order should be changed too according to sorting order implemented in Step 1.

Regards, Evgeniy.

0

Step 3: As you could see, I've used one design area to implement the mappings for all target nodes. If you take a look at your Step 1, you'll see that SplitByValue step produces the queue with exactly the same elements count and context changes you need to map to DeviceItems node. So, why not to use it instead of reproducing the same mapping again? :-)

Regards, Evgeniy.

0

Step 4: since ocurrence of these fields is 0..1, you should use MapWithDefault before RemoveContexts to avoid loosing the empty contexts in case the source element doesn't exist.

Regards, Evgeniy.

0
Former Member

Hi Evgeniy,

Thank you very much for your time. I have two more things need to added in the mapping.

1.Delete duplicates.

2 Drop records where status are not on the fixed values.

For the above I have created two separate mapping but I would like to create a single mapping and that too give no exception when there are no records.

a) Delete logic


List<String> list = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
String key = "";
for (int i = 0; i < var1.length; i++) {
if (!var1[i].equals(ResultList.CC)) {
sb.setLength(0);
key = sb.append(var1[i]).append(var2[i]).append(var3[i]).append(var4[i]).append(var5[i]).append(var6[i]).append(var7[i]).append(var8[i]).toString();
if (list.contains(key))
result.addSuppress();
else {
list.add(key);
result.addValue("");
}
}
}

a) Drop the records of the statuses not in fixed values.

For the above I have created two separate mappings, Could you please suggest me ? How do I include it into single a mapping?

Thanks

Praveen

0

Hi Praveen!

When you build the mapping for any of target fields, you can drag-n-drop another target element(s) to same mapping area using right mouse button.

Regards, Evgeniy.

0
Former Member

Ok, Thank you... How to create a target based on values maintained in the fixed values? this should be happened to the mapping you have mentioned earlier. I tried above , I am unable to do it in the same mapping.

Thanks

Praveen

0
Vivek Jain Mar 14 at 07:10 AM
1

Hi Praveen,

As per my understanding , it can be achieved using Graphical Mapping only with proper context handling.

Regards,

Vivek Jain

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Vivek,

Thank you, yes I tied with Graphical mapping . in the mapping I have to sort records with first 4 fields, if the 2 records or 3 are same then first 4 fields considered as header and remaining are details.so I have written UDF. can you share your views ? how to sort with combination of 4 fields with Graphical mapping.

Thank you

Praveen

0
Evgeniy Kolmakov Mar 14 at 05:46 AM
0

Hi Praveen!

Are you sure that you need UDF to fulfill your requirement?

Regards, Evgeniy.

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Evgeniy,

Thank you... I am not sure , I have tried with Graphical mapping and then used UDF . I have to sort the records with combination of first 4 fields and then it requires grouping.

Thanks

Praveen

0