ekuiper icon indicating copy to clipboard operation
ekuiper copied to clipboard

storing multi-rows data in PostgreSQL DB from a single message

Open ngjaying opened this issue 1 year ago • 0 comments

Discussed in https://github.com/lf-edge/ekuiper/discussions/3141

Originally posted by robodevchampion August 23, 2024 I have data coming like below after executing SQL

{'device_name': 'sensor2', 'ts': '1724231947811', 'values': {'combine': '41:107', 'humidity': 41, 'temperature': '30'}}

Added an action for insertion data in DB, I have enabled send single as False

Below is the data template I am using

[
  {{- $firstMsg := true -}}
  {{- range $i, $msg := . -}}
    {{- $firstKey := true -}}
    {{- range $key, $value := $msg.values -}}
      {{- if not $firstKey -}},{{- end -}}
      {{- $firstKey = false -}}
      {
        "deviceid": "{{$msg.device_name}}",
        "ts": "{{$msg.ts}}",
        "key_name": "{{$key}}",
        "value": "{{$value}}"
      }
    {{- end -}}
    {{- if not $firstMsg -}},{{- end -}}
    {{- $firstMsg = false -}}
  {{- end -}}
]

Below is the error I got:

fail to decode data [{
        "deviceid": "sensor1",
        "ts": "1724231947811",
        "key_name": "combine",
        "value": "40:107"
      },{
        "deviceid": "sensor1",
        "ts": "1724231947811",
        "key_name": "humidity",
        "value": "40"
      },{
        "deviceid": "sensor1",
        "ts": "1724231947811",
        "key_name": "temperature",
        "value": "200"
      }]
 after applying dataTemplate for error json: cannot unmarshal array into Go value of type map[string]interface {}

Can you help me what I am missing, above data format is desired.

Thanks

ngjaying avatar Aug 28 '24 00:08 ngjaying