influx2.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. fields []string
  44. cli client.Client
  45. fieldMap map[string]interface{}
  46. hasTransform bool
  47. }
  48. func (m *influxSink2) Configure(props map[string]interface{}) error {
  49. if i, ok := props["addr"]; ok {
  50. if i, ok := i.(string); ok {
  51. m.addr = i
  52. }
  53. }
  54. if i, ok := props["measurement"]; ok {
  55. if i, ok := i.(string); ok {
  56. m.measurement = i
  57. }
  58. }
  59. if i, ok := props["tagKey"]; ok {
  60. if i, ok := i.(string); ok {
  61. m.tagKey = i
  62. }
  63. }
  64. if i, ok := props["tagValue"]; ok {
  65. if i, ok := i.(string); ok {
  66. m.tagValue = i
  67. }
  68. }
  69. if i, ok := props["fields"]; ok {
  70. if i, ok := i.([]interface{}); ok {
  71. for _, v := range i {
  72. if v, ok := v.(string); ok {
  73. m.fields = append(m.fields, v)
  74. }
  75. }
  76. }
  77. }
  78. if i, ok := props["dataTemplate"]; ok {
  79. if i, ok := i.(string); ok && i != "" {
  80. m.hasTransform = true
  81. }
  82. }
  83. if i, ok := props["token"]; ok {
  84. if i, ok := i.(string); ok {
  85. m.token = i
  86. }
  87. }
  88. if i, ok := props["org"]; ok {
  89. if i, ok := i.(string); ok {
  90. m.org = i
  91. }
  92. }
  93. if i, ok := props["bucket"]; ok {
  94. if i, ok := i.(string); ok {
  95. m.bucket = i
  96. }
  97. }
  98. return nil
  99. }
  100. func (m *influxSink2) Open(ctx api.StreamContext) (err error) {
  101. logger := ctx.GetLogger()
  102. logger.Debug("Opening influx2 sink")
  103. options := client.DefaultOptions().SetBatchSize(100)
  104. m.cli = client.NewClientWithOptions(m.addr, m.token, options)
  105. return nil
  106. }
  107. func (m *influxSink2) Collect(ctx api.StreamContext, data interface{}) error {
  108. logger := ctx.GetLogger()
  109. if m.hasTransform {
  110. jsonBytes, _, err := ctx.TransformOutput(data, true)
  111. if err != nil {
  112. return err
  113. }
  114. m := make(map[string]interface{})
  115. err = json.Unmarshal(jsonBytes, &m)
  116. if err != nil {
  117. return fmt.Errorf("fail to decode data %s after applying dataTemplate for error %v", string(jsonBytes), err)
  118. }
  119. data = m
  120. } else if len(m.fields) > 0 {
  121. d, err := transform.SelectMap(data, m.fields)
  122. if err != nil {
  123. return fmt.Errorf("fail to select fields %v for data %v", m.fields, data)
  124. }
  125. data = d
  126. }
  127. var output map[string]interface{}
  128. switch v := data.(type) {
  129. case map[string]interface{}:
  130. output = v
  131. case []map[string]interface{}:
  132. if len(v) > 0 {
  133. output = v[0]
  134. } else {
  135. ctx.GetLogger().Warnf("Get empty data %v, just return", data)
  136. return nil
  137. }
  138. }
  139. writeAPI := m.cli.WriteAPIBlocking(m.org, m.bucket)
  140. tags := map[string]string{m.tagKey: m.tagValue}
  141. m.fieldMap = output
  142. pt := client.NewPoint(m.measurement, tags, m.fieldMap, time.Now())
  143. err := writeAPI.WritePoint(ctx, pt)
  144. if err != nil {
  145. logger.Debug(err)
  146. return err
  147. }
  148. logger.Debug("insert data into influxdb2 success")
  149. return nil
  150. }
  151. func (m *influxSink2) Close(ctx api.StreamContext) error {
  152. m.cli.Close()
  153. return nil
  154. }
  155. func Influx2() api.Sink {
  156. return &influxSink2{}
  157. }