Bassochette
Bassochette

Reputation: 166

How to break string from an excel file into substrings and load it?

I'm actually working on a talend job. I need to load from an excel file to an oracle 11g database.

I can't figure out how to break a field of my excel entry file within talend and load the broken string into the database.

For example I've got a field like this:

toto:12;tata:1;titi:15

And I need to load into a table, for example grade:

| name | grade |
|------|-------|
| toto |12     |
| titi |15     |
| tata |1      |
|--------------|

Thank's in advance

Upvotes: 1

Views: 1847

Answers (2)

drmirror
drmirror

Reputation: 3760

In a Talend job, you can use tFileInputExcel to read your Excel file, and then tNormalize to split your special column into individual rows with a separator of ";". After that, use tExtractDelimitedFields with a separator of ":" to split the normalized column into name and grade columns. Then you can use a tOracleOutput component to write the result to the database.

While this solution is more verbose than the Java snippet suggested by AlexR, it has the advantage that it stays within Talend's graphical programming model.

Upvotes: 3

AlexR
AlexR

Reputation: 115338

for(String pair : str.split(";")) {
    String[] kv = pair.split(":");
    // at this point you have separated values
    String name = kv[0];
    String grade = kv[1];

    dbInsert(name, grade);
}

Now you have to implement dbInsert(). Do it either using JDBC or using any higher level tools (e.g. Hivernate, iBatis, JDO, JPA etc).

Upvotes: 2

Related Questions