file_source.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package extensions
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/emqx/kuiper/common"
  6. "github.com/emqx/kuiper/xstream/api"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "time"
  11. )
  12. type FileType string
  13. const (
  14. JSON_TYPE FileType = "json"
  15. )
  16. var fileTypes = map[FileType]bool{
  17. JSON_TYPE: true,
  18. }
  19. type FileSourceConfig struct {
  20. FileType FileType `json:"fileType"`
  21. Path string `json:"path"`
  22. Interval int `json:"interval"`
  23. RetainSize int `json:"$retainSize"`
  24. }
  25. // The BATCH to load data from file at once
  26. type FileSource struct {
  27. file string
  28. config *FileSourceConfig
  29. }
  30. func (fs *FileSource) Close(ctx api.StreamContext) error {
  31. ctx.GetLogger().Infof("Close file source")
  32. // do nothing
  33. return nil
  34. }
  35. func (fs *FileSource) Configure(fileName string, props map[string]interface{}) error {
  36. cfg := &FileSourceConfig{}
  37. err := common.MapToStruct(props, cfg)
  38. if err != nil {
  39. return fmt.Errorf("read properties %v fail with error: %v", props, err)
  40. }
  41. if cfg.FileType == "" {
  42. return errors.New("missing or invalid property fileType, must be 'json'")
  43. }
  44. if _, ok := fileTypes[cfg.FileType]; !ok {
  45. return fmt.Errorf("invalid property fileType: %s", cfg.FileType)
  46. }
  47. if cfg.Path == "" {
  48. return errors.New("missing property Path")
  49. }
  50. if fileName == "" {
  51. return errors.New("file name must be specified")
  52. }
  53. if !filepath.IsAbs(cfg.Path) {
  54. cfg.Path, err = common.GetLoc("/" + cfg.Path)
  55. if err != nil {
  56. return fmt.Errorf("invalid path %s", cfg.Path)
  57. }
  58. }
  59. fs.file = path.Join(cfg.Path, fileName)
  60. if fi, err := os.Stat(fs.file); err != nil {
  61. if os.IsNotExist(err) {
  62. return fmt.Errorf("file %s not exist", fs.file)
  63. } else if !fi.Mode().IsRegular() {
  64. return fmt.Errorf("file %s is not a regular file", fs.file)
  65. }
  66. }
  67. fs.config = cfg
  68. return nil
  69. }
  70. func (fs *FileSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  71. err := fs.Load(ctx, consumer)
  72. if err != nil {
  73. errCh <- err
  74. return
  75. }
  76. if fs.config.Interval > 0 {
  77. ticker := time.NewTicker(time.Millisecond * time.Duration(fs.config.Interval))
  78. logger := ctx.GetLogger()
  79. defer ticker.Stop()
  80. for {
  81. select {
  82. case <-ticker.C:
  83. logger.Debugf("Load file source again at %v", common.GetNowInMilli())
  84. err := fs.Load(ctx, consumer)
  85. if err != nil {
  86. errCh <- err
  87. return
  88. }
  89. case <-ctx.Done():
  90. return
  91. }
  92. }
  93. }
  94. }
  95. func (fs *FileSource) Load(ctx api.StreamContext, consumer chan<- api.SourceTuple) error {
  96. switch fs.config.FileType {
  97. case JSON_TYPE:
  98. ctx.GetLogger().Debugf("Start to load from file %s", fs.file)
  99. resultMap := make([]map[string]interface{}, 0)
  100. err := common.ReadJsonUnmarshal(fs.file, &resultMap)
  101. if err != nil {
  102. return fmt.Errorf("loaded %s, check error %s", fs.file, err)
  103. }
  104. ctx.GetLogger().Debug("Sending tuples")
  105. if fs.config.RetainSize > 0 && fs.config.RetainSize < len(resultMap) {
  106. resultMap = resultMap[(len(resultMap) - fs.config.RetainSize):]
  107. ctx.GetLogger().Debug("Sending tuples for retain size %d", fs.config.RetainSize)
  108. }
  109. for _, m := range resultMap {
  110. select {
  111. case consumer <- api.NewDefaultSourceTuple(m, nil):
  112. // do nothing
  113. case <-ctx.Done():
  114. return nil
  115. }
  116. }
  117. // Send EOF if retain size not set
  118. if fs.config.RetainSize == 0 {
  119. select {
  120. case consumer <- api.NewDefaultSourceTuple(nil, nil):
  121. // do nothing
  122. case <-ctx.Done():
  123. return nil
  124. }
  125. }
  126. ctx.GetLogger().Debug("All tuples sent")
  127. return nil
  128. }
  129. return fmt.Errorf("invalid file type %s", fs.config.FileType)
  130. }