Skip to main content

Get Countries and split

Introduction#

After Logging the countries to the Execution Log you notice that the output is not so friendly to the eyes. That is where the Splitter comes into play. The Splitter component processes a message and breaks it apart into pieces to process the messages individually. We will also use an aggregation strategy to help display the returned output clearly in the Execution Logs. That way, when VisionsCorp looks at the returned data, they can visually filter through to see what they need quickly.

Integration Ingredients:#

  • GET method (created in a previous lesson)
  • SQL (created in the last lesson)
  • Splitter
  • Process
  • Marshal
  • Log

Goals:#

  • Learn how to use a Splitter
  • Learn how to use an Aggregation Strategy
  • Marshal into JSON
  • Combine the knowledge from the previous lessons to create a larger integration

Step 1: Update the previous integration#

Docusaurus

  • Continuing from the previous lesson, stop running the integration
  • Remove the Log from the route
  • Add a Splitter component
  • For the LANGUAGE choose Simple
  • USE AGGREGATION STRATEGY → Checked ☑️
  • AGGREGATION STRATEGY → CountriesAggregationStrategy
  • Now let us create that resource

Step 2: Creating an Aggregation Strategy#

  • In the Explorer, scroll down until you see Source Definitions
  • Clicking Source Definitions will be presented with a modal asking for a name, add CountriesAggregationStrategy
  • Description → *anything
  • Filename→ CountriesAggregationStrategy.java
  • You are prompted to add code, place this code inside
import org.apache.camel.AggregationStrategy;
import org.apache.camel.BindToRegistry;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
// simply combines Exchange String body values using '; ' as a delimiter
public class CountriesAggregationStrategy extends RouteBuilder implements AggregationStrategy {
@BindToRegistry("CountriesAggregationStrategy")
public CountriesAggregationStrategy getCountriesAggregationStrategy() {
return this;
}
@Override
public void configure() throws Exception {
}
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
//Get Current Record
Map<String, Object> newCountry = newExchange.getIn().getBody(Map.class);
//Add very first Country
if (oldExchange == null) {
Map<Integer, Map<String, Object>> newBody = new HashMap();
newBody.put((Integer) newCountry.get("id"), newCountry);
newExchange.getIn().setBody(newBody);
return newExchange;
}
//Get Current Aggregation Result
Map<Integer, Map> currentResult = oldExchange.getIn().getBody(Map.class);
Map<String, Object> oldCountry = null;
//Check if Country Has been Added
Integer newCountryId = (Integer) newCountry.get("id");
if (currentResult.containsKey(newCountryId)) {
oldCountry = currentResult.get(newCountryId);
}
if (oldCountry != null) {
//Update existing Country
List<Map<String, Object>> oldCities = (List<Map<String, Object>>) oldCountry.get("cities");
oldCities.addAll((List<Map<String, Object>>) newCountry.get("cities"));
oldCountry.put("cities", oldCities);
currentResult.put(newCountryId, oldCountry);
} else {
//Add new Country
currentResult.put(newCountryId, newCountry);
}
//Update Exchange Result
oldExchange.getIn().setBody(currentResult);
return oldExchange;
}
}
  • Save the Aggregation Strategy

Step 3: Add the Processes#

  • Add 2 Process components under the Splitter. This will case the route to branch
  • In the Process under the Route branch add this code to the top section:
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
  • And this code in the bottom section:
Map<String, Map<String, Object>> aggregatedData = exchange.getIn().getBody(Map.class);
List<Map<String, Object>> countries = new ArrayList<>();
for (Map.Entry<String, Map<String, Object>> entry : aggregatedData.entrySet()) {
Map<String, Object> countryData = entry.getValue();
Map<String, Object> country = new HashMap<>();
country.put("id", entry.getKey());
country.put("name", countryData.get("name"));
country.put("cities", countryData.get("cities"));
countries.add(country);
}
exchange.getIn().setBody(countries);
  • In the Process under the Split branch add this code to the top section:
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
  • And this code in the bottom section:
//get current iteration's body
Map<String, Object> sqlRow = exchange.getIn().getBody(Map.class);
// Create a country object
Map<String, Object> country = new HashMap<>();
country.put("name", sqlRow.get("country_name"));
country.put("id", sqlRow.get("country_id"));
//Create a city Object
Map<String, Object> city = new HashMap<>();
city.put("name", sqlRow.get("city_name"));
city.put("id", sqlRow.get("city_id"));
//Add City to Country as List
List<Map<String, Object>> cities = new ArrayList<>();
cities.add(city);
country.put("cities", cities);
exchange.getIn().setBody(country);
  • Under the route’s Process, add a To JSON COMPONENT
  • Under the Split’s Process, add a Log component
  • For the MESSAGE put ${body}
  • Run the integration
  • Open the Execution Logs
  • After the integration says INFO [io.quarkus] (main) Installed features: [ use Postman to shoot the GET request used in the first lesson
  • View the logs and see the output

🎉 Congratulations you have successfully used your first Aggregation Strategy in Jetic! Notice how much cleaner the output of the logs look🎉