Java in mapping

Highlighted
One Star

Java in mapping

Hi everyone,

I am trying to use some java code inside my mapping and I am getting error as Syntax error on token(s) , misplaced construct(s).
Below is my java code , I am just worry am I aloud to use this java structure inside my mapping panel as var. (Expration Builder)

if (row14.S != null) {
if (row14.S.trim().equalsIgnoreCase("Cash")) {
row14.InstrumentType = "Cash";
} else if (row14.S.trim().equalsIgnoreCase("Equity")) {
if (row14.T.trim().equalsIgnoreCase("ADR")) {
row14.InstrumentType = "ADR";
} else if (row14.T.trim().equalsIgnoreCase("GDR")) {
row14.InstrumentType = "GDR";
} else if (row14.T.trim().matches(".*[aA][dD][rR].*[gG][dD][rR].*")) {
row14.InstrumentType = "ADR";
} else if (row14.T.trim().equalsIgnoreCase("QUOTED EQUITIES")) {
row14.InstrumentType = "Equity";
}
}
}
One Star

Re: Java in mapping

Hi,

as of my knowledge you cannot use nested if in a Expression Builder you can use only conditional operator there.

for more info in using a conditional operator use the below link.

http://www.ibiblio.org/java/course/week2/43.html

Regards,
Vijay.M
One Star

Re: Java in mapping

if I can't use in an Expression builder what I will be able to use , I have to add I am not really Talend advince.
Four Stars

Re: Java in mapping

Hi ?eljka,

With this you may not use regexp but, maybe you'll find it useful or modify it to your needs (e.g. use equalsIgnoreCase instead of equals, etc..). It's a piece of code that acts like SQL IN operator.

You can add it inside code/routines and call it wherever...


	
/**
* SqlINstr: Emulate SQL IN operator.
*
*
* {talendTypes} boolean
*
* {Category} User Defined
*
* {param} String("value") input: The string need to be printed.
*
* {param} String[] ("value2","value3") pattern: The string array to match at least one.
*
* {example} SqlINstr (String EntryValue, String[] MatchingConditions)
*
*/
public static boolean SqlINstr(String EntryValue, String[] CondSet) {
boolean MatchFound = false;
if (EntryValue != null) {
int cnt = 0;
while (cnt < CondSet.length) {
if (EntryValue.equals(CondSet[cnt])) {MatchFound = true;}
cnt++;
}
} else {
MatchFound = false;
}

return MatchFound;

}

/**
* SqlINstr: Emulate SQL IN operator
*
*
* {talendTypes} boolean
*
* {Category} User Defined
*
* {param} Integer(1) input: The integer need to be printed.
*
* {param} Integer[] (100, 101) pattern: The integer array to match at least one.
*
* {example} SqlINnum (int EntryValue, int[] MatchingConditions)
*
*/
public static boolean SqlINnum(int EntryValue, int[] CondSet) {
int cnt = 0;
boolean MatchFound = false;
while (cnt < CondSet.length) {
if (EntryValue == CondSet[cnt]) {MatchFound = true;}
cnt++;
}
return MatchFound;
}

You can use it inside tJavaRow for example like this:

// Define array to be used in filtering records
String[] AcrGL = {"DND","DOD","SPD","VAG"};

//Some logical conditions
if (routines.MyCode.SqlINstr(input_row.acr, AcrGL))
{output_row.saldorsd_gl = input_row.saldorsd;}
else{output_row.saldorsd_gl = 0;};

and so forth...
One Star

Re: Java in mapping

Thanks will try..
z
Seven Stars

Re: Java in mapping

You just need to use a ternary expression in tMap, rather than if statements i.e.

row14.S != null
?row14.S.trim().equalsIgnoreCase("Cash")
?"Cash"
:row14.S.trim().equalsIgnoreCase("Equity")
?row14.T.trim().equalsIgnoreCase("ADR")
?"ADR"
:row14.T.trim().equalsIgnoreCase("GDR")
?"GDR"
:row14.T.trim().matches(".*[aA][dD][rR].*[gG][dD][rR].*")
?"ADR"
:row14.T.trim().equalsIgnoreCase("QUOTED EQUITIES")
?"Equity"
:null
:null
:null
One Star

Re: Java in mapping

Thanks guys , I had try both version and working ; )

Zeljka
One Star

Re: Java in mapping

If you have to change this logic a lot, you may want to consider moving it to a table. I put up a blog post that loads the regular expressions in Java data structure called a Map. The job then uses the Map for a lookup.

Rather than using a tMap, a custom tJavaRow component is used. For each data record input, the Java Map is consulted using the regular expression function "matches()".

http://bekwam.blogspot.com/2011/03/regex-lookup-table-with-talend-open.html

-Carl
One Star

Re: Java in mapping

Hi I have a problem while running a job , I'm not a telnd expert and I can't understand what's wrong with my code : 
if (row1.location_dest_id == 12) {row1.location_dest_id=113 ; } 
else if (row1.location_dest_id == 8) {row1.location_dest_id=109 ; }
else if (row1.location_dest_id == 9) {row1.location_dest_id=110 ; }
else if (row1.location_dest_id == 5) {row1.location_dest_id=106 ; }
else if (row1.location_dest_id == 6) {row1.location_dest_id=107 ; } 
else if (row1.location_dest_id == 7) {row1.location_dest_id=108 ; }
else if (row1.location_dest_id == 4) {row1.location_dest_id=105 ; }
else if (row1.location_dest_id == 14) {row1.location_dest_id=122 ;}
else if (row1.location_dest_id == 28) {row1.location_dest_id=119 ;}
else if (row1.location_dest_id == 25) {row1.location_dest_id=123 ; }
else if (row1.location_dest_id == 11) {row1.location_dest_id=116 ; }
else if (row1.location_dest_id == 27) {row1.location_dest_id=121 ; } ;
and here is the error that I get : 
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Syntax error on token(s), misplaced construct(s)
Syntax error on token(s), misplaced construct(s)

at metal.stock_move_test_0_1.stock_move_test.tPostgresqlInput_1Process(stock_move_test.java:2751)
at metal.stock_move_test_0_1.stock_move_test.runJobInTOS(stock_move_test.java:3876)
at metal.stock_move_test_0_1.stock_move_test.main(stock_move_test.java:3733)
could you please help me.
thank you !! 

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Have you checked out Talend’s 2019 Summer release yet?

Find out about Talend's 2019 Summer release

Blog

Talend Summer 2019 – What’s New?

Talend continues to revolutionize how businesses leverage speed and manage scale

Watch Now

6 Ways to Start Utilizing Machine Learning with Amazon We Services and Talend

Look at6 ways to start utilizing Machine Learning with Amazon We Services and Talend

Blog