main.go 1.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
package main

import (
	"database/sql/driver"
	"fmt"
	"io"
	"os"
	"time"

	taos "github.com/taosdata/driver-go/v2/af"
)

func main() {
	db, err := taos.Open("", "", "", "log", 0)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	defer db.Close()
	topic, err := db.Subscribe(false, "taoslogtail", "select ts, level, ipaddr, content from log", time.Second)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(2)
	}
	defer topic.Unsubscribe(true)
	for {
		func() {
			rows, err := topic.Consume()
			defer func() { rows.Close(); time.Sleep(time.Second) }()
			if err != nil {
				fmt.Println(err)
				os.Exit(3)
			}
			for {
				values := make([]driver.Value, 4)
				err := rows.Next(values)
				if err == io.EOF {
					break
				} else if err != nil {
					fmt.Fprintln(os.Stderr, err)
					os.Exit(4)
				}
				ts := values[0].(time.Time)
				level := values[1].(int8)
				ipaddr := values[2].(string)
				content := values[3].(string)
				fmt.Printf("%s %d %s %s\n", ts.Format(time.StampMilli), level, ipaddr, content)
			}
		}()
	}
}

// 未完成