influx.go 3.9 KB

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