package main import ( "database/sql" "fmt" _ "github.com/taosdata/driver-go/v3/taosSql" "log" "math/rand" "strings" "time" ) var DateFmtYYYYMMDDHHmmss = "2006-01-02 15:04:05" func main() { var taosDSN = "root:taosdata@tcp(192.168.0.153:6030)/test2" taos, err := sql.Open("taosSql", taosDSN) if err != nil { log.Fatalln("failed to connect TDengine, err:", err) return } defer taos.Close() // 测试插入数据 now := time.Now().Add(-12 * time.Hour) fmt.Println("开始 ", time.Now().Format(DateFmtYYYYMMDDHHmmss)) for p := 0; p < 20; p++ { for k := 0; k < 10000; k++ { d := "" account := fmt.Sprintf("iaa%s", GetRandomString(39)) for i := 40; i > 1; i-- { day := now.AddDate(0, 0, -i) operation := rand.Intn(19) + 1 t := rand.Intn(2) + 1 for j := 0; j < 3; j++ { num := 1 rand.Seed(time.Now().UnixNano()) addMill := rand.Intn(24) + 1 gas := 0 if operation == 1 { // 创建链账户授权 if t != 1 { gas = rand.Intn(20000) + 50000 } } else if operation == 8 || operation == 9 || operation == 10 || operation == 11 || operation == 17 || operation == 18 || operation == 19 || operation == 20 { gas = rand.Intn(20000) + 50000 num = rand.Intn(9) + 1 gas = gas * num } else { gas = rand.Intn(20000) + 50000 } //fmt.Println(day.Add(time.Duration(j*22)*time.Minute).Format(DateFmtYYYYMMDDHHmmss), a) d = fmt.Sprintf("%s (%d,%d,%d)", d, day.Add(time.Duration(j*addMill)*time.Hour).UnixMilli(), num, gas) fmt.Println(d) return } d = fmt.Sprintf("insert into %s USING tx_metrics TAGS (%d, %d, %d,\"%s\", %d, %d) values%s", account, 1, 10, 20, account, operation, t, d) _, err = taos.Exec(d) if err != nil { fmt.Println("failed to insert, err:", err) return } d = "" } fmt.Println("完成", k, "个 ", time.Now().Format(DateFmtYYYYMMDDHHmmss)) } } fmt.Println("结束 ", time.Now().Format(DateFmtYYYYMMDDHHmmss)) } func GetRandomString(l int) string { now := time.Now().Format("060102150405.000") nowFmt := strings.Replace(now, ".", "", -1) str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" bytes := []byte(str) var result []byte j := 0 r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(rand.Intn(100)))) for i := 0; i < l; i++ { if i%2 != 0 && j < len(nowFmt) { result = append(result, nowFmt[j]) j++ } else { result = append(result, bytes[r.Intn(len(bytes))]) } } return string(result) }