influx.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2021 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. _ "github.com/influxdata/influxdb1-client/v2"
  30. client "github.com/influxdata/influxdb1-client/v2"
  31. api "github.com/lf-edge/ekuiper/pkg/api"
  32. "strings"
  33. "time"
  34. )
  35. type influxSink struct {
  36. addr string
  37. username string
  38. password string
  39. measurement string
  40. databasename string
  41. tagkey string
  42. tagvalue string
  43. fields string
  44. cli client.Client
  45. fieldmap map[string]interface{}
  46. }
  47. func (m *influxSink) Configure(props map[string]interface{}) error {
  48. if i, ok := props["addr"]; ok {
  49. if i, ok := i.(string); ok {
  50. m.addr = i
  51. }
  52. }
  53. if i, ok := props["username"]; ok {
  54. if i, ok := i.(string); ok {
  55. m.username = i
  56. }
  57. }
  58. if i, ok := props["password"]; ok {
  59. if i, ok := i.(string); ok {
  60. m.password = i
  61. }
  62. }
  63. if i, ok := props["measurement"]; ok {
  64. if i, ok := i.(string); ok {
  65. m.measurement = i
  66. }
  67. }
  68. if i, ok := props["databasename"]; ok {
  69. if i, ok := i.(string); ok {
  70. m.databasename = i
  71. }
  72. }
  73. if i, ok := props["tagkey"]; ok {
  74. if i, ok := i.(string); ok {
  75. m.tagkey = i
  76. }
  77. }
  78. if i, ok := props["tagvalue"]; ok {
  79. if i, ok := i.(string); ok {
  80. m.tagvalue = i
  81. }
  82. }
  83. if i, ok := props["fields"]; ok {
  84. if i, ok := i.(string); ok {
  85. m.fields = i
  86. }
  87. }
  88. return nil
  89. }
  90. func (m *influxSink) Open(ctx api.StreamContext) (err error) {
  91. logger := ctx.GetLogger()
  92. logger.Debug("Opening influx sink")
  93. m.cli, err = client.NewHTTPClient(client.HTTPConfig{
  94. Addr: m.addr,
  95. Username: m.username,
  96. Password: m.password,
  97. })
  98. if err != nil {
  99. logger.Debug(err)
  100. return err
  101. }
  102. return nil
  103. }
  104. func (m *influxSink) Collect(ctx api.StreamContext, data interface{}) error {
  105. logger := ctx.GetLogger()
  106. var output map[string]interface{}
  107. switch v := data.(type) {
  108. case map[string]interface{}:
  109. output = v
  110. case []map[string]interface{}:
  111. if len(v) > 0 {
  112. output = v[0]
  113. } else {
  114. ctx.GetLogger().Warnf("Get empty data %v, just return", data)
  115. return nil
  116. }
  117. }
  118. bp, err := client.NewBatchPoints(client.BatchPointsConfig{
  119. Database: m.databasename,
  120. Precision: "ns",
  121. })
  122. if err != nil {
  123. logger.Debug(err)
  124. return err
  125. }
  126. tags := map[string]string{m.tagkey: m.tagvalue}
  127. fields := strings.Split(m.fields, ",")
  128. m.fieldmap = make(map[string]interface{}, 100)
  129. for _, field := range fields {
  130. if output[field] != nil {
  131. m.fieldmap[field] = output[field]
  132. }
  133. }
  134. pt, err := client.NewPoint(m.measurement, tags, m.fieldmap, time.Now())
  135. if err != nil {
  136. logger.Debug(err)
  137. return err
  138. }
  139. bp.AddPoint(pt)
  140. err = m.cli.Write(bp)
  141. if err != nil {
  142. logger.Debug(err)
  143. return err
  144. }
  145. logger.Debug("insert success")
  146. return nil
  147. }
  148. func (m *influxSink) Close(ctx api.StreamContext) error {
  149. m.cli.Close()
  150. return nil
  151. }
  152. func Influx() api.Sink {
  153. return &influxSink{}
  154. }