ballerina 学习十 streams
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的更多相关文章
- ballerina 学习十九 安全编程
ballerina 内部提供了几种常用的安全开发模型,token 认证(jwt) basic auth jwt 安全 参考代码 import ballerina/http; http:AuthPr ...
- ballerina 学习十八 事务编程
事务在分布式开发,以及微服务开发中是比较重要的 ballerina 支持 本地事务.xa 事务.分布式事务 ,但是具体的服务实现起来需要按照ballerian 的事务模型 infection agre ...
- ballerina 学习十六 错误&&异常处理
ballerina 的error 处理和elxiir 以及rust 比较类似使用模式匹配,但是他的 error lifting 还是比较方便的 同时check 也挺好,异常处理没什么特殊的 throw ...
- ballerina 学习十五 控制流
ballerina 的控制流没有什么特殊,只是相比一般语言多了一个模式匹配的操作match ,实际上其他语言(erlang elixir rust 中的模式匹配是很强大的) 简单例子 if/else ...
- ballerina 学习十四 values && types
ballerina 包含的数据类型有string int map array record boolean ojbect function table tuple any 简单说明 数据类型和其他语言 ...
- ballerina 学习十二 变量
ballerina 有两种方式进行变量的定义,类型加上名称以及初始值.,使用var 关键字 简单例子 代码 import ballerina/io; // 全局public 变量,使用类型定义 pub ...
- 强化学习(十九) AlphaGo Zero强化学习原理
在强化学习(十八) 基于模拟的搜索与蒙特卡罗树搜索(MCTS)中,我们讨论了MCTS的原理和在棋类中的基本应用.这里我们在前一节MCTS的基础上,讨论下DeepMind的AlphaGo Zero强化学 ...
- 强化学习(十六) 深度确定性策略梯度(DDPG)
在强化学习(十五) A3C中,我们讨论了使用多线程的方法来解决Actor-Critic难收敛的问题,今天我们不使用多线程,而是使用和DDQN类似的方法:即经验回放和双网络的方法来改进Actor-Cri ...
- 强化学习(十五) A3C
在强化学习(十四) Actor-Critic中,我们讨论了Actor-Critic的算法流程,但是由于普通的Actor-Critic算法难以收敛,需要一些其他的优化.而Asynchronous Adv ...
随机推荐
- VC中release和debug版本的区别
DEBUG版本 包含调试信息,编译器生成的代码方便调试 RELEASE版本 算是发布版本,RELEASE下不能调试,生成的文件更小,编译器生成的程序速度更快但是Debug下生成的exe文件在单独运行时 ...
- [洛谷U62364]三次函数极值
U62364 三次函数极值 题面 给定一个三次函数\(f(x)=a_3x^3+a_2x^2+a_1x+a_0\) 求其极值. 格式 输入包括一行四个整数\(a_3,a_2,a_1,a_0\) 输出包括 ...
- Spring Boot配置ContextPath【从零开始学Spring Boot】
Spring boot默认是/ ,这样直接通过http://ip:port/就可以访问到index页面,如果要修改为http://ip:port/path/ 访问的话,那么需要在Application ...
- 005-对象——对象的 final const
<?php /** * */ /*class shouji { public $pinpai; final function chongdian() { //final 最终的 return $ ...
- MySQL数据中分级分组显示数据
前面已经有了SqlServer数据分级分组显示数据了.今天又来做一个MySQL数据库中的分级分组显示,SqlServer中用到了递归,这里为了简单就直接把根的数据显示为0 ,而不用递归了. 在MySQ ...
- Mysqlde的权限操作,以及增加用户
增加用户及直接授权 ' 在这条命令里边all代表所有的权限,*.*代表所有的空间名.表名 sql的通配符 _ 代表任意的一个字符 % 代表任意的字符的任意长度 修改用户的密码 update 空间名.表 ...
- js常用正则表达式,滚蛋吧!你们测试组bug,让你挑
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- tsl/ssl 证书制作记录
生成自签名证书 生成服务端秘钥 $ openssl genrsa -out server.key 1024 生成证书请求文件 编写配置文件openssl.cnf $ vi openssl.cnf [r ...
- vector概念
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 如何规划和选择数据库服务器:CPU、内存、磁盘、网络(转)
转自:http://blog.chinaunix.net/uid-5715-id-2734517.html 学习如何根据业务模型来计算tpcc值,挺有帮助的. 当一个新的业务系统开发完成后,需要在一个 ...