redis.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2021 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 main
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "time"
  19. "github.com/go-redis/redis/v7"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "github.com/lf-edge/ekuiper/pkg/cast"
  22. )
  23. type RedisSink struct {
  24. // host:port address.
  25. addr string
  26. username string
  27. // Optional password. Must match the password specified in the
  28. password string
  29. // Database to be selected after connecting to the server.
  30. db int
  31. // key of field
  32. field string
  33. // key define
  34. key string
  35. dataType string
  36. expiration time.Duration
  37. sendSingle bool
  38. cli *redis.Client
  39. }
  40. func (r *RedisSink) Configure(props map[string]interface{}) error {
  41. if i, ok := props["addr"]; ok {
  42. if i, ok := i.(string); ok {
  43. r.addr = i
  44. }
  45. } else {
  46. return errors.New("redis addr is null")
  47. }
  48. if i, ok := props["password"]; ok {
  49. if i, ok := i.(string); ok {
  50. r.password = i
  51. }
  52. }
  53. r.db = 0
  54. if i, ok := props["db"]; ok {
  55. if t, err := cast.ToInt(i, cast.STRICT); err == nil {
  56. r.db = t
  57. }
  58. }
  59. if i, ok := props["key"]; ok {
  60. if i, ok := i.(string); ok {
  61. r.key = i
  62. }
  63. } else {
  64. return errors.New("not config data key for redis")
  65. }
  66. if i, ok := props["field"]; ok {
  67. if i, ok := i.(string); ok {
  68. r.field = i
  69. }
  70. }
  71. r.sendSingle = true
  72. if i, ok := props["sendSingle"]; ok {
  73. if i, ok := i.(bool); ok {
  74. r.sendSingle = i
  75. }
  76. }
  77. r.dataType = "string"
  78. if i, ok := props["dataType"]; ok {
  79. if i, ok := i.(string); ok {
  80. r.dataType = i
  81. }
  82. }
  83. r.expiration = -1
  84. if i, ok := props["expiration"]; ok {
  85. if t, err := cast.ToInt(i, cast.STRICT); err == nil {
  86. r.expiration = time.Duration(t)
  87. }
  88. }
  89. return nil
  90. }
  91. func (r *RedisSink) Open(ctx api.StreamContext) (err error) {
  92. logger := ctx.GetLogger()
  93. logger.Debug("Opening redis sink")
  94. r.cli = redis.NewClient(&redis.Options{
  95. Addr: r.addr,
  96. Username: r.username,
  97. Password: r.password,
  98. DB: r.db, // use default DB
  99. })
  100. return nil
  101. }
  102. func (r *RedisSink) Collect(ctx api.StreamContext, data interface{}) error {
  103. logger := ctx.GetLogger()
  104. if v, ok := data.([]byte); ok {
  105. if r.field != "" {
  106. if !r.sendSingle {
  107. var out []map[string]interface{}
  108. if err := json.Unmarshal(v, &out); err != nil {
  109. logger.Debug("Failed to unmarshal data with error: ", err, " data:", string(v))
  110. return err
  111. }
  112. for _, m := range out {
  113. key := r.field
  114. field, ok := m[key].(string)
  115. if ok {
  116. key = field
  117. }
  118. if r.dataType == "list" {
  119. err := r.cli.LPush(key, v).Err()
  120. if err != nil {
  121. logger.Error(err)
  122. return err
  123. }
  124. logger.Debugf("send redis list success, key:%s data: %s", key, string(v))
  125. } else {
  126. err := r.cli.Set(key, v, r.expiration*time.Second).Err()
  127. if err != nil {
  128. logger.Error(err)
  129. return err
  130. }
  131. logger.Debugf("send redis string success, key:%s data: %s", key, string(v))
  132. }
  133. }
  134. } else {
  135. var out map[string]interface{}
  136. if err := json.Unmarshal(v, &out); err != nil {
  137. logger.Debug("Failed to unmarshal data with error: ", err, " data:", string(v))
  138. return err
  139. }
  140. key := r.field
  141. field, ok := out[key].(string)
  142. if ok {
  143. key = field
  144. }
  145. if r.dataType == "list" {
  146. err := r.cli.LPush(key, v).Err()
  147. if err != nil {
  148. logger.Error(err)
  149. return err
  150. }
  151. logger.Debugf("send redis list success, key:%s data: %s", key, string(v))
  152. } else {
  153. err := r.cli.Set(key, v, r.expiration*time.Second).Err()
  154. if err != nil {
  155. logger.Error(err)
  156. return err
  157. }
  158. logger.Debugf("send redis string success, key:%s data: %s", key, string(v))
  159. }
  160. }
  161. } else if r.key != "" {
  162. if r.dataType == "list" {
  163. err := r.cli.LPush(r.key, v).Err()
  164. if err != nil {
  165. logger.Error(err)
  166. return err
  167. }
  168. logger.Debugf("send redis list success, key:%s data: %s", r.key, string(v))
  169. } else {
  170. err := r.cli.Set(r.key, v, r.expiration*time.Second).Err()
  171. if err != nil {
  172. logger.Error(err)
  173. return err
  174. }
  175. logger.Debugf("send redis string success, key:%s data: %s", r.key, string(v))
  176. }
  177. }
  178. logger.Debug("insert success", string(v))
  179. } else {
  180. logger.Debug("insert failed data is not []byte data:", data)
  181. }
  182. return nil
  183. }
  184. func (r *RedisSink) Close(ctx api.StreamContext) error {
  185. err := r.cli.Close()
  186. return err
  187. }
  188. func Redis() api.Sink {
  189. return &RedisSink{}
  190. }