influx.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. //go:build plugins
  26. // +build plugins
  27. package main
  28. import (
  29. "encoding/json"
  30. "fmt"
  31. _ "github.com/influxdata/influxdb1-client/v2"
  32. client "github.com/influxdata/influxdb1-client/v2"
  33. "github.com/lf-edge/ekuiper/internal/topo/transform"
  34. "github.com/lf-edge/ekuiper/pkg/api"
  35. "time"
  36. )
  37. type influxSink struct {
  38. addr string
  39. username string
  40. password string
  41. measurement string
  42. databaseName string
  43. tagKey string
  44. tagValue string
  45. fields []string
  46. cli client.Client
  47. fieldMap map[string]interface{}
  48. hasTransform bool
  49. }
  50. func (m *influxSink) Configure(props map[string]interface{}) error {
  51. if i, ok := props["addr"]; ok {
  52. if i, ok := i.(string); ok {
  53. m.addr = i
  54. }
  55. }
  56. if i, ok := props["username"]; ok {
  57. if i, ok := i.(string); ok {
  58. m.username = i
  59. }
  60. }
  61. if i, ok := props["password"]; ok {
  62. if i, ok := i.(string); ok {
  63. m.password = i
  64. }
  65. }
  66. if i, ok := props["measurement"]; ok {
  67. if i, ok := i.(string); ok {
  68. m.measurement = i
  69. }
  70. }
  71. if i, ok := props["databasename"]; ok {
  72. if i, ok := i.(string); ok {
  73. m.databaseName = i
  74. }
  75. }
  76. if i, ok := props["tagkey"]; ok {
  77. if i, ok := i.(string); ok {
  78. m.tagKey = i
  79. }
  80. }
  81. if i, ok := props["tagvalue"]; ok {
  82. if i, ok := i.(string); ok {
  83. m.tagValue = i
  84. }
  85. }
  86. if i, ok := props["fields"]; ok {
  87. if i, ok := i.([]interface{}); ok {
  88. for _, v := range i {
  89. if v, ok := v.(string); ok {
  90. m.fields = append(m.fields, v)
  91. }
  92. }
  93. }
  94. }
  95. if i, ok := props["dataTemplate"]; ok {
  96. if i, ok := i.(string); ok && i != "" {
  97. m.hasTransform = true
  98. }
  99. }
  100. return nil
  101. }
  102. func (m *influxSink) Open(ctx api.StreamContext) (err error) {
  103. logger := ctx.GetLogger()
  104. logger.Debug("Opening influx sink")
  105. m.cli, err = client.NewHTTPClient(client.HTTPConfig{
  106. Addr: m.addr,
  107. Username: m.username,
  108. Password: m.password,
  109. })
  110. if err != nil {
  111. logger.Debug(err)
  112. return err
  113. }
  114. return nil
  115. }
  116. func (m *influxSink) Collect(ctx api.StreamContext, data interface{}) error {
  117. logger := ctx.GetLogger()
  118. if m.hasTransform {
  119. jsonBytes, _, err := ctx.TransformOutput(data, true)
  120. if err != nil {
  121. return err
  122. }
  123. m := make(map[string]interface{})
  124. err = json.Unmarshal(jsonBytes, &m)
  125. if err != nil {
  126. return fmt.Errorf("fail to decode data %s after applying dataTemplate for error %v", string(jsonBytes), err)
  127. }
  128. data = m
  129. } else if len(m.fields) > 0 {
  130. d, err := transform.SelectMap(data, m.fields)
  131. if err != nil {
  132. return fmt.Errorf("fail to select fields %v for data %v", m.fields, data)
  133. }
  134. data = d
  135. }
  136. var output map[string]interface{}
  137. switch v := data.(type) {
  138. case map[string]interface{}:
  139. output = v
  140. case []map[string]interface{}:
  141. if len(v) > 0 {
  142. output = v[0]
  143. } else {
  144. ctx.GetLogger().Warnf("Get empty data %v, just return", data)
  145. return nil
  146. }
  147. }
  148. bp, err := client.NewBatchPoints(client.BatchPointsConfig{
  149. Database: m.databaseName,
  150. Precision: "ns",
  151. })
  152. if err != nil {
  153. logger.Debug(err)
  154. return err
  155. }
  156. tags := map[string]string{m.tagKey: m.tagValue}
  157. m.fieldMap = output
  158. pt, err := client.NewPoint(m.measurement, tags, m.fieldMap, time.Now())
  159. if err != nil {
  160. logger.Debug(err)
  161. return err
  162. }
  163. bp.AddPoint(pt)
  164. err = m.cli.Write(bp)
  165. if err != nil {
  166. logger.Debug(err)
  167. return err
  168. }
  169. logger.Debug("insert success")
  170. return nil
  171. }
  172. func (m *influxSink) Close(ctx api.StreamContext) error {
  173. m.cli.Close()
  174. return nil
  175. }
  176. func Influx() api.Sink {
  177. return &influxSink{}
  178. }