sink.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "github.com/lf-edge/ekuiper/internal/io/memory/pubsub"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/ast"
  21. "github.com/lf-edge/ekuiper/pkg/cast"
  22. "strings"
  23. )
  24. type config struct {
  25. Topic string `json:"topic"`
  26. DataTemplate string `json:"dataTemplate"`
  27. RowkindField string `json:"rowkindField"`
  28. KeyField string `json:"keyField"`
  29. }
  30. type sink struct {
  31. topic string
  32. hasTransform bool
  33. keyField string
  34. rowkindField string
  35. }
  36. func (s *sink) Open(ctx api.StreamContext) error {
  37. ctx.GetLogger().Debugf("Opening memory sink: %v", s.topic)
  38. pubsub.CreatePub(s.topic)
  39. return nil
  40. }
  41. func (s *sink) Configure(props map[string]interface{}) error {
  42. cfg := &config{}
  43. err := cast.MapToStruct(props, cfg)
  44. if err != nil {
  45. return err
  46. }
  47. if strings.ContainsAny(cfg.Topic, "#+") {
  48. return fmt.Errorf("invalid memory topic %s: wildcard found", cfg.Topic)
  49. }
  50. s.topic = cfg.Topic
  51. if cfg.DataTemplate != "" {
  52. s.hasTransform = true
  53. }
  54. s.rowkindField = cfg.RowkindField
  55. s.keyField = cfg.KeyField
  56. if s.rowkindField != "" && s.keyField == "" {
  57. return fmt.Errorf("keyField is required when rowkindField is set")
  58. }
  59. return nil
  60. }
  61. func (s *sink) Collect(ctx api.StreamContext, data interface{}) error {
  62. ctx.GetLogger().Debugf("receive %+v", data)
  63. topic, err := ctx.ParseTemplate(s.topic, data)
  64. if err != nil {
  65. return err
  66. }
  67. if s.hasTransform {
  68. jsonBytes, _, err := ctx.TransformOutput(data)
  69. if err != nil {
  70. return err
  71. }
  72. m := make(map[string]interface{})
  73. err = json.Unmarshal(jsonBytes, &m)
  74. if err != nil {
  75. return fmt.Errorf("fail to decode data %s after applying dataTemplate for error %v", string(jsonBytes), err)
  76. }
  77. data = m
  78. }
  79. switch d := data.(type) {
  80. case []map[string]interface{}:
  81. for _, el := range d {
  82. err := s.publish(ctx, topic, el)
  83. if err != nil {
  84. return fmt.Errorf("fail to publish data %v for error %v", d, err)
  85. }
  86. }
  87. case map[string]interface{}:
  88. err := s.publish(ctx, topic, d)
  89. if err != nil {
  90. return fmt.Errorf("fail to publish data %v for error %v", d, err)
  91. }
  92. default:
  93. return fmt.Errorf("unrecognized format of %s", data)
  94. }
  95. return nil
  96. }
  97. func (s *sink) Close(ctx api.StreamContext) error {
  98. ctx.GetLogger().Debugf("closing memory sink")
  99. pubsub.RemovePub(s.topic)
  100. return nil
  101. }
  102. func (s *sink) publish(ctx api.StreamContext, topic string, el map[string]interface{}) error {
  103. if s.rowkindField != "" {
  104. c, ok := el[s.rowkindField]
  105. var rowkind string
  106. if !ok {
  107. rowkind = ast.RowkindUpsert
  108. } else {
  109. rowkind, ok = c.(string)
  110. if !ok {
  111. return fmt.Errorf("rowkind field %s is not a string in data %v", s.rowkindField, el)
  112. }
  113. if rowkind != ast.RowkindInsert && rowkind != ast.RowkindUpdate && rowkind != ast.RowkindDelete && rowkind != ast.RowkindUpsert {
  114. return fmt.Errorf("invalid rowkind %s", rowkind)
  115. }
  116. }
  117. key, ok := el[s.keyField]
  118. if !ok {
  119. return fmt.Errorf("key field %s not found in data %v", s.keyField, el)
  120. }
  121. pubsub.ProduceUpdatable(ctx, topic, el, rowkind, key)
  122. } else {
  123. pubsub.Produce(ctx, topic, el)
  124. }
  125. return nil
  126. }