influx2.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2021-2022 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. _ "github.com/influxdata/influxdb-client-go/v2"
  30. client "github.com/influxdata/influxdb-client-go/v2"
  31. "github.com/lf-edge/ekuiper/pkg/api"
  32. "strings"
  33. "time"
  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.(string); ok {
  71. m.fields = i
  72. }
  73. }
  74. if i, ok := props["dataTemplate"]; ok {
  75. if i, ok := i.(string); ok && i != "" {
  76. m.hasTransform = true
  77. }
  78. }
  79. if i, ok := props["token"]; ok {
  80. if i, ok := i.(string); ok {
  81. m.token = i
  82. }
  83. }
  84. if i, ok := props["org"]; ok {
  85. if i, ok := i.(string); ok {
  86. m.org = i
  87. }
  88. }
  89. if i, ok := props["bucket"]; ok {
  90. if i, ok := i.(string); ok {
  91. m.bucket = i
  92. }
  93. }
  94. return nil
  95. }
  96. func (m *influxSink2) Open(ctx api.StreamContext) (err error) {
  97. logger := ctx.GetLogger()
  98. logger.Debug("Opening influx2 sink")
  99. options := client.DefaultOptions().SetBatchSize(100)
  100. m.cli = client.NewClientWithOptions(m.addr, m.token, options)
  101. return nil
  102. }
  103. func (m *influxSink2) Collect(ctx api.StreamContext, data interface{}) error {
  104. logger := ctx.GetLogger()
  105. if m.hasTransform {
  106. jsonBytes, _, err := ctx.TransformOutput(data)
  107. if err != nil {
  108. return err
  109. }
  110. m := make(map[string]interface{})
  111. err = json.Unmarshal(jsonBytes, &m)
  112. if err != nil {
  113. return fmt.Errorf("fail to decode data %s after applying dataTemplate for error %v", string(jsonBytes), err)
  114. }
  115. data = m
  116. }
  117. var output map[string]interface{}
  118. switch v := data.(type) {
  119. case map[string]interface{}:
  120. output = v
  121. case []map[string]interface{}:
  122. if len(v) > 0 {
  123. output = v[0]
  124. } else {
  125. ctx.GetLogger().Warnf("Get empty data %v, just return", data)
  126. return nil
  127. }
  128. }
  129. writeAPI := m.cli.WriteAPIBlocking(m.org, m.bucket)
  130. tags := map[string]string{m.tagKey: m.tagValue}
  131. fields := strings.Split(m.fields, ",")
  132. m.fieldMap = make(map[string]interface{}, 100)
  133. for _, field := range fields {
  134. if output[field] != nil {
  135. m.fieldMap[field] = output[field]
  136. }
  137. }
  138. pt := client.NewPoint(m.measurement, tags, m.fieldMap, time.Now())
  139. err := writeAPI.WritePoint(ctx, pt)
  140. if err != nil {
  141. logger.Debug(err)
  142. return err
  143. }
  144. logger.Debug("insert data into influxdb2 success")
  145. return nil
  146. }
  147. func (m *influxSink2) Close(ctx api.StreamContext) error {
  148. m.cli.Close()
  149. return nil
  150. }
  151. func Influx2() api.Sink {
  152. return &influxSink2{}
  153. }