influx.go 4.6 KB

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