[resolved] New to Pass row1 directly to routine without using dynamic schema

Four Stars

[resolved] New to Pass row1 directly to routine without using dynamic schema

I have a business scenario like, reading data from source and pass it as a whole(row1 connector name) to a routine and extract data data from the row1 inside routine without using dynamic schema feature of talend. 
I have created a job where where I am reading data from tRowGenerator , then pass it to tJava . In tJava, I am printing row1. I am not able to display the value of row1. It is displayed as an object. Please help on this.

Accepted Solutions
Sixteen Stars

Re: [resolved] New to Pass row1 directly to routine without using dynamic schema

Ok, you can do what you want, but I don't see why you would want to. However, I like a challenge and I guess you must have a reason, so here is how you do it. You will have to excuse my bad example, but hopefully it will give you enough info. I have created a job that you can see below....


A tRowGenerator and a tJavaFlex. The tRowGenerator is connected to a row called "row1". Talend generates code from you configuration of its components. So we need to take a look at the code. For this, I am going to the Code tab. Here is the code for the class created for "row1"....
	public static class row1Struct implements
routines.system.IPersistableRow<row1Struct> {
final static byte[] commonByteArrayLock_LOCAL_PROJECT_Test = new byte;
static byte[] commonByteArray_LOCAL_PROJECT_Test = new byte;
public String newColumn;
public String getNewColumn() {
return this.newColumn;
}
public String newColumn1;
public String getNewColumn1() {
return this.newColumn1;
}
public String newColumn2;
public String getNewColumn2() {
return this.newColumn2;
}
public String newColumn3;
public String getNewColumn3() {
return this.newColumn3;
}
public String newColumn4;
public String getNewColumn4() {
return this.newColumn4;
}
private String readString(ObjectInputStream dis) throws IOException {
String strReturn = null;
int length = 0;
length = dis.readInt();
if (length == -1) {
strReturn = null;
} else {
if (length > commonByteArray_LOCAL_PROJECT_Test.length) {
if (length < 1024
&& commonByteArray_LOCAL_PROJECT_Test.length == 0) {
commonByteArray_LOCAL_PROJECT_Test = new byte;
} else {
commonByteArray_LOCAL_PROJECT_Test = new byte;
}
}
dis.readFully(commonByteArray_LOCAL_PROJECT_Test, 0, length);
strReturn = new String(commonByteArray_LOCAL_PROJECT_Test, 0,
length, utf8Charset);
}
return strReturn;
}
private void writeString(String str, ObjectOutputStream dos)
throws IOException {
if (str == null) {
dos.writeInt(-1);
} else {
byte[] byteArray = str.getBytes(utf8Charset);
dos.writeInt(byteArray.length);
dos.write(byteArray);
}
}
public void readData(ObjectInputStream dis) {
synchronized (commonByteArrayLock_LOCAL_PROJECT_Test) {
try {
int length = 0;
this.newColumn = readString(dis);
this.newColumn1 = readString(dis);
this.newColumn2 = readString(dis);
this.newColumn3 = readString(dis);
this.newColumn4 = readString(dis);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public void writeData(ObjectOutputStream dos) {
try {
// String
writeString(this.newColumn, dos);
// String
writeString(this.newColumn1, dos);
// String
writeString(this.newColumn2, dos);
// String
writeString(this.newColumn3, dos);
// String
writeString(this.newColumn4, dos);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(super.toString());
sb.append("");
return sb.toString();
}
/**
* Compare keys
*/
public int compareTo(row1Struct other) {
int returnValue = -1;
return returnValue;
}
private int checkNullsAndCompare(Object object1, Object object2) {
int returnValue = 0;
if (object1 instanceof Comparable && object2 instanceof Comparable) {
returnValue = ((Comparable) object1).compareTo(object2);
} else if (object1 != null && object2 != null) {
returnValue = compareStrings(object1.toString(),
object2.toString());
} else if (object1 == null && object2 != null) {
returnValue = 1;
} else if (object1 != null && object2 == null) {
returnValue = -1;
} else {
returnValue = 0;
}
return returnValue;
}
private int compareStrings(String string1, String string2) {
return string1.compareTo(string2);
}
}

I am assuming that you are proficient in Java, so won't explain this. But you can see that the class if public. Therefore we can use this. 
I next created a routine (it is very poorly named as I did this quickly). You can see that below.....
package routines;
import local_project.test_0_1.Test.row1Struct;
/*
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
* it must be before the "{talendTypes}" key.
*
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
* Short
*
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
*
* 4. {param} 's format is: {param} <type> <name>
*
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
* added. you can have many parameters for the Function.
*
* 5. {example} gives a example for the Function. it is optional.
*/
public class TestRoutine {
public static void rowHandlingTest(row1Struct myRow) {

System.out.println("My Row = "+myRow.toString());
}
}

I am importing the "row1Struct" class from the job I created (therefore this routine will only work with this job) and have created a simple method to print out the columns from the row1Struct class (it prints out some other bits as well, but you modify this to what you want).
Now, we need to call this routine. I do that in a tJavaFlex. The reason I do that is because it can receive row data. The tJava is not great for this. The code I have used is shown below. This code goes in the "Main Code" section of the tJavaFlex.....
routines.TestRoutine.rowHandlingTest(row1);

Now, when the job is run, I get the following printed to the output....
My Row = local_project.test_0_1.Test$row1Struct@5cb27de5
My Row = local_project.test_0_1.Test$row1Struct@5cb27de5
My Row = local_project.test_0_1.Test$row1Struct@5cb27de5
My Row = local_project.test_0_1.Test$row1Struct@5cb27de5
My Row = local_project.test_0_1.Test$row1Struct@5cb27de5

The row1Struct class' toString method outputs each record as above, but if you want just the column data you can select that as you wish. 
Hope this helps.

All Replies
Sixteen Stars

Re: [resolved] New to Pass row1 directly to routine without using dynamic schema

Ok, you can do what you want, but I don't see why you would want to. However, I like a challenge and I guess you must have a reason, so here is how you do it. You will have to excuse my bad example, but hopefully it will give you enough info. I have created a job that you can see below....


A tRowGenerator and a tJavaFlex. The tRowGenerator is connected to a row called "row1". Talend generates code from you configuration of its components. So we need to take a look at the code. For this, I am going to the Code tab. Here is the code for the class created for "row1"....
	public static class row1Struct implements
routines.system.IPersistableRow<row1Struct> {
final static byte[] commonByteArrayLock_LOCAL_PROJECT_Test = new byte;
static byte[] commonByteArray_LOCAL_PROJECT_Test = new byte;
public String newColumn;
public String getNewColumn() {
return this.newColumn;
}
public String newColumn1;
public String getNewColumn1() {
return this.newColumn1;
}
public String newColumn2;
public String getNewColumn2() {
return this.newColumn2;
}
public String newColumn3;
public String getNewColumn3() {
return this.newColumn3;
}
public String newColumn4;
public String getNewColumn4() {
return this.newColumn4;
}
private String readString(ObjectInputStream dis) throws IOException {
String strReturn = null;
int length = 0;
length = dis.readInt();
if (length == -1) {
strReturn = null;
} else {
if (length > commonByteArray_LOCAL_PROJECT_Test.length) {
if (length < 1024
&& commonByteArray_LOCAL_PROJECT_Test.length == 0) {
commonByteArray_LOCAL_PROJECT_Test = new byte;
} else {
commonByteArray_LOCAL_PROJECT_Test = new byte;
}
}
dis.readFully(commonByteArray_LOCAL_PROJECT_Test, 0, length);
strReturn = new String(commonByteArray_LOCAL_PROJECT_Test, 0,
length, utf8Charset);
}
return strReturn;
}
private void writeString(String str, ObjectOutputStream dos)
throws IOException {
if (str == null) {
dos.writeInt(-1);
} else {
byte[] byteArray = str.getBytes(utf8Charset);
dos.writeInt(byteArray.length);
dos.write(byteArray);
}
}
public void readData(ObjectInputStream dis) {
synchronized (commonByteArrayLock_LOCAL_PROJECT_Test) {
try {
int length = 0;
this.newColumn = readString(dis);
this.newColumn1 = readString(dis);
this.newColumn2 = readString(dis);
this.newColumn3 = readString(dis);
this.newColumn4 = readString(dis);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public void writeData(ObjectOutputStream dos) {
try {
// String
writeString(this.newColumn, dos);
// String
writeString(this.newColumn1, dos);
// String
writeString(this.newColumn2, dos);
// String
writeString(this.newColumn3, dos);
// String
writeString(this.newColumn4, dos);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(super.toString());
sb.append("");
return sb.toString();
}
/**
* Compare keys
*/
public int compareTo(row1Struct other) {
int returnValue = -1;
return returnValue;
}
private int checkNullsAndCompare(Object object1, Object object2) {
int returnValue = 0;
if (object1 instanceof Comparable && object2 instanceof Comparable) {
returnValue = ((Comparable) object1).compareTo(object2);
} else if (object1 != null && object2 != null) {
returnValue = compareStrings(object1.toString(),
object2.toString());
} else if (object1 == null && object2 != null) {
returnValue = 1;
} else if (object1 != null && object2 == null) {
returnValue = -1;
} else {
returnValue = 0;
}
return returnValue;
}
private int compareStrings(String string1, String string2) {
return string1.compareTo(string2);
}
}

I am assuming that you are proficient in Java, so won't explain this. But you can see that the class if public. Therefore we can use this. 
I next created a routine (it is very poorly named as I did this quickly). You can see that below.....
package routines;
import local_project.test_0_1.Test.row1Struct;
/*
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
* it must be before the "{talendTypes}" key.
*
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
* Short
*
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
*
* 4. {param} 's format is: {param} <type> <name>
*
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
* added. you can have many parameters for the Function.
*
* 5. {example} gives a example for the Function. it is optional.
*/
public class TestRoutine {
public static void rowHandlingTest(row1Struct myRow) {

System.out.println("My Row = "+myRow.toString());
}
}

I am importing the "row1Struct" class from the job I created (therefore this routine will only work with this job) and have created a simple method to print out the columns from the row1Struct class (it prints out some other bits as well, but you modify this to what you want).
Now, we need to call this routine. I do that in a tJavaFlex. The reason I do that is because it can receive row data. The tJava is not great for this. The code I have used is shown below. This code goes in the "Main Code" section of the tJavaFlex.....
routines.TestRoutine.rowHandlingTest(row1);

Now, when the job is run, I get the following printed to the output....
My Row = local_project.test_0_1.Test$row1Struct@5cb27de5
My Row = local_project.test_0_1.Test$row1Struct@5cb27de5
My Row = local_project.test_0_1.Test$row1Struct@5cb27de5
My Row = local_project.test_0_1.Test$row1Struct@5cb27de5
My Row = local_project.test_0_1.Test$row1Struct@5cb27de5

The row1Struct class' toString method outputs each record as above, but if you want just the column data you can select that as you wish. 
Hope this helps.
Four Stars

Re: [resolved] New to Pass row1 directly to routine without using dynamic schema

I followed the steps . Now, it is working for me. Thanks a lot Richard Hall.