zoukankan      html  css  js  c++  java
  • kuiper流式计算完整实例演示

    背景

    前面文章分享了如何安装kuiper和kuiper-manager,本篇文章通过一个完整的例子来演示kuiper的一个比较完整的流式计算。

    下图仍旧使用了kuiper官网文档中的图,我在里面稍微加了一些注释:

    流式计算创建操作过程

    kuiper的流式计算创建于操作分为如下几个步骤:

    1. 使用命令行/rest/控制台创建一个流(对应sources)
    2. 基于创建的流编写路由规则(对应sql/rule部分)
    3. 使用mqtt工具给mqtt broker发送消息(上文中kuiper使用emqx作为其mqtt broker)
    4. kuiper将符合路由规则的数据转发到目的地(sinks)

    创建一个流

    1)docker命令行方式

    # 进入kuiper容器
    
    docker exec -it kuiper /bin/bash
    
    # 创建一个流,定义了temperature和humidity这两个字段,后面会对应mqtt消息(payload)的两个字段,其中DataSource可以理解为订阅的topic
    bin/kuiper create stream demo2 '(temperature float, humidity bigint) WITH (FORMAT="JSON", DATASOURCE="demo2")'

    2) rest方式

    #-d指定了stream的具体数据
    curl -H "Content-Type: application/json" -X POST -d '{"sql":"create stream demo2 (temperature float, humidity bigint) WITH ( datasource = "demo2",FORMAT = "json")"}' http://localhost:9081/streams

    创建规则

    1)docker命令行

    #编写rule规则文件myRule,过滤temperature>30的数据,并输出到log里面(使用了kuiper的log插件),内容如下
    {
        "sql": "SELECT temperature from demo2 where temperature > 30",
        "actions": [{
            "log":  {}
        }]
    }
    
    #命令行创建规则,指定ruleid为ruleDemo
    bin/kuiper create rule ruleDemo -f myRule

    2) rest方式

    curl  -H "Content-Type: application/json"  -X POST -d '{"id":"ruleDemo","sql":"SELECT temperature from demo2 where temperature > 30","actions":[{"log":{}}]}'  http://localhost:9081/rules

    发送mqtt消息给emqx

    #给emqx(192.168.200.2)发送{"temperature": 40, "humidity" : 20},指定topic为demo2
    mosquitto_pub -h 192.168.200.2 -m '{"temperature": 40, "humidity" : 20}' -t demo2

    查看规则过滤后的数据

     

     以上就是kuiper流式计算的例子。总结一下:kuiper运行在某个边缘设备(这里是一台虚机)上,订阅了emqx的topic:demo2,当有{"temperature": 40, "humidity" : 20}这样的数据上报的时候,kuiper的规则引擎会通过sql将数据输出到log中。

    博主:测试生财(一个不为996而996的测开码农)

    座右铭:专注测试开发与自动化运维,努力读书思考写作,为内卷的人生奠定财务自由。

    内容范畴:技术提升,职场杂谈,事业发展,阅读写作,投资理财,健康人生。

    csdn:https://blog.csdn.net/ccgshigao

    博客园:https://www.cnblogs.com/qa-freeroad/

    51cto:https://blog.51cto.com/14900374

    微信公众号:测试生财(定期分享独家内容和资源)

  • 相关阅读:
    django 如何重用app
    vim常用命令
    linux find grep
    linux su su-的区别
    linux定时任务crontab
    linux shell的单行多行注释
    python字符串的截取,查找
    gdb调试
    python字符转化
    python读写文件
  • 原文地址:https://www.cnblogs.com/qa-freeroad/p/14349457.html
Copyright © 2011-2022 走看看