| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- 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)
- }
- }
|