What is a Hashmap?

A hashmap, or a hash table, is a data structure which stores data in the form of Key-Value pairs. By looking up for a key, the corresponding value against that key can be retrieved. How it works? It transforms the key into a hash, using a hash function. A hash is nothing but a number which serves as an index to locate the record in the hashmap, from where the corresponding value against that key can be retrieved.

Typically, a Hashmap support insert, delete and lookup functions. Lookup is the searching and locating of a desired record, fetched against a given key. It finds significant use in extremely large data sets wherein a hashmap makes lookup extremely fast and efficient.

 

HashMaps in CPI

SAP Cloud Platform Integration (CPI) supports the use of Hashmap variables through the HashMap class which is available and can be used in scripts. By using these variables, data available in (for example) XML nodes can be stored in HashMap, and later retrieved through a lookup.

 

The CPI already does offer the “Content Modifier” which also helps in storing such data and retrieving subsequently. However, the Hashmap offers the following advantages:

 

·        Offers easy storing of values against a key

·        Can be stored in data stores and can thus be accessible across different i-Flows.

·        The “key” can be a combination of multiple (concatenated) values, thereby offering multiple values to be used as a key.

·        Retrieval (Lookup) is fast and efficient. Should be considered for extremely high data volumes.

 

Implementation

 

Let us have a quick look at an example of how these hashmap variables can be used in a simple groovy script in CPI to store data and subsequently retrieve it.

For the purpose of this document, we will assume a simple XML payload body as a message. This message will have the data which will be looped in a groovy script and stored in hashmap variables. Then the data will be retrieved from this hashmap.

 

To start, create an integration flow and add a small content modifier. The content modifier can contain the following XML data in the Message Body.

 

<root>

<person>

<id>0001</id>

<firstname>Jack</firstname>

<lastname>Sparrow</lastname>

</person>

<person>

<id>0002</id>

<firstname>Tony</firstname>

<lastname>Stark</lastname>

</person>

</root>

 

Also add a property in the Content Modifier. This will help to retrieve values from the hashmap and easy monitoring.

 

Now, after the content modifier, add a script in the I-flow. The code snippet to be used in the groovy script is mentioned below:

 

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import java.io.*;

 

def Message processData(Message message) {

           

            def body = message.getBody(java.io.Reader);

            HashMap<Integer, String> hmap = new HashMap<Integer, String>();

            def Root = new XmlSlurper().parse(body);

    Root.person.each{

        try{

        hmap.put(it.id.text().toString(), it.firstname.text().toString());

        }

        catch(Exception ex){

            //put relevant exception handling here

        }

    }

            message.setProperty("hashmapdata", hmap);

            return message;

}

The HashMap class is used to declare the hashmap object. The following line in the script

               HashMap<Integer, String> hmap = new HashMap<Integer, String>();        

declares the hashmap. It takes a key-value pair, the key being an integer type and the value being a string.

 

Later, the xml payload is looped using the “Each” keyword. Within this loop, the value of the “id” and the “firstname” tags from the xml payload are put into the hashmap variable.

 

In the end of the code, the property declared earlier as part of the content modifier, is populated with the content of the hashmap. This is only to verify that our data is correctly stored in the hashmap. We copy the content of the hashmap into this property and retrieve in a subsequent script in the form of an attachment (shown below).

 

 

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

 

def Message processData(Message message) {

 

def map = message.getProperties();

def body = message.getBody(java.lang.String) as String;

 

String hashmap = map.get("hashmapdata");

 

def messageLog = messageLogFactory.getMessageLog(message);

                                    messageLog.addAttachmentAsString("Payload post hashmap:", hashmap, "text/xml");

       return message;

}

 

Save the i-flow. Deploying it at this stage will show us an attachment with the content of the property, which in-turn stores the content of the hashmap variable.

 

As can be seen in the above screenshot, the key-value pair of ID and firstname from the xml message have been retrieved in the attachment from the hashmap.

 

We will now see how we can lookup the hashmap in the script code itself. This can be done with the “get” method of the hashmap class.

Modify the code in the second groovy script as follows:

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import groovy.xml.*;

def Message processData(Message message) {

    def body = message.getBody(java.lang.String);

            def messageLog = messageLogFactory.getMessageLog(message);

            map = message.getProperties();

            Map<Integer,String> hmap1 = map.get("hashmapdata");

 

//now retrieve value from hashmap

    String id  = '0001';

    String val = new String(hmap1.get(Integer.parseInt(id)));

    messageLog.addAttachmentAsString("FinalOutput:", val, "text/plain");  

            return message;

}

In the above example, we retrieve the value from the hashmap variable by using a simple key value 0001. Here, this is a simple retrieval just to show that from the “key-value” pair of the hashmap, the value is retrieved by simply specifying the key in the “get” method. The same can be used to get values in more complicated/real-life business scenario as well. The output of the above script is as below:

 

This was a simple example to demonstrate the use of hashmap variables in CPI.