kuiper-12-sources-readfile.go

19 篇文章 0 订阅

处理流程

  1. 编写readfile.go
  2. 编写readfile.yml配置文件
  3. 编译成.so
  4. 启动kuiper
  5. 创建stream
  6. 查询所有

readfile.go

任务是读取txt文件,每读取一行就输出,读到最后结束。

其中,struct readfile为:

type readfile struct {
	filepath string //文件路径
	cancel   context.CancelFunc
}

核心是实现方法Open()

func (s *readfile) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
	t := time.NewTicker(1000 * time.Millisecond)
	exeCtx, cancel := ctx.WithCancel()
	s.cancel = cancel

	go func(exeCtx api.StreamContext) {

		defer t.Stop()
		file, err := os.Open(s.filepath)
		if err != nil{
			print(err)
			os.Exit(2)
		}
		defer file.Close()
		reader := bufio.NewReader(file)
		for {
			select {
			case <-t.C:
				str,err := reader.ReadBytes('\n')

				if err == io.EOF {
					println("read the file finished")
					cancel()
					os.Exit(2)
					return
				}
				if err != nil {
					println(err)
					os.Exit(2)
				}
				str3 := string(str)
				print(str3)
				s.pattern["val"]=str3
				api.NewDefaultSourceTuple(s.pattern,nil)
			case <-exeCtx.Done():
				return
			}
		}
	}(exeCtx)
}

相关命令

//创建流
bin/cli create stream  streamRead '(NUM STRING)WITH (DATASOURCE="topicRead", TYPE="readfile")'

//查询所有
bin/cli query
select * from streamRead

结果

通过执行完上面6步,预期结果是:

  1. 由于readfile.go中有一句api.NewDefaultSourceTuple(msg_map,meta_map),应该会在query面板每隔1秒收到一条消息。
  2. 由于readfile.go中有一句print(str3),应该在server面板每隔1秒打印读取到的字符串。

实际结果是:
第一条没有收到,第二条收到了
在这里插入图片描述

在这里插入图片描述

分析

读取文件成功,但是没有传出去,应该是Open()方法中,最后调用函数api.NewDefaultSourceTuple(msg_map,meta_map)时,传入的参数有问题。
但是不传入map,编译为插件时不通过,会报错如下:

sources/readfile.go:59:30: cannot use msg_list (type []string) as type map[string]interface {} in argument to api.NewDefaultSourceTuple
sources/readfile.go:59:30: cannot use meta_list (type []string) as type map[string]interface {} in argument to api.NewDefaultSourceTuple

官方的对这个函数的说明:
https://github.com/emqx/kuiper/blob/master/docs/en_US/extension/source.md

Typically, use api.NewDefaultSourceTuple(message, meta) to create a SourceTuple. The meta data could be anything that worth to be recorded. For example, the qualified topic of the message. The first parameter is a StreamContext pointer. You can retrieve the context information and logger etc. from it.

.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值