kuiper-9-教程-function插件编译

参考官方教程: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": {
      }
    }
  ]
}

  1. 启动kuiper
bin/server
  1. 创建流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)
  1. 创建规则ruleecho,利用文件ruleecho.txt
bin/cli create rule ruleecho -f ruleecho.txt
  1. mosquitto发送数据,主题是echo
mosquitto_pub -h localhost -t "echo" -m "{\"temperature\":30}"
  1. 检查log/stream.log

发现收到的消息成功记录到了该文件中。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值