Search code examples
javacsvtalend

Modify CSV fields before import them to a DataBase


I want to get information that is stored in a CSV file, and send it to a Oracle DataBase Table (I'm using the 'tOracleOutput' component'). However, before I send that information to the respective columns in the table, I want to change some fields.

For example:

If the field X in the CSV file = 15 -> I want to write 0 in the column Y in the Database Table

If the field X in the CSV file = 30 -> I want to write 1 in the column Y in the Database Table

Can I use the 'tJava' component to do this? How can I access each field I receive from the CSV file in the 'tJava' component?

Thanks


Solution

  • Accessing columns inside a tJavaRow is always possible. Be careful, tJava insert code inside the main(), but you need a code to be executed at each iteration. Since you need to evaluate an expression for each row of your dataset, you need tJavaRow.

    Basically if 'foo' is the name of your ingoing connection and 'X' is your field, it means you have a foo public struct with a 'bar' public attribute inside. So, you can access with foo.bar

    Given 'bar' your outgoing connection to tOracleOutput, inside your tJavaRow code you could use something like:

    bar.Y = (foo.X == 15 ? 0 : (foo.X == 30 ? 1 : null))
    

    Anyway, why use a tJavaRow component when you can do the same with simple drag and drop and a couple of lines of code inside a tMap component?