hbase.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package data
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "go-common/app/service/main/up/model/data"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "strconv"
  11. )
  12. func hbaseMd5Key(aid int64) []byte {
  13. hasher := md5.New()
  14. hasher.Write([]byte(strconv.Itoa(int(aid))))
  15. return []byte(hex.EncodeToString(hasher.Sum(nil)))
  16. }
  17. // BaseUpStat get base up stat.
  18. func (d *Dao) BaseUpStat(c context.Context, mid int64, date string) (stat *data.UpBaseStat, err error) {
  19. var (
  20. ctx, cancel = context.WithTimeout(c, d.hbaseTimeOut)
  21. tableName = HBaseUpStatTablePrefix + date // change table at 12:00am
  22. )
  23. defer cancel()
  24. result, err := d.hbase.Get(ctx, []byte(tableName), hbaseMd5Key(mid))
  25. if err != nil {
  26. log.Error("BaseUpStat d.hbase.Get BackupTable(%s, %d) error(%v)", tableName, mid, err)
  27. err = ecode.ServerErr
  28. return
  29. }
  30. if result == nil {
  31. log.Error("BaseUpStat d.hbase.Get BackupTable(%s, %d) result nil", tableName, mid)
  32. return
  33. }
  34. stat = &data.UpBaseStat{}
  35. for _, c := range result.Cells {
  36. if c == nil {
  37. continue
  38. }
  39. v, _ := strconv.ParseInt(string(c.Value[:]), 10, 64)
  40. if !bytes.Equal(c.Family, []byte("u")) {
  41. continue
  42. }
  43. switch {
  44. case bytes.Equal(c.Qualifier, []byte("play")):
  45. stat.View = v
  46. case bytes.Equal(c.Qualifier, []byte("dm")):
  47. stat.Dm = v
  48. case bytes.Equal(c.Qualifier, []byte("reply")):
  49. stat.Reply = v
  50. case bytes.Equal(c.Qualifier, []byte("fans")):
  51. stat.Fans = v
  52. case bytes.Equal(c.Qualifier, []byte("fav")):
  53. stat.Fav = v
  54. case bytes.Equal(c.Qualifier, []byte("like")):
  55. stat.Like = v
  56. }
  57. }
  58. return
  59. }