sink.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package memory
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "strings"
  19. "github.com/lf-edge/ekuiper/internal/io/memory/pubsub"
  20. "github.com/lf-edge/ekuiper/internal/topo/transform"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. "github.com/lf-edge/ekuiper/pkg/ast"
  23. "github.com/lf-edge/ekuiper/pkg/cast"
  24. )
  25. type config struct {
  26. Topic string `json:"topic"`
  27. DataTemplate string `json:"dataTemplate"`
  28. RowkindField string `json:"rowkindField"`
  29. KeyField string `json:"keyField"`
  30. Fields []string `json:"fields"`
  31. }
  32. type sink struct {
  33. topic string
  34. hasTransform bool
  35. keyField string
  36. rowkindField string
  37. fields []string
  38. }
  39. func (s *sink) Open(ctx api.StreamContext) error {
  40. ctx.GetLogger().Debugf("Opening memory sink: %v", s.topic)
  41. pubsub.CreatePub(s.topic)
  42. return nil
  43. }
  44. func (s *sink) Configure(props map[string]interface{}) error {
  45. cfg := &config{}
  46. err := cast.MapToStruct(props, cfg)
  47. if err != nil {
  48. return err
  49. }
  50. if strings.ContainsAny(cfg.Topic, "#+") {
  51. return fmt.Errorf("invalid memory topic %s: wildcard found", cfg.Topic)
  52. }
  53. s.topic = cfg.Topic
  54. if cfg.DataTemplate != "" {
  55. s.hasTransform = true
  56. }
  57. s.fields = cfg.Fields
  58. s.rowkindField = cfg.RowkindField
  59. s.keyField = cfg.KeyField
  60. if s.rowkindField != "" && s.keyField == "" {
  61. return fmt.Errorf("keyField is required when rowkindField is set")
  62. }
  63. return nil
  64. }
  65. func (s *sink) Collect(ctx api.StreamContext, data interface{}) error {
  66. ctx.GetLogger().Debugf("receive %+v", data)
  67. topic, err := ctx.ParseTemplate(s.topic, data)
  68. if err != nil {
  69. return err
  70. }
  71. if s.hasTransform {
  72. jsonBytes, _, err := ctx.TransformOutput(data, true)
  73. if err != nil {
  74. return err
  75. }
  76. m := make(map[string]interface{})
  77. err = json.Unmarshal(jsonBytes, &m)
  78. if err != nil {
  79. return fmt.Errorf("fail to decode data %s after applying dataTemplate for error %v", string(jsonBytes), err)
  80. }
  81. data = m
  82. } else if len(s.fields) > 0 {
  83. m, err := transform.SelectMap(data, s.fields)
  84. if err != nil {
  85. return fmt.Errorf("fail to select fields %v for data %v", s.fields, data)
  86. }
  87. data = m
  88. }
  89. switch d := data.(type) {
  90. case []map[string]interface{}:
  91. for _, el := range d {
  92. err := s.publish(ctx, topic, el)
  93. if err != nil {
  94. return fmt.Errorf("fail to publish data %v for error %v", d, err)
  95. }
  96. }
  97. case map[string]interface{}:
  98. err := s.publish(ctx, topic, d)
  99. if err != nil {
  100. return fmt.Errorf("fail to publish data %v for error %v", d, err)
  101. }
  102. default:
  103. return fmt.Errorf("unrecognized format of %s", data)
  104. }
  105. return nil
  106. }
  107. func (s *sink) Close(ctx api.StreamContext) error {
  108. ctx.GetLogger().Debugf("closing memory sink")
  109. pubsub.RemovePub(s.topic)
  110. return nil
  111. }
  112. func (s *sink) publish(ctx api.StreamContext, topic string, el map[string]interface{}) error {
  113. if s.rowkindField != "" {
  114. c, ok := el[s.rowkindField]
  115. var rowkind string
  116. if !ok {
  117. rowkind = ast.RowkindUpsert
  118. } else {
  119. rowkind, ok = c.(string)
  120. if !ok {
  121. return fmt.Errorf("rowkind field %s is not a string in data %v", s.rowkindField, el)
  122. }
  123. if rowkind != ast.RowkindInsert && rowkind != ast.RowkindUpdate && rowkind != ast.RowkindDelete && rowkind != ast.RowkindUpsert {
  124. return fmt.Errorf("invalid rowkind %s", rowkind)
  125. }
  126. }
  127. key, ok := el[s.keyField]
  128. if !ok {
  129. return fmt.Errorf("key field %s not found in data %v", s.keyField, el)
  130. }
  131. pubsub.ProduceUpdatable(ctx, topic, el, rowkind, key)
  132. } else {
  133. pubsub.Produce(ctx, topic, el)
  134. }
  135. return nil
  136. }