My java code which I am using to run the above XML transformation are as follows:
As I not able to get any constructor in XMLExtract.Mapping which works only for element and not for outPort, I am passing outPort as -ve nos.
// create connection object. Get driver and connect string from cfg file
// specified as a first argument
DBConnection dbCon = new DBConnection("Conn0", "D:/postgre.cfg");
dbCon.setName("Test");
EngineInitializer.initEngine("plugins", null, null);
EngineInitializer.forceActivateAllPlugins();
GraphRuntimeContext runtimeContext = new GraphRuntimeContext();
TransformationGraph graph = new TransformationGraph();
graph.setDebugMode(true);
graph.addConnection(dbCon);
Phase phase = new Phase(0);
Sequence sequence = new SimpleSequence("Sequence0",graph,"Sequence0" , "C:/misc/seq.dat",0,1,1000);
graph.addSequence(sequence);
sequence.isInitialized();
DataRecordMetadata metadata1=new DataRecordMetadata("RecordMetadata0",DataRecordMetadata.DELIMITED_RECORD);
metadata1.addField(new DataFieldMetadata("ID", DataFieldMetadata.STRING_FIELD,"\n"));
metadata1.addField(new DataFieldMetadata("count", DataFieldMetadata.INTEGER_FIELD,"\n"));
Edge inEdge1 = new Edge("InEdge1",metadata1);
DataRecordMetadata metadata2=new DataRecordMetadata("RecordMetadata0",DataRecordMetadata.DELIMITED_RECORD);
metadata2.addField(new DataFieldMetadata("ID", DataFieldMetadata.STRING_FIELD,"\n"));
metadata2.addField(new DataFieldMetadata("count", DataFieldMetadata.INTEGER_FIELD,"\n"));
Edge inEdge2 = new Edge("InEdge2",metadata2);
XMLExtract nodeXML=new XMLExtract("Smallest");
nodeXML.setInputFile("C:/misc/smallest.xml");
nodeXML.setEnabled("enabled");
nodeXML.setUseNestedNodes(true);
nodeXML.addOutputPort(0, inEdge1);
nodeXML.addOutputPort(1, inEdge2);
XMLExtract.Mapping childMap1=nodeXML.new Mapping("Item_Number",-1);
XMLExtract.Mapping childMap2=nodeXML.new Mapping("ID",0);
childMap2.setSequenceField("count");
childMap2.setSequenceId("Sequence0");
childMap1.addChildMapping(childMap2);
nodeXML.addMapping(childMap1);
XMLExtract.Mapping childMap4=nodeXML.new Mapping("Mill_Description",-2);
XMLExtract.Mapping childMap3=nodeXML.new Mapping("ID",1);
childMap3.setSequenceField("count");
childMap3.setSequenceId("Sequence0");
childMap4.addChildMapping(childMap3);
nodeXML.addMapping(childMap4);
DataRecordMetadata metadata3=new DataRecordMetadata("RecordMetadata0",DataRecordMetadata.DELIMITED_RECORD);
metadata3.addField(new DataFieldMetadata("field1", DataFieldMetadata.STRING_FIELD,"\n"));
metadata3.addField(new DataFieldMetadata("field2", DataFieldMetadata.STRING_FIELD,"\n"));
Edge inEdge3 = new Edge("InEdge3",metadata3);
String[] joinKeys = {"count"};
MergeJoin join = new MergeJoin("join1","count;#count", null,"com.clover.test.Transform","",MergeJoin.Join.LEFT_OUTER,true,true);
join.addInputPort(0, inEdge1);
join.addInputPort(1, inEdge2);
join.setGraph(graph);
join.setPhase(phase);
join.addOutputPort(0, inEdge3);
DBOutputTable dbOutputTable = new DBOutputTable("DBTABLE",dbCon.getId(),"product");
dbOutputTable.setSqlQuery("insert into product (product_id,name) values(?,?)");
dbOutputTable.addInputPort(0, inEdge3);
String[] cloverFields = {"field1;field2"};
dbOutputTable.setCloverFields(cloverFields);
//add nodes to phase
try {
graph.addPhase(phase);
phase.addNode(nodeXML);
phase.addNode(join);
phase.addNode(dbOutputTable);
graph.addEdge(inEdge1);
graph.addEdge(inEdge2);
graph.addEdge(inEdge3);
EngineInitializer.initGraph(graph, runtimeContext);
} catch (GraphConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ComponentNotReadyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
IThreadManager threadManager = new SimpleThreadManager();
WatchDog watchDog = new WatchDog(graph,runtimeContext);
runtimeContext.setUseJMX(true);
watchDog.getGraphRuntimeContext().setVerboseMode(true);
threadManager.executeWatchDog(watchDog);
and my Transform method code :
public int transform(DataRecord[] inputRecords, DataRecord[] outputRecords) throws TransformException {
try {
// user's code STARTs from here !
System.err.println("### Running Transformation ");
(outputRecords[0].getField(0)).setValue((inputRecords[0].getField(0).toString()));
(outputRecords[0].getField(1)).setValue((inputRecords[1].getField(0).toString()));
//(outputRecords[0].getField(2)).setValue((inputRecords[2].getField(0).toString()));
// user's code ENDs here !
} catch(Exception e) {
throw new TransformException("Error in transformation class " + Transform.class.getName() + ": " + e.getMessage(), e);
}
return ALL;
}
I am getting NullRecord at outPort 1 from XMLExtract to MergeJoin when transform method gets invoked. And also because of NullRecord I am getting exception as parameter number: 2 missing in insert query.
Please suggest some solution for the above issue.
Thanks
Pushpendra