无论如何我可以为siddhi中的流参数设置默认值吗?
我有一个通过 curl 命令获取输入的输入流。
@source(type='http', receiver.url='http://0.0.0.0:8008/test', @map(type = 'json', @attributes(a='$.a', b='$.b', c='$.c')))
我希望让参数 c 将默认值设置为“test”,除非在我的 cURL 命令中另有指定。
有什么办法可以做到这一点?
您可以在 JSON 映射器中将fail.on.missing.attribute设置为 false 并执行 null 检查并分配值。请参阅官方 Source JSON Mapper 文档和Siddhi 默认函数,如果参数为空,您可以使用它来设置参数的默认值。
以下是一个示例 Siddhi 应用程序供您参考。
@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;