12.2. Your First Data Transformation


In this tutorial you will learn how to use the tooling provided by JBoss Fuse to add a data transformation to a predefined Camel route. The Camel route used in this example directs messages from a source endpoint that produces XML data to a target endpoint that consumes JSON data. You will add a data transformation to map the XML data to JSON data in order for this route to work correctly.

Prerequisites

Procedure

  1. Import the starter quickstart application in JBoss Developer Studio.

    1. Select File Import. Expand Maven and select Existing Maven Projects, then select Next.
    2. You are prompted to select a Maven project to import. Select Browse and navigate to the root directory of the starter quickstart application, then select OK. Review the selection and select Finish. The starter application will appear in the Project Explorer.
    3. Expand the starter project in the Project Explorer and navigate to starter/src/main/resources/META-INF/spring/camel-context.xml. Open this file to view the Camel route. By default, the file is presented visually on the Design tab.
      Description
      Select the Source tab to view the underlying XML. You can see that an XML file is produced from a source endpoint and a JSON file is consumed by a target endpoint.
  2. Add a data transformation to the Camel route.

    1. Select the Design view for the camel-context.xml file.
    2. Remove the arrow connecting the two endpoints.
      Note
      Do not save your project at this point. If you save your project now, the endpoints will disappear.
    3. Click on the Transformation drawer in the Palette. Drag a Data Transformation endpoint onto the canvas.
      Description
      This will invoke the New Transformation wizard. If the Palette is not yet visible, select Window Show View Other, then expand General and select Palette.
    4. In the New Transformation wizard, set the following values (leaving all other default values) then select Next.
      Field Value
      Transformation ID xml2json
      Source Type XML
      Target Type JSON
    5. Now you are prompted to provide the source type definition. You can do this by supplying either an XML Schema (default) or an example XML Instance Document. Leave the default option and browse to select a Source File. Choose abc-order.xsd, then select OK. You are provided with a preview of the XML. Select Next to continue.
    6. Now you are prompted to describe the target type definition in the same way. Browse to select a Target File. Choose xyz-order.json, then select OK. Because this is an example file, the JSON Type Definition is updated automatically to reflect this. Select Finish.
  3. You are now presented with the transformation editor with source fields on the left and target fields on the right. Map one set to the other.

    1. Expand all of the fields on both sides of the transformation editor.
    2. Drag the source customerNum field from the left onto the target custId field on the right. The transformation (or mapping) will appear in the center.
      Description
      Continue to map the fields as follows:
      Source Target
      orderNum orderId
      status priority
      item lineItems
      id itemId
      price cost
      quantity amount
  4. Map any remaining fields.

    1. Click on Hide mapped fields for both source and target to determine whether all fields have been mapped.
      Description
      You will find that the target approvalCode and origin fields remain unmapped.
      Description
    2. Map the origin field to a constant variable: click on the Variables tab on the Source side. Select Add a new variable.
      Description
    3. You are prompted to Enter a new variable name. Enter ORIGIN and select OK. The variable is created and appears on the left in the Variables window.
    4. Select the default value and change it to Web.
      Description
    5. Drag the new variable onto the target origin field.
      Description
    6. Associate the approvalCode field with a dynamic evaluation of a Camel language expression: select Add a new mapping.
      Description
      An empty transformation is added.
      Description
    7. Drag approvalCode from the target list into the target box of the empty transformation. You will notice that the selected transformation also appears in a window below. When you move your cursor over the source field in this view, an arrow appears. Select this arrow to open the menu from which you select Set expression.
      Description
    8. You are prompted to select the expression language, and to enter an expression. Set the following values, then select OK:
      Field Value
      Language Header
      Expression ApprovalID
  5. Connect the endpoints.

    1. Select the camel-context.xml tab to focus on the Camel route again.
    2. When you hover your cursor over each endpoint, you are presented with an option to Create connection.
      Description
      Create a connection from the source endpoint to the transformation endpoint and then from the transformation endpoint to the target endpoint. Save your changes. The endpoints are automatically arranged horizontally.
      Description
  6. Test your data transformation.

    1. Select File New Other. Expand Fuse Tooling and select Fuse Transformation Test, then select Next. This will invoke the New Transformation Test wizard.
    2. In the New Transformation Test wizard, set the following values:
      Field Value
      Package example
      Transformation ID xml2json
    3. In the Project Explorer navigate to starter/src/test/java/example and then open the TransformationTest.java file.
    4. Add the following code to the transform method:
      startEndpoint.sendBodyAndHeader(readFile("src/data/abc-order.xml"), "approvalID", "AUTO_OK");
    5. Ensure the TransformationTest.java file has focus, then select Run Run As JUnit Test.
    6. The output will be presented in the Console window. Here you can see both the source XML and the resulting target JSON.

      Example 12.1. Source XML

      <ABCOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com">
          <header>
              <status>GOLD</status>
              <customer-num>ACME-123</customer-num>
              <order-num>ORDER1</order-num>
          </header>
          <order-items>
              <item id="PICKLE">
                  <price>2.25</price>
                  <quantity>1000</quantity>
              </item>
              <item id="BANANA">
                  <price>1.25</price>
                  <quantity>400</quantity>
              </item>
          </order-items>
      </ABCOrder>

      Example 12.2. Target JSON

      {"custId":"ACME-123","priority":"GOLD","orderId":"ORDER1","origin":"Web","approvalCode":"AUTO_OK","lineItems":[{"itemId":"PICKLE","amount":1000,"cost":2.25},{"itemId":"BANANA","amount":400,"cost":1.25}]}
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.