qsend.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/mq/client"
  7. "github.com/seaweedfs/seaweedfs/weed/mq/messages"
  8. "os"
  9. "time"
  10. )
  11. var (
  12. master = flag.String("master", "localhost:9333", "master csv list")
  13. topic = flag.String("topic", "", "topic name")
  14. )
  15. func main() {
  16. flag.Parse()
  17. publisher := client.NewPublisher(&client.PublisherOption{
  18. Masters: *master,
  19. Topic: *topic,
  20. })
  21. err := eachLineStdin(func(line string) error {
  22. if len(line) > 0 {
  23. if err := publisher.Publish(&messages.Message{
  24. Key: nil,
  25. Content: []byte(line),
  26. Properties: nil,
  27. Ts: time.Time{},
  28. }); err != nil {
  29. return err
  30. }
  31. }
  32. return nil
  33. })
  34. publisher.Shutdown()
  35. if err != nil {
  36. fmt.Printf("error: %v\n", err)
  37. }
  38. }
  39. func eachLineStdin(eachLineFn func(string) error) error {
  40. scanner := bufio.NewScanner(os.Stdin)
  41. for scanner.Scan() {
  42. text := scanner.Text()
  43. if err := eachLineFn(text); err != nil {
  44. return err
  45. }
  46. }
  47. // handle error
  48. if scanner.Err() != nil {
  49. return fmt.Errorf("scan stdin: %v", scanner.Err())
  50. }
  51. return nil
  52. }