Six Stars

Tmap & string filter

Hello (again),

 

I'm having issue on solving a problem that looks simple : In my tMap i have a field " category " which is made like that :

 

Category 

 

Clothes > shoes

Clothes > Top >  T shirt

Clothes > Top > Man > Sweat

Clothes > Trouser  > Women > Jeans 

... and so on

I want my output to look like that  (1st cateogry & Last category)

 

Category              Subcategory 

Clothes                      Shoes

Clothes                      T shirt 

Clothes                     Sweat

Clothes                     Jeans

 

 

Does anyone has an idea ? Since the string is not always the same, I'm lost.


Kindly

 

 

Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
Nine Stars

Re: Tmap & string filter

For the above scenario data we may need to add one more condition like below

Category_First: row2.Category==null?"":row2.Category.indexOf(">")<0?row2.Category:row2.Category.substring(0,row2.Category.indexOf(">")).trim() 

Category_Last:row2.Category==null?"": row2.Category.indexOf(">")<0?"":row2.Category.substring(row2.Category.lastIndexOf(">")+1).trim() 

 

Regards,

Veeru Boppudi
6 REPLIES
Fifteen Stars

Re: Tmap & string filter

You may want to try this with a tJavaRow.....but to be honest, the code I am giving you can be wrapped up in to a Talend Routine and used in a tMap.

 

Lets assume your data is arriving at the your tJavaRow in a column called "categories" and is leaving in two columns ("out1" and "out2"). You need to create  your output schema in the tJavaRow first. Then use the code below to split out your data.....

 

//Save your input data to a Sting variable
String cats = input_row.categories;

//Check if there is data as we can do nothing with null
if(cats!=null){
        //Use the String split method to split your String by >
	String[] catArray = cats.split(">");
	//Check that catArray has more than 0 elements
	if(catArray.length>0){
		output_row.out1 = catArray[0].trim();
		output_row.out2 = catArray[catArray.length-1].trim();
	}else{
		output_row.out1 = "";
		output_row.out2 = "";
	}
}else{
	output_row.out1 = "";
	output_row.out2 = "";
}

This is a very basic way of doing it, but should let you extrapolate from it or just use it as it is.

Rilhia Solutions
Nine Stars

Re: Tmap & string filter

You can try below process also

 

SplitString1.PNGSplitString2.PNG

Regards,

Veeru Boppudi
Six Stars

Re: Tmap & string filter

Hello & Thanks to both of you for your precious time;

 

As I tried to explain the easiest way, I prefer to stay on Tmap considering my job; more composants would blow my mind. I tried your process @vboppudi but I forgot that i have some field in Category that are empty. So I get an error : 


Exception in component tMap_1 (TestKooples)
java.lang.NullPointerException

 

So i tried this instead : 

(row7.Category != null)  ? row7.Category.substring(0,row7.Category.indexOf(">")).trim() : " "

(row7.Category != null ) ? row7.Category.substring(row7.Category.lastIndexOf(">")+1).trim() : " "

 

 

 

And its working fine, Thanks a lot again!

Regards

Nine Stars

Re: Tmap & string filter

If issue fixed please mark discussion as resolved.

 

Regards,

Veeru Boppudi
Fifteen Stars

Re: Tmap & string filter

Try the method you think works against this.....

"Clothes > shoes"

"Clothes > Top >  T shirt"

"Clothes > Top > Man > Sweat"

"Clothes > Trouser  > Women > Jeans"

"Clothes"

 

Rilhia Solutions
Nine Stars

Re: Tmap & string filter

For the above scenario data we may need to add one more condition like below

Category_First: row2.Category==null?"":row2.Category.indexOf(">")<0?row2.Category:row2.Category.substring(0,row2.Category.indexOf(">")).trim() 

Category_Last:row2.Category==null?"": row2.Category.indexOf(">")<0?"":row2.Category.substring(row2.Category.lastIndexOf(">")+1).trim() 

 

Regards,

Veeru Boppudi