influx.go 4.3 KB

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