Four Stars

Split a input file into multiple output files

Hello,

I have an input file and I want to make "n" output files using distinct value.

Eg:

Input File

HZARA 123

L123456HY

L98798676

L54354254

HCUSTO456

L98687687

L64746689

 

I need n different files based on distinct values in Col1 with "H".

 

HZARA.txt

L123456HY

L98798676

L54354254

 

HCUSTO.txt

L98687687

L64746689

 

Any help in this direction would be vey helpful. Smiley Sad

2 ACCEPTED SOLUTIONS

Accepted Solutions
Twelve Stars TRF
Twelve Stars

Re: Split a input file into multiple output files

Hi,

You cannot use "!=" operator for a string.

Try to import the attached job.


TRF
Twelve Stars TRF
Twelve Stars

Re: Split a input file into multiple output files

Great!

You're welcome.

Think to select the answer which help you most and mark the question as solved.

Kudos also accepted Smiley Wink


TRF
14 REPLIES
Twelve Stars TRF
Twelve Stars

Re: Split a input file into multiple output files

Add a tJavaRow to set a context variable with the filename as soon the first character is H. Add a tFilterRow to exclude rows with a H as a first character and finally call a child job to write rows in the output file according to the context variable.

TRF
Four Stars

Re: Split a input file into multiple output files

Hi!

 

I'm learning Talend and i don't know how do that in the tJavaRow... Smiley Sad

Twelve Stars TRF
Twelve Stars

Re: Split a input file into multiple output files

Here is the main job:

Capture.PNG

- tFixedFlowInput is here to mimic your input file:

Capture.PNG

- tJavaRow set the needed context variables (filename and line):

output_row.line = input_row.line;
output_row.type = input_row.line.substring(0, 1);

context.line = input_row.line;

if(output_row.type.equalsIgnoreCase("H"))
	context.filename = input_row.line.substring(1, 5);

- tFilterRow is here to exclude header lines from result:

Capture.PNG

- tRunJob call the external job called "writer" (check the option "Transmit whole context" on this component to transmit the context variables "filename" and "line" you to define for both jobs)

 

Here is the external subjob "writer" :

Capture.PNG

tFixedFlowInput is here to convert context variable "line" into a flow (required for the next component):

Capture.PNG

- tFileOutputDelimited is here to write the flow into the current file (designed by filename context variable (check option "Append" to write at the end of the file):

Capture.PNG

 

It works.


TRF
Four Stars

Re: Split a input file into multiple output files

Thank you! I'm trying Smiley Very Happy

But I get error -> Error: Could not find or load main class. Robot Sad

Twelve Stars TRF
Twelve Stars

Re: Split a input file into multiple output files

Did you ever run a job successfully with this installation?
If so, please share a capture of your design

TRF
Four Stars

Re: Split a input file into multiple output files

Now it's ok.

But i can't choose "type" in the filter... why?

 

Sin título.png

Twelve Stars TRF
Twelve Stars

Re: Split a input file into multiple output files

Is the tFilter schema synchronized with the tJavaRow schema?

TRF
Four Stars

Re: Split a input file into multiple output files

Hi,

 

I have synchronized tFilter schema with the tJavaRow schema but the field "type" not...

 

Sin título2.png

Four Stars

Re: Split a input file into multiple output files

Hi again

 

I have create the out fields but the job says:

 

Sin título3.png

Twelve Stars TRF
Twelve Stars

Re: Split a input file into multiple output files

Hi,

You cannot use "!=" operator for a string.

Try to import the attached job.


TRF
Four Stars

Re: Split a input file into multiple output files

Thank you very much!! You are the best! Smiley Happy

 

I have trying do the same job but i have errors. Could you tell me why please? Smiley Sad

Twelve Stars TRF
Twelve Stars

Re: Split a input file into multiple output files

I cannot import your job (due to the version).
Can you share components configuration for those you are not sure?

TRF
Four Stars

Re: Split a input file into multiple output files

¡Now work!

I forget to declare the variables in "Contexts"... Robot Frustrated

 

Thank you for all (especially for your patience) Robot wink

Twelve Stars TRF
Twelve Stars

Re: Split a input file into multiple output files

Great!

You're welcome.

Think to select the answer which help you most and mark the question as solved.

Kudos also accepted Smiley Wink


TRF