Lab 2 - Data Transformation

RH_Icon_Compass_Shiny.png 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.
  1. Click on the menu File->Import..
  2. Select General-> “Existing Project into Workspace” and click Next.
  3. Click on “Select archive file” and browse to ~/FuseWorkshop/support/Lab2/Lab2.zip
  4. Click Finish.
  5. Create a new folder “Schemas” by right-clicking Lab2 -> New -> Folder
  6. Right-click the Schemas folder -> Import
  7. Select General -> File System and browse to ~/FuseWorkshop/support/Lab2/
  8. Select stocktrading.xsd and stocktrading.json and click Finish.
  9. Right-click on the imported stocktrading.xsd file and select Generate->JAXB Classes

  1. Select Lab2 as destination project and click Next.
  2. Enter com.demo.mycompany as package and click Finish.

general_info_polished.png 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.
  1. Open blueprint.xml to modify the Camel route.
  2. Delete the Log component from the route and re-link the remaining components.
  3. Save the file (Ctrl+S).
  4. Drag and drop a “Data Transformation” component from the transformation palette:

  5. Transform ID: transOrder

  6. Dozer file path: transOrder.xml
  7. Source Type: Java
  8. Target Type: JSON

  1. Click Next
  2. Source Type:

  3. Click on the browse button and enter Stocktrading in the dialog and select the first class in the list.

  4. Click OK and Next.

  5. Target Type:

  6. Browse for the stocktrading.json file.

  7. Click Finish.

  8. In the Data Transformation Editor, link each of the following source fields with their corresponding target fields by dragging and dropping them:

  9. custId

  10. stockId
  11. vip

  1. Create a new mapping by clicking on the button.
  2. Drag and drop the name target field to the right box.
  3. Click on the arrow in the left box and select Set expression.

  1. Select Simple as the Language, and enter: ${body.name} ${body.lastName} as the Expression.
  2. Click OK.

  1. Create another mapping by clicking on the button again.
  2. Drag and drop the total target field to the right box.
  3. Click on the arrow in the left box and select Set expression.

  1. Select Groovy as the Language, and enter the following as Expression: request.body.shares * request.body.cost
  2. Click OK.

  1. Close the Data Transformation editor.
general_info_polished.png http://dozer.sourceforge.net/
  1. Back in blueprint.xml, re-link the File endpoint with the Data Transformation component “ref:transOrder”, and the latest with the SetHeader component.
  2. Save the file (Ctrl+S).

  1. Add a new Unmarshal component by dragging and dropping from the Transformation palette
  2. Click on the properties tab,select the “jaxb” tab and enter the following property:

  3. Context Path: com.demo.mycompany

general_info_polished.png The Marshal component serializes a bean to some binary or textual format for transmission over some transport via a Camel component.
general_info_polished.png 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)
  1. Re-link the File component to the Unmarshal component, and the latest to the ref:transOrder component.

.

  1. Edit setHeader properties:

  2. Expression: change file extension to “.json”

  3. Save the blueprint (Ctrl+S).

  4. Go to Project Explorer and open pom.xml under Lab2 project
  5. Click on the Dependencies tab.
  6. Click on the Add button.

  7. Group id: org.apache.camel

  8. Artifact id: camel-jaxb
  9. Version: ${camel-version}

  10. Verify the following dependencies have been automatically added:

  11. Camel-ftp

  12. Camel-dozer
  13. Camel-jackson
  14. Camel-groovy

  15. Save and close the file.

general_info_polished.png 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 )
  1. Run the camel route by right-clicking blueprint.xml, Run As -> “Local Camel Context (without tests)”
  2. Right-click on the InputDir folder and select Import.

  3. If the folder doesn’t appear in the folder tree, refresh it (F5).

  4. Browse to ~/FuseWorkshop/support/Lab2 and select File1.xml.

  5. Click Finish.
  6. Verify that there is a new json file in the FTP server directory.

  7. Open a terminal.

  8. Run: “ftp localhost”.
  9. Press enter for the default user (jboss).
  10. Enter jboss#1! as password.
  11. Run “cd OutputDir”.
  12. Run “ls” to validate there is a json file.
  13. Run “quit” to exit ftp.

  14. Go to ~/FuseWorkshop/OutputDir.

  15. 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.
  16. Go back to JBDS.
  17. Close all the open files.
  18. Stop the camel route by clicking the stop button .

results matching ""

    No results matching ""