hbase.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. "go-common/app/job/main/spy/model"
  9. "go-common/library/log"
  10. "github.com/pkg/errors"
  11. "github.com/tsuna/gohbase/hrpc"
  12. )
  13. var (
  14. // table
  15. _hbaseTableActive = "active_data"
  16. // family
  17. _familyDays = "activeDays"
  18. _familyDaysB = []byte(_familyDays)
  19. )
  20. func strRowKey(mid int64) string {
  21. return fmt.Sprintf("%d", mid)
  22. }
  23. // GetActiveData get user active days and watched bangumi video time
  24. func (dao *Dao) GetActiveData(c context.Context, mid int64) (active *model.Active, err error) {
  25. var (
  26. result *hrpc.Result
  27. key = strRowKey(mid)
  28. ctx, cancel = context.WithTimeout(c, time.Duration(dao.c.HBase.ReadTimeout))
  29. )
  30. defer cancel()
  31. if result, err = dao.hbase.GetStr(ctx, _hbaseTableActive, key); err != nil {
  32. err = errors.Wrapf(err, "hbase.GetStr(%s,%s)", _hbaseTableActive, key)
  33. return
  34. }
  35. active = &model.Active{}
  36. for _, c := range result.Cells {
  37. h := &model.Active{}
  38. if c != nil && bytes.Equal(c.Family, _familyDaysB) {
  39. days, err := strconv.ParseInt(string(c.Qualifier), 10, 64)
  40. if err != nil {
  41. log.Error("strconv.ParseInt err(%v)", err)
  42. continue
  43. }
  44. h.Active = days
  45. active = h
  46. }
  47. }
  48. return
  49. }