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

stream join

  • 参考代码

代码就是进行通过http 获取的到数据进行流化,同时进行join 并对于符合业务的数据进行报警

import ballerina/http;
import ballerina/mime;
import ballerina/io;
type ProductMaterial {
string name;
float amount;
};
type MaterialUsage {
string name;
float totalRawMaterial;
float totalConsumption;
};
stream<ProductMaterial> rawMaterialStream;
stream<ProductMaterial> productionInputStream;
stream<MaterialUsage> materialUsageStream;
function initRealtimeProductionAlert() {
materialUsageStream.subscribe(printMaterialUsageAlert);
forever {
from productionInputStream window time(10000) as p
join rawMaterialStream window time(10000) as r
on r.name == p.name
select r.name, sum(r.amount) as totalRawMaterial,
sum(p.amount) as totalConsumption
group by r.name
having ((totalRawMaterial - totalConsumption) * 100.0 /
totalRawMaterial) > 5
=> (MaterialUsage[] materialUsages) {
materialUsageStream.publish(materialUsages);
}
}
}
function printMaterialUsageAlert(MaterialUsage materialUsage) {
float materialUsageDifference = (materialUsage.totalRawMaterial -
materialUsage.totalConsumption) * 100.0 /
(materialUsage.totalRawMaterial); io:println("ALERT!! : Material usage is higher than the expected"
+ " limit for material : " + materialUsage.name +
" , usage difference (%) : " + materialUsageDifference);
}
endpoint http:Listener productMaterialListener {
port: 9090
};
@http:ServiceConfig {
basePath: "/"
}
service productMaterialService bind productMaterialListener {
future ftr = start initRealtimeProductionAlert();
@http:ResourceConfig {
methods: ["POST"],
path: "/rawmaterial"
}
rawmaterialrequests(endpoint outboundEP, http:Request req) {
var jsonMsg = req.getJsonPayload();
io:println(jsonMsg);
match jsonMsg {
json msg => {
var productMaterial = check <ProductMaterial>msg;
rawMaterialStream.publish(productMaterial);
http:Response res = new;
res.setJsonPayload({"message": "Raw material request"
+ " successfully received"});
_ = outboundEP->respond(res);
}
error err => {
http:Response res = new;
res.statusCode = 500;
res.setPayload(err.message);
_ = outboundEP->respond(res);
}
}
}
@http:ResourceConfig {
methods: ["POST"],
path: "/productionmaterial"
}
productionmaterialrequests(endpoint outboundEP,http:Request req) {
var jsonMsg = req.getJsonPayload();
match jsonMsg {
json msg => {
var productMaterial = check <ProductMaterial>msg;
productionInputStream.publish(productMaterial);
http:Response res = new;
res.setJsonPayload({"message": "Production input " +
"request successfully received"});
_ = outboundEP->respond(res);
}
error err => {
http:Response res = new;
res.statusCode = 500;
res.setPayload(err.message);
_ = outboundEP->respond(res);
}
}
}
}

用途

对于事件处理的应用特别方便,比如日志处理,以及响应式系统开发,其中的siddhi 也是一个很不错的工具

参考资料

https://ballerina.io/learn/by-example/hello-world-streams.html

 
 
 
 

ballerina 学习十 streams的更多相关文章

  1. ballerina 学习十九 安全编程

      ballerina 内部提供了几种常用的安全开发模型,token 认证(jwt) basic auth jwt 安全 参考代码 import ballerina/http; http:AuthPr ...

  2. ballerina 学习十八 事务编程

    事务在分布式开发,以及微服务开发中是比较重要的 ballerina 支持 本地事务.xa 事务.分布式事务 ,但是具体的服务实现起来需要按照ballerian 的事务模型 infection agre ...

  3. ballerina 学习十六 错误&&异常处理

    ballerina 的error 处理和elxiir 以及rust 比较类似使用模式匹配,但是他的 error lifting 还是比较方便的 同时check 也挺好,异常处理没什么特殊的 throw ...

  4. ballerina 学习十五 控制流

    ballerina 的控制流没有什么特殊,只是相比一般语言多了一个模式匹配的操作match ,实际上其他语言(erlang elixir rust 中的模式匹配是很强大的) 简单例子 if/else ...

  5. ballerina 学习十四 values && types

    ballerina 包含的数据类型有string int map array record boolean ojbect function table tuple any 简单说明 数据类型和其他语言 ...

  6. ballerina 学习十二 变量

    ballerina 有两种方式进行变量的定义,类型加上名称以及初始值.,使用var 关键字 简单例子 代码 import ballerina/io; // 全局public 变量,使用类型定义 pub ...

  7. 强化学习(十九) AlphaGo Zero强化学习原理

    在强化学习(十八) 基于模拟的搜索与蒙特卡罗树搜索(MCTS)中,我们讨论了MCTS的原理和在棋类中的基本应用.这里我们在前一节MCTS的基础上,讨论下DeepMind的AlphaGo Zero强化学 ...

  8. 强化学习(十六) 深度确定性策略梯度(DDPG)

    在强化学习(十五) A3C中,我们讨论了使用多线程的方法来解决Actor-Critic难收敛的问题,今天我们不使用多线程,而是使用和DDQN类似的方法:即经验回放和双网络的方法来改进Actor-Cri ...

  9. 强化学习(十五) A3C

    在强化学习(十四) Actor-Critic中,我们讨论了Actor-Critic的算法流程,但是由于普通的Actor-Critic算法难以收敛,需要一些其他的优化.而Asynchronous Adv ...

随机推荐

  1. vs 2017 保存文件 utf8

    vs 2017 保存文件 utf8 转自:https://blog.csdn.net/jiegemena/article/details/79369650

  2. Java集合详解3:Iterator,fail-fast机制与比较器

    Java集合详解3:Iterator,fail-fast机制与比较器 今天我们来探索一下LIterator,fail-fast机制与比较器的源码. 具体代码在我的GitHub中可以找到 https:/ ...

  3. psd-面试-dp/LCS

    链接:https://www.nowcoder.com/acm/contest/90/D来源:牛客网 掌握未来命运的女神 psd 师兄在拿了朝田诗乃的 buff 后决定去实习. 埃森哲公司注册成立于爱 ...

  4. hack games

    记下,有时间玩玩~ wargame http://www.wechall.net/lang_ranking/en --------------- Monyer系列(黑客游戏) 1. http://mo ...

  5. Ansible 小手册系列 十一(变量)

    变量名约束 变量名称应为字母,数字和下划线. 变量应始终以字母开头. 变量名不应与python属性和方法名冲突. 变量使用 通过命令行传递变量(extra vars) ansible-playbook ...

  6. 【http】HTTP请求方法 之 OPTIONS

    OPTIONS方法是用于请求获得由Request-URI标识的资源在请求/响应的通信过程中可以使用的功能选项.通过这个方法,客户端可以在采取具体资源请求之前,决定对该资源采取何种必要措施,或者了解服务 ...

  7. 20165202 2017-2018-2《Java程序设计》课程总结

    每周作业链接汇总 ++预备作业一:我期待的师生关系++ ++预备作业二:学习基础和C语言基础调查++ ++预备作业三:linux安装及学习++ ++第一周作业:初识JAVA,注册码云并配置Git++ ...

  8. New Concept English three (22)

    34w 54 Some plays are so successful that they run for years on end. In many ways, this is unfortunat ...

  9. 浅谈title属性与alt属性

    XHTML是CSS布局的基础,webjx.com一直强调XHTML知识的学习,重视语义和文档的结构.title 和alt 属性,给我最直观的感受就是,可以提高文档的适应性,并合理提高关键词密度.在XH ...

  10. vue.js 源代码学习笔记 ----- instance proxy

    /* not type checking this file because flow doesn't play well with Proxy */ import config from 'core ...