Showing posts with label ESQL. Show all posts
Showing posts with label ESQL. Show all posts

Sunday, March 1, 2020

IBM Integration Bus - Filter Node

Filter Node
The “Filter” node is the simplest of the routing nodes and provides a simple “If-Then-Else” routing mechanism within a Message Flow. The “Filter” node is an ESQL node. The ESQL executed in this node must terminate with an ESQL RETURN statement. This statement must return a Boolean value. The route taken from this node depends upon the value of the RETURN statement.
This node has four output terminals. These terminals are:

  •  Failure (A failure in the ESQL code)
  • True (ESQL code returns a “True” value)
  • False (ESQL code returns a “False” value)
  • Unknown (ESQL code returns a “NULL” value or does not “RETURN” a boolean)
Note: The “Filter” node propagates an unchanged input message to an output terminal. If you are referring to the input message to interrogate its contents in a Database or Filter node, use correlation name Body to refer to the start of the message. Body is equivalent to Root followed by the parser name (for example, Root.XMLNS), which you can use if you prefer. You cannot use the Body correlation name in a DatabaseInput node.
You must use these different correlation names because there is only one message to which to refer in a Database or Filter node; you cannot create an output message in these nodes. Use a Compute node to create an output message.


Example:

CREATE FILTER MODULE HTTPInputMessageFlow_Filter
    CREATE FUNCTION Main() RETURNS BOOLEAN
    BEGIN
        
        DECLARE space1 NAMESPACE 'namespace1';
        IF Body.space1:root.space1:example = 'ABCDE' THEN
            RETURN TRUE;
        ELSE
            RETURN FALSE;
        END IF;
    END;
END MODULE;



Wednesday, February 26, 2020

IBM Integration Bus- DFDL in ESQL

To parse  binary to XML in ESQL:

CREATE LASTCHILD OF OutputRoot DOMAIN('DFDL') PARSE(binaryData ENCODING InputRoot.Properties.Encoding TYPE '{Namespace}:RootElement');

To convert XML to binanry in ESQL:
We have to prepare the DFDL in OutputRoot like:

CREATE LASTCHILD OF OutputRoot DOMAIN('DFDL') NAME 'DFDL';
DECLARE ns NAMESPACE 'Namespace';
SET OutputRoot.DFDL.Namespace:RootElement=...

The Output is auto convert to binary
DECLARE bPayload BLOB ASBITSTREAM(InputRoot.DFDL.HRRecords CCSID InputRoot.Properties.CodedCharSetId ENCODING InputRoot.Properties.Encoding);

IBM Integration Bus- ESQL Code Snippets

ESQL Code Snippets

1. Convert XMLNSC to BLOB 

Sometimes you may need to convert your payload to a BLOB(Binary Large Object). To be able to accomplish this the ASBITSTREAM function has to be used. The ASBITSTREAM function will return a bit stream representation of your payload. When I need to put my payload to MQ queue I convert it to a BLOB.
 DECLARE myChar CHAR CAST(myBLOB AS CHAR CCSID InputRoot.Properties.CodedCharSetId Encoding InputRoot.Properties.Encoding);  

2. Convert BLOB to CHAR

 DECLARE myChar CHAR CAST(myBLOB AS CHAR CCSID InputRoot.Properties.CodedCharSetId Encoding InputRoot.Properties.Encoding);  

3. Convert CHAR to BLOB

 DECLARE myBlob BLOB CAST( myChar AS BLOB CCSID InputRoot.Properties.CodedCharSetId);  

4. Convert BLOB to XMLNSC 

 CREATE LASTCHILD OF OutputRoot.XMLNSC DOMAIN('XMLNSC') PARSE(myBlob, InputRoot.Properties.Encoding, InputRoot.Properties.CodedCharSetId);  

5. Left Padding

 RIGHT('0000000000' || CAST(field AS CHAR),10);  
The above will left zero pad field to a 10 long field.


6. Nil Element & Namespace Declaration

 SET OutputRoot.XMLNSC.ns:myElement.(XMLNSC.NamespaceDecl)xmlns:"xsi" ='http://www.w3.org/2001/XMLSchema-instance';    
 SET OutputRoot.XMLNSC.ns:myElement.(XMLNSC.Attribute)xsi:nil = 'true';   

7. Convert Payload to a String

 DECLARE myBlob BLOB;  
 SET myBlob = ASBITSTREAM(InputRoot.XMLNSC CCSID InputRoot.Properties.CodedCharSetId ENCODING InputRoot.Properties.Encoding);  
 DECLARE myChar CHAR CAST(myBlob AS CHAR CCSID InputRoot.Properties.CodedCharSetId Encoding InputRoot.Properties.Encoding);  

8. Backup a JSON Payload without losing arrays

 CREATE LASTCHILD OF OutputRoot DOMAIN('JSON') TYPE Name NAME 'JSON';  
 CREATE FIELD OutputRoot.JSON.Data IDENTITY(JSON.Object)Data;  
 SET OutputRoot.JSON.Data = InputLocalEnvironment.Backup.Data;   

9.Pass Parameters to an HTTP request Node 

 OutputLocalEnvironment.Destination.HTTP.QueryString.param1 = 'param1';  
Each parameter must be individually set.

10. Convert BLOB to JSON 

 CREATE LASTCHILD OF OutputRoot DOMAIN('JSON') PARSE(InputRoot.BLOB.BLOB);  

11. Select value from an ESQL array 

This statement allows you to select the name without iterating through OutputLocalEnvironment.Variables.Person[] where the Id value matches the nameId.

 SET name = the(select item fieldvalue(r.Name) from OutputLocalEnvironment.Variables.Person[] as r where r.Id = nameId);   

Tuesday, February 25, 2020

IBM Integration Bus - Assign node with SELECT command in ESQL

For Ex, We have the input below.
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>
   Two of our famous Belgian Waffles with plenty of real maple syrup
   </description>
    <calories>650</calories>
</food>
<food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>
    Light Belgian waffles covered with strawberries and whipped cream
    </description>
    <calories>900</calories>
</food>
<food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>
    Belgian waffles covered with assorted fresh berries and whipped cream
    </description>
    <calories>900</calories>
</food>
<food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>
    Thick slices made from our homemade sourdough bread
    </description>
    <calories>600</calories>
</food>
<food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>
    Two eggs, bacon or sausage, toast, and our ever-popular hash browns
    </description>
    <calories>950</calories>
</food>
</breakfast_menu>

We just want create the output with the price of food that have name is "Homestyle Breakfast", We could use the SELECT command  in ESQL like below.

SET OutputRoot.XMLNSC.food.price= THE(SELECT FIELDVALUE(c.price) AS price FROM InputRoot.XMLNSC.breakfast_menu.food[] AS c WHERE c.name ='Homestyle Breakfast');

The SELECT command for querying the food with name is 'Homestyle Breakfast' and return a LIST of matched ROW. The "THE" command with the first element in the LIST and return for assigning to OutputRoot.XMLNSC.food.price
We will have the output like:
<food>
       <price>$6.95</price>
</food>
Without the "THE" command, we have use [] like below
SET OutputRoot.XMLNSC.food[]= (SELECT FIELDVALUE(c.price) AS price FROM InputRoot.XMLNSC.breakfast_menu.food[] AS c WHERE c.name ='Homestyle Breakfast');
The same output:
<food>
       <price>$6.95</price>
</food>

Install and use xorg-server on macOS via Homebrew

  The instructions to install and use xorg-server on macOS via Homebrew: Install Homebrew (if you haven't already): /bin/bash -c ...