kuiper-7-编写function.go

扩展function

开发定制功能:

  1. 编写go程序,实现api.Function
  2. build成插件
go build --buildmode=plugin -o plugins/functions/MyFunction.so plugins/functions/my_function.go

go程序要实现 Validate方法,这个方法会在SQL验证时被调用。
在此方法中,xsql.Expr类型的切片作为参数传递,包含这个function运行时的参数(argument);
开发人员可以对它进行验证,以检查参数数量和类型等。 如果验证成功,返回 nil。 否则,返回一个错误对象。

//The argument is a list of xsql.Expr
Validate(args []interface{}) error

Function包括 聚合函数,一般函数。
聚合函数,如果参数是列,接收到的值始终是组中列值的一个切片;
扩展函数必须通过实现 IsAggregate 方法来区分函数类型。

//If this function is an aggregate function. Each parameter of an aggregate function will be a slice
IsAggregate() bool

Function的主要任务是实现 exec 方法。 该方法将利用 SQL 计算函数的结果。 参数是函数参数值的一个切片。 你可以用它们来计算。 如果计算成功,则返回结果和 true; 否则,返回 nil 和 false。

Exec(args []interface{}) (interface{}, bool)

因为函数是一个插件,必须在主包中,

var MyFunction myFunction

https://github.com/emqx/kuiper/blob/master/plugins/functions/echo.go

例子

echo.go的功能是读取一个参数,然后返回这个参数

导包 fmt
结构:echo
方法:Validate(interface切片)
判断参数是否正常,比如判断是否切片长度为1
方法:Exec(interface切片) interface bool
根据传入的切皮处理,得到一个结果,第二个返回值是?
方法:IsAggregate() bool
返回true/false
var Echo echo

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值