Use Golang template to customize analaysis result in Kuiper

Introduction

Golang template introduction

Actions

{{if pipeline}} T1 {{else}} T0 {{end}}
{{range pipeline}} T1 {{else}} T0 {{end}}
{{if pipeline}} {"field1": true} {{else}}  {"field1": false} {{end}}

Kuiper sink data format

[]map[string]interface{}

Send slice data by piece

[
{"device_id":"1","t_av":36.25,"t_count":4,"t_max":80,"t_min":10},
{"device_id":"2","t_av":27,"t_count":4,"t_max":45,"t_min":12}
]
...
"sendSingle": true,
"dataTemplate": "{{json .}}"

Data content conversion

...
"dataTemplate": "{\"device_id\": {{.device_id}}, \"description\": \"{{if lt .t_av 30.0}}Current temperature is {{.t_av}}, it's normal.\"{{else if ge .t_av 30.0}}Current temperature is {{.t_av}}, it's high.\"{{end}}}"
"sendSingle": true,
{"device_id": {{.device_id}}, "description": "
{{if lt .t_av 30.0}}
Current temperature is {{.t_av}}, it's normal."
{{else if ge .t_av 30.0}}
Current temperature is {{.t_av}}, it's high."
{{end}}
}
{"device_id": 1, "description": "Current temperature is 36.25, it's high."}
{"device_id": 2, "description": "Current temperature is 27, it's normal."}

Data traversal

{"device_id":"1", 
"values": [
{"temperature": 10.5},
{"temperature": 20.3},
{"temperature": 30.3}
]
}
"sendSingle": true,
"dataTemplate": "{{$len := len .values}} {{$loopsize := add $len -1}} {\"device_id\": \"{{.device_id}}\", \"description\": [{{range $index, $ele := .values}} {{if le .temperature 25.0}}\"fine\"{{else if gt .temperature 25.0}}\"high\"{{end}} {{if eq $loopsize $index}}]{{else}},{{end}}{{end}}}"
{{range $index, $ele := .values}} 
{{if le .temperature 25.0}}
“fine”
{{else if gt .temperature 25.0}}
“high”
{{end}}
{{if eq $loopsize $index}}
]
{{else}}
,
{{end}}
{{end}}
{"device_id": "1", "description": [ "fine" , "fine" , "high" ]}

Summary

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
EMQ Technologies

EMQ is an open-source IoT data infrastructure software provider, delivering the world’s leading open-source MQTT message broker and stream processing database.