utils.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package data
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/library/log"
  7. "go-common/library/database/hbase.v2"
  8. "github.com/tsuna/gohbase/hrpc"
  9. )
  10. func (d *Dao) getDataWithBackup(c context.Context, client *hbase.Client, tableNameFunc func(retryCount int) string, maxRetry int, key string, options ...func(hrpc.Call) error) (result *hrpc.Result, err error) {
  11. if client == nil {
  12. err = fmt.Errorf("hbase client is nil")
  13. return
  14. }
  15. for i := 0; i < maxRetry; i++ {
  16. var tableName = tableNameFunc(i)
  17. if result, err = d.hbase.GetStr(c, tableName, key, options...); err != nil {
  18. log.Error("hbase GetStr tableName(%s)|key(%v)|error(%v)", tableName, key, err)
  19. continue
  20. }
  21. break
  22. }
  23. return
  24. }
  25. func getTableName(tablePrefix string, date time.Time) string {
  26. return tablePrefix + date.Format("20060102")
  27. }
  28. func generateTableNameFunc(tablePrefix string, date time.Time, dayDiff int) func(retryCount int) string {
  29. return func(retryCount int) string {
  30. var backdate = date.AddDate(0, 0, dayDiff*retryCount)
  31. return getTableName(tablePrefix, backdate)
  32. }
  33. }