Map

Provides examples on basic map functions provided via siddhi-execution-map extension.

For information of performing scatter and gather using map:tokenize(), and map:collect() refer the examples in Data Pipelining section. For information on all map functions, refer the Siddhi APIs.

define stream CoupleDealInfoStream (
                 item1 string, price1 double,
                 item2 string, price2 double);

@info(name = 'Create-map')
from CoupleDealInfoStream
select map:create(item1, price1, item2, price2)
            as itemPriceMap
insert into NewMapStream;

@info(name = 'Check-map')
from NewMapStream
select map:isMap(itemPriceMap) as isMap,
       map:containsKey(itemPriceMap, 'Cookie')
            as isCookiePresent,
       map:containsValue(itemPriceMap, 24.0)
            as isThereItemWithPrice24,
       map:isEmpty(itemPriceMap) as isEmpty,
       map:keys(itemPriceMap) as keys,
       map:size(itemPriceMap) as size
insert into MapAnalysisStream;

@info(name = 'Clone-and-update')
from NewMapStream
select map:replace(
                   map:put(map:clone(itemPriceMap),
                           "Gift",
                           1.0),
                   "Cake",
                   12.0) as itemPriceMap
insert into ItemInsertedMapStream;
define stream CoupleDealInfoStream (
                 item1 string, price1 double,
                 item2 string, price2 double);

Defines CoupleDealInfoStream having attributes item1, price1, item2, and price2 with string and double types.

@info(name = 'Create-map')
from CoupleDealInfoStream
select map:create(item1, price1, item2, price2)
            as itemPriceMap

Create a map with values of item1 and item2 as keys, and price1 and price2 as values.

insert into NewMapStream;
@info(name = 'Check-map')
from NewMapStream
select map:isMap(itemPriceMap) as isMap,

Check if itemPriceMap is a Map.

       map:containsKey(itemPriceMap, 'Cookie')
            as isCookiePresent,

Check if itemPriceMap contains a key 'Cookie'.

       map:containsValue(itemPriceMap, 24.0)
            as isThereItemWithPrice24,

Check if itemPriceMap contains a value 24.0.

       map:isEmpty(itemPriceMap) as isEmpty,

Check if itemPriceMap is empty.

       map:keys(itemPriceMap) as keys,

Get all keys of itemPriceMap as a List.

       map:size(itemPriceMap) as size

Get size of itemPriceMap.

insert into MapAnalysisStream;
@info(name = 'Clone-and-update')
from NewMapStream
select map:replace(
                   map:put(map:clone(itemPriceMap),
                           "Gift",
                           1.0),
                   "Cake",
                   12.0) as itemPriceMap

Clone itemPriceMap, put Gift key with value 1.0 to it, and replace Cake key with value 12.0.

insert into ItemInsertedMapStream;

Input

Below event is sent to CoupleDealInfoStream,

['Chocolate', 18.0, 'Ice Cream', 24.0]

Output

After processing, the following events will be arriving at each stream:

  • NewMapStream:
    [{Ice Cream=24.0, Chocolate =18.0}]
  • MapAnalysisStream:
    [true, false, true, false, [Ice Cream, Chocolate], 2]
  • ItemInsertedMapStream:
    [{Ice Cream=24.0, Gift=1.0, Chocolate =18.0}]
Top