influx.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. //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/pkg/api"
  34. "strings"
  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.(string); ok {
  88. m.fields = i
  89. }
  90. }
  91. if i, ok := props["dataTemplate"]; ok {
  92. if i, ok := i.(string); ok && i != "" {
  93. m.hasTransform = true
  94. }
  95. }
  96. return nil
  97. }
  98. func (m *influxSink) Open(ctx api.StreamContext) (err error) {
  99. logger := ctx.GetLogger()
  100. logger.Debug("Opening influx sink")
  101. m.cli, err = client.NewHTTPClient(client.HTTPConfig{
  102. Addr: m.addr,
  103. Username: m.username,
  104. Password: m.password,
  105. })
  106. if err != nil {
  107. logger.Debug(err)
  108. return err
  109. }
  110. return nil
  111. }
  112. func (m *influxSink) Collect(ctx api.StreamContext, data interface{}) error {
  113. logger := ctx.GetLogger()
  114. if m.hasTransform {
  115. jsonBytes, _, err := ctx.TransformOutput(data)
  116. if err != nil {
  117. return err
  118. }
  119. m := make(map[string]interface{})
  120. err = json.Unmarshal(jsonBytes, &m)
  121. if err != nil {
  122. return fmt.Errorf("fail to decode data %s after applying dataTemplate for error %v", string(jsonBytes), err)
  123. }
  124. data = m
  125. }
  126. var output map[string]interface{}
  127. switch v := data.(type) {
  128. case map[string]interface{}:
  129. output = v
  130. case []map[string]interface{}:
  131. if len(v) > 0 {
  132. output = v[0]
  133. } else {
  134. ctx.GetLogger().Warnf("Get empty data %v, just return", data)
  135. return nil
  136. }
  137. }
  138. bp, err := client.NewBatchPoints(client.BatchPointsConfig{
  139. Database: m.databaseName,
  140. Precision: "ns",
  141. })
  142. if err != nil {
  143. logger.Debug(err)
  144. return err
  145. }
  146. tags := map[string]string{m.tagKey: m.tagValue}
  147. fields := strings.Split(m.fields, ",")
  148. m.fieldMap = make(map[string]interface{}, 100)
  149. for _, field := range fields {
  150. if output[field] != nil {
  151. m.fieldMap[field] = output[field]
  152. }
  153. }
  154. pt, err := client.NewPoint(m.measurement, tags, m.fieldMap, time.Now())
  155. if err != nil {
  156. logger.Debug(err)
  157. return err
  158. }
  159. bp.AddPoint(pt)
  160. err = m.cli.Write(bp)
  161. if err != nil {
  162. logger.Debug(err)
  163. return err
  164. }
  165. logger.Debug("insert success")
  166. return nil
  167. }
  168. func (m *influxSink) Close(ctx api.StreamContext) error {
  169. m.cli.Close()
  170. return nil
  171. }
  172. func Influx() api.Sink {
  173. return &influxSink{}
  174. }