package main import ( "fmt" "os" "strings" "time" . "git.wecise.com/wecise/odbserver/odb" "git.wecise.com/wecise/odbserver/odb/cass" "gitee.com/wecisecode/util/logger" ) func main() { // connect to the cluster //cluster := gocql.NewCluster("192.168.40.14") date := time.Now().AddDate(0, -3, 0).Format("2006-01-02") if len(os.Args) > 1 { date = os.Args[1] } keyspace := "matrix" if len(os.Args) > 2 { keyspace = os.Args[2] } table := "vobject" if len(os.Args) > 3 { table = os.Args[3] } fmt.Printf("keyspace: %s , table: %s, date: %s\n", keyspace, table, date) var pp int var err error if pp, err = DateToPartition(date); err != nil { fmt.Printf("date format error :%v .", err) os.Exit(1) } if table == "vobject" || strings.HasPrefix(table, "vobject_") { var cassOption cass.Option cassOption.Keyspace = keyspace cassOption.DisableInitialHostLookup = true if session, err := cass.NewCassSession(cassOption); err != nil { logger.Error(err) } else { fmt.Printf("create session successfully .\n") sql := fmt.Sprintf(`select distinct id, day from %s.%s limit 50000`, keyspace, table) iter := session.Query(sql).PageSize(200).Iter() defer iter.Close() var id, day string del_set := map[string][]string{} record := 0 for iter.Scan(&id, &day) { if partition, err := DateToPartition(day); err != nil { fmt.Printf("date format error :%v .", err) os.Exit(1) } else { record++ if record%200 == 0 { fmt.Printf("current record:%d .\n", record) } if partition <= pp { if ss, ok := del_set[id]; ok { del_set[id] = append(ss, day) } else { del_set[id] = []string{day} } } } } fmt.Printf("select total %d.\n", len(del_set)) if f, err := os.Create(fmt.Sprintf("%s_del.cql", table)); err != nil { fmt.Println("create file fail") os.Exit(1) } else { defer f.Close() counter := 0 for id, days := range del_set { counter++ cql := fmt.Sprintf("delete from %s.%s where id='%s' and day in (%s) ;\n", keyspace, table, id, `'`+strings.Join(days, `','`)+`'`) f.WriteString(cql) f.Sync() if counter%500 == 0 { fmt.Printf("write total :%d .\n", counter) } } } } } else { fmt.Printf("unsupport table %s .", table) os.Exit(1) } }