dilip sundar
dilip sundar

Reputation: 91

How to set default values to stream parameters in siddhi?

Is there anyway I can set a default value to a parameter of a stream in siddhi?

I have an input stream that takes inputs via a curl command.

@source(type='http', receiver.url='http://0.0.0.0:8008/test', @map(type = 'json', @attributes(a='$.a', b='$.b', c='$.c')))

I wish to make the parameter c set a default value as 'test', unless specified otherwise in my cURL command.

Is there any way to achieve this?

Upvotes: 0

Views: 303

Answers (1)

Chiran Fernando
Chiran Fernando

Reputation: 437

You can set fail.on.missing.attribute to false in the JSON mapper and do a null check and assign the value. Refer the official Source JSON Mapper documentation and Siddhi default function you can use to set a default value to the argument if it's null.

Following is a sample Siddhi application for you to refer.

@App:name("ReceiveAndOutput")

@Source(type = 'http',
        receiver.url='http://localhost:8006/productionStream',
        basic.auth.enabled='false',
        @map(type='json',fail.on.missing.attribute='false', @attributes(name='$.item', amount='$.charge')))
define stream SweetProductionStream (name string, amount string);

@sink(type='log')
define stream OutputStream (name string, amount string);

-- Count the incoming events
@info(name='query1')
from SweetProductionStream
select default(name, 'DefaultName') as name, default(amount, '0.0') as amount
insert into OutputStream;

Upvotes: 1

Related Questions