Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hello! Is it possible to somehow figure out what column names are in a schema that's getting passed through?
Lets say the schema is PersonID, FirstName, LastName. I would want to generate a string that said "PersonID, FirstName, LastName". Then I could split this string on the "," and get one-record-per-column-name.
Is this possible? One solution I have is to write one record to a CSV, which will have the column names as the first line. Then read the first line of that CSV as a raw text file. But this is not an ideal solution at all.
Thank you!
Hello @Aaron Rumley
You could use the row that connects the component from which you want to extract the column names and convert it fully to String.
Lets say you have tDBInput -> row1 -> tJava
Then on tJava component you can use the expression row1.toString() to convert the whole Struct to text.
This expression above will return something like this:
project.job_0_1.job$row1Struct@3f57bcad[PersonId=value,FirstName=value,LastName=value]
Next you could use some regular expression or even some substring/split methods to extract the column names from the resulting String.
I hope it helps.
This is a good start, thank you! It'd be nice if Talend had some built in functionality for this.
if anyone in the future is interested, here's my code for this. It probably won't work if one of your columns has a record that has a "," in it's value, or if the value is JSON, since that contains lots of commas and also square brackets.
/***
* if your component is connected with a "row1" connection, for example
* then call row1.toString(), and pass in that string to this method. It will produce an array of the column names in row1.
*
* At the moment, this doesn't work if the schema contains columns that contain json.
*
*/
public static ArrayList<String> PrintColumnNames(String schemaString)
{
String parsedString = StringHandling.RIGHT(schemaString, schemaString.length() - schemaString.indexOf("[")).replaceAll("\\[|\\]", "");
String[] arrayOfStr = parsedString.split(",");
ArrayList<String> nameList = new ArrayList<String>();
for (int i = 0; i < arrayOfStr.length; i++)
{
String extract = StringHandling.LEFT(arrayOfStr[i], (arrayOfStr[i].length() - (arrayOfStr[i].length() - arrayOfStr[i].indexOf("=")) ));
nameList.add(extract);
//System.out.println(extract);
}
return nameList;
}