hbase.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package weeklyhonor
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/admin/main/up/util/hbaseutil"
  6. model "go-common/app/interface/main/creative/model/weeklyhonor"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "github.com/tsuna/gohbase/hrpc"
  10. )
  11. // reverse for string.
  12. func reverseString(s string) string {
  13. rs := []rune(s)
  14. l := len(rs)
  15. for f, t := 0, l-1; f < t; f, t = f+1, t-1 {
  16. rs[f], rs[t] = rs[t], rs[f]
  17. }
  18. ns := string(rs)
  19. if l < 10 {
  20. for i := 0; i < 10-l; i++ {
  21. ns = ns + "0"
  22. }
  23. }
  24. return ns
  25. }
  26. func honorRowKey(id int64, date string) string {
  27. idStr := strconv.FormatInt(id, 10)
  28. s := reverseString(idStr) + date
  29. return s
  30. }
  31. // HonorStat get up honor.
  32. func (d *Dao) HonorStat(c context.Context, mid int64, date string) (stat *model.HonorStat, err error) {
  33. var (
  34. result *hrpc.Result
  35. ctx, cancel = context.WithTimeout(c, d.hbaseTimeOut)
  36. tableName = "up_honorary_weekly"
  37. key = honorRowKey(mid, date)
  38. )
  39. defer cancel()
  40. if result, err = d.hbase.GetStr(ctx, tableName, key); err != nil {
  41. log.Error("HonorStat d.hbase.GetStr tableName(%s) mid(%d) key(%v) error(%v)", tableName, mid, key, err)
  42. err = ecode.CreativeDataErr
  43. return
  44. }
  45. if result == nil {
  46. return
  47. }
  48. stat = new(model.HonorStat)
  49. parser := hbaseutil.Parser{}
  50. err = parser.Parse(result.Cells, &stat)
  51. if err != nil {
  52. log.Error("failed to parse hbase cells,tableName(%s) mid(%d) key(%v) stat(%+v) err(%v)", tableName, mid, key, stat, err)
  53. return
  54. }
  55. log.Info("HonorStat d.hbase.GetStr tableName(%s) mid(%d) key(%v) stat(%+v)", tableName, mid, key, stat)
  56. return
  57. }