Lab 2 - Data Transformation
In this lab you will transform an XML file to a JSON file using a Data Mapper. This component has a visual transformation tool that allows you to map fields from the source structure to the target structure. | |
---|---|
- Click on the menu File->Import..
- Select General-> “Existing Project into Workspace” and click Next.
- Click on “Select archive file” and browse to ~/FuseWorkshop/support/Lab2/Lab2.zip
- Click Finish.
- Create a new folder “Schemas” by right-clicking Lab2 -> New -> Folder
- Right-click the Schemas folder -> Import
- Select General -> File System and browse to ~/FuseWorkshop/support/Lab2/
- Select stocktrading.xsd and stocktrading.json and click Finish.
- Right-click on the imported stocktrading.xsd file and select Generate->JAXB Classes
- Select Lab2 as destination project and click Next.
- Enter com.demo.mycompany as package and click Finish.
Java Architecture for XML Binding (JAXB) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects. | |
---|---|
- Open blueprint.xml to modify the Camel route.
- Delete the Log component from the route and re-link the remaining components.
- Save the file (Ctrl+S).
Drag and drop a “Data Transformation” component from the transformation palette:
Transform ID: transOrder
- Dozer file path: transOrder.xml
- Source Type: Java
- Target Type: JSON
- Click Next
Source Type:
Click on the browse button and enter Stocktrading in the dialog and select the first class in the list.
Click OK and Next.
Target Type:
Browse for the stocktrading.json file.
Click Finish.
In the Data Transformation Editor, link each of the following source fields with their corresponding target fields by dragging and dropping them:
custId
- stockId
- vip
- Create a new mapping by clicking on the button.
- Drag and drop the name target field to the right box.
- Click on the arrow in the left box and select Set expression.
- Select Simple as the Language, and enter: ${body.name} ${body.lastName} as the Expression.
- Click OK.
- Create another mapping by clicking on the button again.
- Drag and drop the total target field to the right box.
- Click on the arrow in the left box and select Set expression.
- Select Groovy as the Language, and enter the following as Expression: request.body.shares * request.body.cost
- Click OK.
- Close the Data Transformation editor.
http://dozer.sourceforge.net/ | |
---|---|
- Back in blueprint.xml, re-link the File endpoint with the Data Transformation component “ref:transOrder”, and the latest with the SetHeader component.
- Save the file (Ctrl+S).
- Add a new Unmarshal component by dragging and dropping from the Transformation palette
Click on the properties tab,select the “jaxb” tab and enter the following property:
Context Path: com.demo.mycompany
The Marshal component serializes a bean to some binary or textual format for transmission over some transport via a Camel component. | |
---|---|
This Unmarshal component will process the incoming XML file and generate a Java object using the JAXB annotated class generated earlier (com.demo.mycompany.Stocktrading) | |
---|---|
- Re-link the File component to the Unmarshal component, and the latest to the ref:transOrder component.
.
Edit setHeader properties:
Expression: change file extension to “.json”
Save the blueprint (Ctrl+S).
- Go to Project Explorer and open pom.xml under Lab2 project
- Click on the Dependencies tab.
Click on the Add button.
Group id: org.apache.camel
- Artifact id: camel-jaxb
Version: ${camel-version}
Verify the following dependencies have been automatically added:
Camel-ftp
- Camel-dozer
- Camel-jackson
Camel-groovy
Save and close the file.
POM stands for "Project Object Model". It is an XML representation of a Maven project held in a file named pom.xml. The cornerstone of the POM is its dependency list, most every project depends upon others to build and run correctly. Maven downloads and links the dependencies for you on compilation and other goals that require them. As an added bonus, Maven brings in the dependencies of those dependencies (transitive dependencies), allowing your list to focus solely on the dependencies your project requires. (https://maven.apache.org/pom.html ) | |
---|---|
- Run the camel route by right-clicking blueprint.xml, Run As -> “Local Camel Context (without tests)”
Right-click on the InputDir folder and select Import.
If the folder doesn’t appear in the folder tree, refresh it (F5).
Browse to ~/FuseWorkshop/support/Lab2 and select File1.xml.
- Click Finish.
Verify that there is a new json file in the FTP server directory.
Open a terminal.
- Run: “ftp localhost”.
- Press enter for the default user (jboss).
- Enter jboss#1! as password.
- Run “cd OutputDir”.
- Run “ls” to validate there is a json file.
Run “quit” to exit ftp.
Go to ~/FuseWorkshop/OutputDir.
- Inspect the JSON file contents and verify that the name property is the concatenation of name + last name, and total is the multiplication of shares per cost.
- Go back to JBDS.
- Close all the open files.
- Stop the camel route by clicking the stop button .