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. Python3 学习第十三弹: 模块学习五之pickle与json

    对于python来说,这两个模块是十分实用的两个模块,以一种简单的方法用于储存数据实例. pickle模块 提供用来储存Python各种数据序列化存储 # 原来的cPickle已经在python3中与 ...

  2. IOS UI-键盘处理和UIToolbar

    // // ViewController.m // IOS_0225-键盘处理和UIToolBar // // Created by ma c on 16/2/25. // Copyright © 2 ...

  3. 【hive】where使用注意的问题

    不能再where后边使用别名,group by后边也一样不能使用别名 select id,col1 - col2 from table1 where (col1 - col2) > 1000;  ...

  4. 由浅入深了解EventBus:(三)

    原理 EventBus的核心工作机制如下图 在EventBus3.0架构图: EventBus类 在EventBus3.0框架的内部,核心类就是EventBus,订阅者的注册/订阅,解除注册,以及事件 ...

  5. CF991C

    题解: 很显然不会有那么多种肯能 所以都列出来即可 代码: #include<bits/stdc++.h> using namespace std; int main() { ]; sca ...

  6. PIVOT 和 UNPIVOT 命令的SQL Server版本

    I:使用 PIVOT 和 UNPIVOT 命令的SQL Server版本要求 1.数据库的最低版本要求为 SQL Server 2005 或 更高 2.必须将数据库的兼容级别设置为 90 或 更高 3 ...

  7. codis3.2安装配置中的一些问题

    1.参考文档与参考资料问题 安装codis集群之前,我先在网上找资料,然后又到github的项目官方地址找,不得不说,相关的资料不好找,而且找到之后有些东西说的也不是很清楚.由于codis版本迭代的问 ...

  8. Java发送短信

    1.接口使用介绍 发送短信肯定需要使用第三方接口,Java本身是肯定不能直接发送短信的.第三方接口有很多,这里直接找个正规靠谱一点的学习一下 这里使用了中国网建(http://sms.webchine ...

  9. web运行异常解决

    端口占用: 在dos下,输入  netstat   -ano|findstr  8080 //说明:查看占用8080端口的进程 显示占用端口的进程 taskkill  /pid  6856  /f   ...

  10. New Concept English there (3)

    25words/ minutes Some time ago,an interesting discovery was made by archaeologists on the Aegean isl ...