Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

How to get column name of csv file dyanamically

Dear everybody,

 

I want to split an input csv file to multiple files by size and I have to get the header name which is defined on tFileInputDelimited component for creating partition files with header name and the flow is contained in the picture below:

 

0683p000009M739.png

I am using tJavaFlex to split files by size. Here is my code: (I have referenced: https://community.talend.com/t5/Design-and-Development/Split-source-based-on-byte-size/ta-p/35466)

 

// start part of your Java code
Integer iterator = 1;
Integer ByteCount = 0;
FileOutputStream fos = new
FileOutputStream(context.TgtFilePath+"TargetFile_"+iterator+".csv");


String tmpReadLine= row1._account_id_+","+ row1._user_id_+","+ row1._coupon_id_+","+ row1._redeemed_on_+","+ row1._status_+","+ row1._expiry_date_+","+ row1._added_on_+","+ row1._reason_+","+ row1._created_at_+","+ row1._updated_at_+"\n"; //Read input fields

byte[] contentInBytes = tmpReadLine.getBytes();//Convert them to Byte array

ByteCount=ByteCount+contentInBytes.length; // Summation of line bytes read

if ( ByteCount > context.fileSizeLimit * 1048576 ) {
    
    // Check if bytes read hasn't crossed the threshold
    ByteCount = contentInBytes.length;

    fos.flush();
    fos.close();
    iterator = iterator+ 1;

    // Threshold crossed write to new file
    
    fos = new FileOutputStream(context.TgtFilePath+"TargetFile_"+iterator+".csv");
    String headerFile = "account_id,user_id,coupon_id,redeemed_on,status,expiry_date,added_on,reason,created_at,updated_at\n";
	byte[] contentHeader = headerFile.getBytes();
	fos.write(contentHeader);

    fos.write(contentInBytes);
    
} else {
    fos.write(contentInBytes); // else write to same file
}

fos.close();

I want to get tmpReadLine dyanamically based on CSV file schema in my code. 

 

Who can help me for this case please!!!

Thank you guys!

 

Labels (2)
3 Replies
Anonymous
Not applicable
Author

I'm not entirely sure what you are trying to do. Are you trying to make this code....

String tmpReadLine= row1._account_id_+","+ row1._user_id_+","+ row1._coupon_id_+","+ row1._redeemed_on_+","+ row1._status_+","+ row1._expiry_date_+","+ row1._added_on_+","+ row1._reason_+","+ row1._created_at_+","+ row1._updated_at_+"\n"; //Read input fields

....more dynamic to take whatever the schema is that is passed into the component?

 

Anonymous
Not applicable
Author

Yes, @rhall. I want to tmpReadLine get index of header name of CSV file. Example: CSV file has : A, B, C columns and tmpReadLine can get row1.A, row1.B, row1.C values dyanamically

Anonymous
Not applicable
Author

You can get a String representation of the header and values using this code.....

System.out.println(row1.toString());

To get just the values in the order they are supplied you can use this....

System.out.println(row1.toLogString());

Obviously you would need to do some String manipulation, but it looks like your Java is good enough to figure that out 🙂