influx2.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Licensed under the Apache License, Version 2.0 (the "License");
  15. // you may not use this file except in compliance with the License.
  16. // You may obtain a copy of the License at
  17. //
  18. // http://www.apache.org/licenses/LICENSE-2.0
  19. //
  20. // Unless required by applicable law or agreed to in writing, software
  21. // distributed under the License is distributed on an "AS IS" BASIS,
  22. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. // See the License for the specific language governing permissions and
  24. // limitations under the License.
  25. package main
  26. import (
  27. "encoding/json"
  28. "fmt"
  29. "time"
  30. _ "github.com/influxdata/influxdb-client-go/v2"
  31. client "github.com/influxdata/influxdb-client-go/v2"
  32. "github.com/lf-edge/ekuiper/internal/topo/transform"
  33. "github.com/lf-edge/ekuiper/pkg/api"
  34. )
  35. type influxSink2 struct {
  36. addr string
  37. token string
  38. measurement string
  39. org string
  40. bucket string
  41. tagKey string
  42. tagValue string
  43. dataField string
  44. fields []string
  45. cli client.Client
  46. fieldMap map[string]interface{}
  47. hasTransform bool
  48. }
  49. func (m *influxSink2) Configure(props map[string]interface{}) error {
  50. if i, ok := props["addr"]; ok {
  51. if i, ok := i.(string); ok {
  52. m.addr = i
  53. }
  54. }
  55. if i, ok := props["measurement"]; ok {
  56. if i, ok := i.(string); ok {
  57. m.measurement = i
  58. }
  59. }
  60. if i, ok := props["tagKey"]; ok {
  61. if i, ok := i.(string); ok {
  62. m.tagKey = i
  63. }
  64. }
  65. if i, ok := props["tagValue"]; ok {
  66. if i, ok := i.(string); ok {
  67. m.tagValue = i
  68. }
  69. }
  70. if i, ok := props["dataField"]; ok {
  71. if i, ok := i.(string); ok {
  72. m.dataField = i
  73. }
  74. }
  75. if i, ok := props["fields"]; ok {
  76. if i, ok := i.([]interface{}); ok {
  77. for _, v := range i {
  78. if v, ok := v.(string); ok {
  79. m.fields = append(m.fields, v)
  80. }
  81. }
  82. }
  83. }
  84. if i, ok := props["dataTemplate"]; ok {
  85. if i, ok := i.(string); ok && i != "" {
  86. m.hasTransform = true
  87. }
  88. }
  89. if i, ok := props["token"]; ok {
  90. if i, ok := i.(string); ok {
  91. m.token = i
  92. }
  93. }
  94. if i, ok := props["org"]; ok {
  95. if i, ok := i.(string); ok {
  96. m.org = i
  97. }
  98. }
  99. if i, ok := props["bucket"]; ok {
  100. if i, ok := i.(string); ok {
  101. m.bucket = i
  102. }
  103. }
  104. return nil
  105. }
  106. func (m *influxSink2) Open(ctx api.StreamContext) (err error) {
  107. logger := ctx.GetLogger()
  108. logger.Debug("Opening influx2 sink")
  109. options := client.DefaultOptions().SetBatchSize(100)
  110. m.cli = client.NewClientWithOptions(m.addr, m.token, options)
  111. return nil
  112. }
  113. func (m *influxSink2) Collect(ctx api.StreamContext, data interface{}) error {
  114. logger := ctx.GetLogger()
  115. if m.hasTransform {
  116. jsonBytes, _, err := ctx.TransformOutput(data)
  117. if err != nil {
  118. return err
  119. }
  120. m := make(map[string]interface{})
  121. err = json.Unmarshal(jsonBytes, &m)
  122. if err != nil {
  123. return fmt.Errorf("fail to decode data %s after applying dataTemplate for error %v", string(jsonBytes), err)
  124. }
  125. data = m
  126. } else {
  127. d, _, err := transform.TransItem(data, m.dataField, m.fields)
  128. if err != nil {
  129. return fmt.Errorf("fail to select fields %v for data %v", m.fields, data)
  130. }
  131. data = d
  132. }
  133. var output map[string]interface{}
  134. switch v := data.(type) {
  135. case map[string]interface{}:
  136. output = v
  137. case []map[string]interface{}:
  138. if len(v) > 0 {
  139. output = v[0]
  140. } else {
  141. ctx.GetLogger().Warnf("Get empty data %v, just return", data)
  142. return nil
  143. }
  144. }
  145. writeAPI := m.cli.WriteAPIBlocking(m.org, m.bucket)
  146. tags := map[string]string{m.tagKey: m.tagValue}
  147. m.fieldMap = output
  148. pt := client.NewPoint(m.measurement, tags, m.fieldMap, time.Now())
  149. err := writeAPI.WritePoint(ctx, pt)
  150. if err != nil {
  151. logger.Debug(err)
  152. return err
  153. }
  154. logger.Debug("insert data into influxdb2 success")
  155. return nil
  156. }
  157. func (m *influxSink2) Close(ctx api.StreamContext) error {
  158. m.cli.Close()
  159. return nil
  160. }
  161. func Influx2() api.Sink {
  162. return &influxSink2{}
  163. }