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. 【程序员笔试面试必会——排序②】Python实现 计数排序、基数排序

    一.计数排序 概要: 时间复杂度O(n),空间复杂度O(k),k是输入序列的值的范围(最大值-最小值),是稳定的.计数排序一般用于已知输入值的范围相对较小,比如给公司员工的身高体重信息排序. 思路: ...

  2. ls/vi等 command not found

    输入一下命令即可 export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin source / ...

  3. ana3+opencv+TensorFlow+NVIDIAGPU 安装

    http://blog.csdn.net/qq_30611601/article/details/79067982 这个博客写的挺完整的 当你发现你的anna下载的贼鸡儿的慢,你就需要使用清华的镜像网 ...

  4. python sort与sorted使用笔记

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...

  5. configParse模块

    一.配置文件简介 在各种程序里面都有配置文件,为了对配置文件进行操作. python中引入了configParse模块进行操作. 配置数值类型: 配置文件中,我们看到的bool型,整数型,在我们操作的 ...

  6. C++:tinyxml的使用

    1. 简介 TinyXML2(最新版本)是一个开源的功能齐全的XML解析库 For C++,源码见:github. 2. 开始使用 首先从Github上获得源码,是一个完整的演示工程,我们只需要其中的 ...

  7. GSpan-频繁子图挖掘算法

    GSpan频繁子图挖掘算法,网上有很多相关的介绍,中文的一些资料总是似是而非,讲的不是很清楚(感觉都是互相抄来抄去,,,基本都是一个样,,,),仔细的研读了原论文后,在这里做一个总结. 1. GSpa ...

  8. 求小于等于k长度的最大区间和

    题意 给出一个序列,求长度小于等于k的最大区间和并输出起点和终点 1<=n<=100000 1<=k<=n   题解:先算出前缀和,利用单调队列的性质,在单调队列中存储sum[ ...

  9. New Concept English three(14)

    06:55:29 32w/m 45words. There was a time when the owners of shop and businesses in Chicago had to pa ...

  10. C#设置System.Net.ServicePointManager.DefaultConnectionLimit,突破Http协议的并发连接数限制

    在Http协议中,规定了同个Http请求的并发连接数最大为2. 这个数值,可谓是太小了. 而目前的浏览器,已基本不再遵循这个限制,但是Dot Net平台上的 System.Net 还是默认遵循了这个标 ...