package main import ( "database/sql" "fmt" _ "github.com/taosdata/driver-go/v3/taosSql" "log" "math/rand" "sync" "time" ) var DateFmtYYYYMMDDHHmmss = "2006-01-02 15:04:05" func main() { var taosDSN = "root:taosdata@tcp(192.168.0.153:6030)/avata_1" 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(-10 * time.Hour) list := getObject() fmt.Println("开始 ", time.Now().Format(DateFmtYYYYMMDDHHmmss)) wg := sync.WaitGroup{} for z := 0; z < 100; z++ { wg.Add(1) go func(z int) { defer wg.Done() p := z * 150 c := p + 150 data := "" for ; p < c; p++ { amount1 := 0.00 amount2 := 0.00 amount3 := 0.00 amount4 := 0.00 for i := 50; i > 1; i-- { day := now.AddDate(0, 0, -i) for j := 1; j < 11; j++ { //for j := 0; j < 5; j++ { //num := 1 d := "" rand.Seed(time.Now().UnixNano()) addMinute := rand.Intn(90) operation := rand.Intn(4) + 1 money := rand.Intn(1000) operationType := 1 if list[p].Pid != 0 { operation = 2 } if operation == 1 { operationType = rand.Intn(3) + 1 amount1 = amount1 + float64(money) d = fmt.Sprintf("amount_%d_%d_%d USING amounts TAGS (%d, %d, %d, %d, %d) values (%d,%f) ", list[p].Chain, list[p].Uid, list[p].Pid, list[p].Chain, list[p].Uid, list[p].Pid, operation, operationType, day.Add(time.Duration(j*addMinute)*time.Minute).UnixMilli(), amount1) } if operation == 2 { amount2 = amount2 + float64(money)*0.07 d = fmt.Sprintf("amount_%d_%d_%d USING amounts TAGS (%d, %d, %d, %d, %d) values (%d,%f) ", list[p].Chain, list[p].Uid, list[p].Pid, list[p].Chain, list[p].Uid, list[p].Pid, operation, operationType, day.Add(time.Duration(j*addMinute)*time.Minute).UnixMilli(), amount2) } if operation == 3 { amount3 = amount3 + float64(money) d = fmt.Sprintf("amount_%d_%d_%d USING amounts TAGS (%d, %d, %d, %d, %d) values (%d,%f) ", list[p].Chain, list[p].Uid, list[p].Pid, list[p].Chain, list[p].Uid, list[p].Pid, operation, operationType, day.Add(time.Duration(j*addMinute)*time.Minute).UnixMilli(), amount3) } if operation == 4 { d = fmt.Sprintf("amount_%d_%d_%d USING amounts TAGS (%d, %d, %d, %d, %d) values (%d,%f) ", list[p].Chain, list[p].Uid, list[p].Pid, list[p].Chain, list[p].Uid, list[p].Pid, operation, operationType, day.Add(time.Duration(j*addMinute)*time.Minute).UnixMilli(), amount4) } //data = fmt.Sprintf(" amount_%d_%d_%d USING amounts TAGS (%d, %d, %d, %d, %d) values%s ", list[p].Chain, list[p].Uid, list[p].Pid, list[p].Chain, list[p].Uid, list[p].Pid, operation, operationType, data) data = fmt.Sprintf("%s %s", data, d) } } if (p+1)%10 == 0 { data = fmt.Sprintf("insert into %s", data) //fmt.Println(data) _, err = taos.Exec(data) if err != nil { fmt.Println("failed to insert, err:", err) return } data = "" } } fmt.Println("完成 ", time.Now().Format(DateFmtYYYYMMDDHHmmss), " z: ", z, " 当前cup: ", list[p]) }(z) wg.Wait() fmt.Println("结束 ", time.Now().Format(DateFmtYYYYMMDDHHmmss)) } } type TxObject struct { Chain int Uid int Pid int //Account string } func getObject() []TxObject { var list []TxObject uid := 1 pid := 1 //for i := 0; i < num; i++ { //3 条链 for j := 1; j < 4; j++ { var chain, user, project int chain = j // 用户 1000 for k := 1; k < 1001; k++ { //for k := 1; k < 21; k++ { user = uid // 项目 5 for t := 1; t < 6; t++ { if pid%5 == 0 || pid%7 == 0 { project = 0 } else { project = pid } pid++ list = append(list, TxObject{ Chain: chain, Uid: user, Pid: project, }) } uid++ } } //} return list }