hbase.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "strconv"
  8. "time"
  9. "go-common/app/job/main/growup/model"
  10. "go-common/library/log"
  11. )
  12. var (
  13. //HBaseArchiveTablePrefix 分类分端播放
  14. HBaseArchiveTablePrefix = "video_play_category_"
  15. //HBaseFamilyPlat family
  16. HBaseFamilyPlat = []byte("v")
  17. )
  18. func hbaseMd5Key(aid int64) string {
  19. hasher := md5.New()
  20. hasher.Write([]byte(strconv.Itoa(int(aid))))
  21. return hex.EncodeToString(hasher.Sum(nil))
  22. }
  23. // ArchiveStat get the stat of archive.
  24. func (d *Dao) ArchiveStat(c context.Context, aid int64, date time.Time) (stat *model.ArchiveStat, err error) {
  25. var (
  26. ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
  27. tableName = HBaseArchiveTablePrefix + date.Format("20060102")
  28. key = hbaseMd5Key(aid)
  29. )
  30. defer cancel()
  31. result, err := d.hbase.GetStr(ctx, tableName, key)
  32. if err != nil {
  33. log.Error("ArchiveStat d.hbase.GetStr tableName(%s)|aid(%d)|key(%v)|error(%v)", tableName, aid, key, err)
  34. return
  35. }
  36. if result == nil {
  37. return
  38. }
  39. stat = &model.ArchiveStat{}
  40. for _, c := range result.Cells {
  41. if c == nil {
  42. continue
  43. }
  44. v, _ := strconv.ParseInt(string(c.Value[:]), 10, 64)
  45. if !bytes.Equal(c.Family, HBaseFamilyPlat) {
  46. continue
  47. }
  48. switch {
  49. case bytes.Equal(c.Qualifier, []byte("play")):
  50. stat.Play = v
  51. case bytes.Equal(c.Qualifier, []byte("dm")):
  52. stat.Dm = v
  53. case bytes.Equal(c.Qualifier, []byte("reply")):
  54. stat.Reply = v
  55. case bytes.Equal(c.Qualifier, []byte("like")):
  56. stat.Like = v
  57. case bytes.Equal(c.Qualifier, []byte("sh")):
  58. stat.Share = v
  59. }
  60. }
  61. return
  62. }