码迷,mamicode.com
首页 > 其他好文 > 详细

ballerina 学习十 streams

时间:2018-05-19 20:40:58      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:应用   single   publish   group by   包含   str   sele   proc   简单   

ballerina 的streams 使用的是siddhi complex event processing 引擎处理,可以包含的语法有
projection filtering windows join pattern

简单例子

  • 代码
import ballerina/io;
import ballerina/runtime;
type StatusCount {
string status;
int totalCount;
};

type Teacher {
string name;
int age;
string status;
string batch;
string school;
};
function testAggregationQuery(
stream<StatusCount> filteredStatusCountStream,
stream<Teacher> teacherStream) {
forever {

//  cep  处理并发布结果
from teacherStream where age > 18 window lengthBatch(3)
select status, count(status) as totalCount
group by status
having totalCount > 1
=> (StatusCount[] status) {
filteredStatusCountStream.publish(status);
}
}
}

function main(string… args) {
stream<StatusCount> filteredStatusCountStream;

stream<Teacher> teacherStream;

testAggregationQuery(filteredStatusCountStream, teacherStream);

Teacher t1 = {name: "Sam", age: 25, status: "single",
batch: "LK2014", school: "Hampden High School"};
Teacher t2 = {name: "Jordan", age: 33, status: "single",
batch: "LK1998", school: "Columbia High School"};
Teacher t3 = {name: "Morgan", age: 45, status: "married",
batch: "LK1988", school: "Central High School"};

filteredStatusCountStream.subscribe(printStatusCount);

// 生产数据
teacherStream.publish(t1);
teacherStream.publish(t2);
teacherStream.publish(t3);

runtime:sleep(1000);
}
function printStatusCount(StatusCount s) {
io:println("Event received; status: " + s.status +
", total occurrences: " + s.totalCount);
}
  • 输出结果
Event received; status: single, total occurrences: 2

ballerina 学习十 streams

标签:应用   single   publish   group by   包含   str   sele   proc   简单   

原文地址:https://www.cnblogs.com/rongfengliang/p/9061462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!