参考官方教程:https://github.com/emqx/kuiper/blob/develop/docs/zh_CN/plugins/plugins_tutorial.md
我的配置
我的go版本1.13
我的gopath:/home/zhang/gopath
创建项目
和教程一样
在$gopath/src目录下新建文件夹plugin_project,作为插件项目地址,内部结构如下:
plugin_project
sources //源(source)插件源代码目录
mysource.go
sinks //目标(sink)插件源代码目录
mysink.go
functions //函数(function)插件源代码目录
myfunction.go
target //编译结果目录
go.mod //go module文件
修改go.mod
module samplePlugin
go 1.13
require (
github.com/emqx/kuiper v0.0.0-20200323140757-60d00241372b
)
编写插件.go文件
我是开发functions,所以在插件项目的functions目录下新建echo.go文件
package main
import (
"fmt"
)
type echo struct {
}
func (f *echo) Validate(args []interface{}) error{
if len(args) != 1{
return fmt.Errorf("echo function only supports 1 parameter but got %d", len(args))
}
return nil
}
func (f *echo) Exec(args []interface{}) (interface{}, bool) {
result := args[0]
return result, true
}
func (f *echo) IsAggregate() bool {
return false
}
var Echo echo
编译插件–克隆kuiper
在$gopath/src目录下执行如下代码:
git clone https://github.com/emqx/kuiper.git
编译插件–编译kuiper
在$gopath/src/kuiper目录下执行如下代码:
make
编译插件
在插件目录下,执行:
go mod edit -replace github.com/emqx/kuiper=/home/zhang/gopath/src/kuiper
在插件目录下,执行:
go build --buildmode=plugin -o /home/zhang/gopath/src/kuiper/_build/$build/plugins/functions/Echo@v1.0.0.so functions/echo.go
$build要改成实际目录名,即:
go build --buildmode=plugin -o /home/zhang/gopath/src/kuiper/_build/kuiper-0.3.0-4-g9423612-linux-x86_64/plugins/functions/Echo@v1.0.0.so functions/echo.go
测试插件
编写rule文件
在kuiper安装目录下(/home/zhang/gopath/src/kuiper/_build/$build/)创建如下文件
ruleechoo.txt
{
"id": "ruleecho",
"sql": "SELECT echo(temperature) from demo",
"actions": [
{
"log": {
}
}
]
}
- 启动kuiper
bin/server
- 创建流demo,主题是echo
bin/cli create stream demo '(temperature bigint) WITH (FORMAT="JSON", DATASOURCE="echo")'
//bin/cli create stream streamInput 'prStruct struct WITH (FORMAT="JSON", DATASOURCE="topicInput")'
//ADDRESS STRUCT(STREET_NAME STRING, NUMBER BIGINT)
- 创建规则ruleecho,利用文件ruleecho.txt
bin/cli create rule ruleecho -f ruleecho.txt
- mosquitto发送数据,主题是echo
mosquitto_pub -h localhost -t "echo" -m "{\"temperature\":30}"
- 检查log/stream.log
发现收到的消息成功记录到了该文件中。