go connect database and spown data of target

`func RandInts(min, max int) int {
// if max < min {
// max = max - min
// min = max + min
// max = min - max
// }
rr := rand.New(rand.NewSource(time.Now().UnixNano() * rand.Int63n(9999)))
return rr.Intn(max - min + 1) + min
}

func alloc(sum, count, min, max int)[]int{
var retlist []int
sum -= mincount
for count > 1 {
maxRemain := sum/count
2
if maxRemain > max {
maxRemain = max
}
ret := RandInts(0, maxRemain - min)
sum -= ret
count --
retlist = append(retlist, ret + min)
}
retlist = append(retlist, sum + min)
return retlist
}

func floattostr(input_num float64) string {
return strconv.FormatFloat(input_num, ‘g‘, 1, 32)
}

func timeformal(timestr string)string{
tims := strings.Split(timestr, "/")
ret := tims[0] + "/"
if len(tims[1]) <= 1 {
ret += "0" + tims[1]
} else {
ret += tims[1]
}
ret += "/"
if len(tims[2]) <= 1 {
ret += "0" + tims[2]
} else {
ret += tims[2]
}
return ret
}

func getRandTimeByWeight(count int)int{
timeweight := [...]int{85,66,52,43,33,22,11,15,20,25,28,30,38,40,38,55,60,65,63,68,71,75,70,66,75,80,85,90,100,90,80,75,66,68,74,66,88,80,95,88,90,100,100,100,80,70,75,80}
if count <= 100 {
for i := 0; i< 48; i++ {
if timeweight[i] < 70 {
timeweight[i] = 0
}
}
}

for i := 1; i< 48; i++ {
	timeweight[i] += timeweight[i-1]
}
ret := RandInts(1, timeweight[47])
dex := 0
for i := 0; i< 48; i++ {
	if ret <= timeweight[i] {
		dex = i
		break
	}
}
return dex

}

func getTimeRand(timestr string, count int)time.Time{
tm2, _ := time.Parse("2006/01/02", timestr)
cur := tm2.Unix()
min := getRandTimeByWeight(count)1800
ret := int(cur-8
3600) + RandInts(min, min+1800)
retu := time.Unix(int64(ret), 0)
return retu
}

//唯一id
func CreateRefId(playType int) string {
serverId := RandInts(1, 3)
return strconv.Itoa(int(serverId)) + "" + strconv.Itoa(int(playType)) + "" + uuid.New().String()
}

type tosql struct {
createdAt time.Time json:"createdAt" db:"createdAt"
level float32 json:"level" db:"level"
platformCommission float32 json:"platformCommission" db:"platformCommission"
}

type tosqlLs []tosql
func (p tosqlLs) Len() int { return len(p) }
func (p tosqlLs) Less(i, j int) bool { return (
p[i]).createdAt.Before((*p[j]).createdAt) }
func (p tosqlLs) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

type UserData struct {
Id int json:"id" db:"id"
RegTime time.Time json:"regTime" db:"regTime"
Region string json:"region" db:"region"
PlayType int json:"playType" db:"playType"
}

var (
database *sqlx.DB //mysql连接对象//
trandata chan UserData
)

//连接mysql
func initMysql() {
trandata = make(chan UserData, 100)
fmt.Printf("StartDB...")
var err error
//Log.Printf("mysql config, host:%s, name:%s, name:%s", MysqlConfigViper_.GetString("host"), MysqlConfigViper_.GetString("name"), MysqlConfigViper_.GetString("username"))
connStr := fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true&loc=Local&charset=utf8",
"root", "f3bc7ec833F98e491939e74a90d875", "120.76.201.244:3306", "indiaGame")
database, err = sqlx.Connect("mysql", connStr)
if err != nil {
panic(err)
}
database.DB.SetMaxOpenConns(500)
database.DB.SetMaxIdleConns(100)
database.DB.SetConnMaxLifetime(time.Second * 0)
fmt.Printf("The Db is: %v \n", database)
}

//取用户信息
func queryUser(playType int, limit int, offset int, back func(*UserData)){
sql := fmt.Sprintf("select * from india_user where playType=%d limit %d offset %d", playType, limit, offset)
results, err := database.Query(sql)
if err != nil {
print(err.Error())
return
}
for results.Next() {
var data UserData
err = results.Scan(&data.Id, &data.RegTime, &data.Region, &data.PlayType)
if err != nil {
print(err.Error())
return
}
del := (RandInts(0,8) - data.Id%9 + 9)%9
if del < 2 {
back(&data)
}
}
}

type InData struct {
CreatedAt time.Time json:"createdAt" db:"createdAt"
Uid int json:"uid" db:"uid"
Region string json:"region" db:"region"
PlayType int json:"playType" db:"playType"
Level float32 json:"level" db:"level"
PlatformCommission float32 json:"platformCommission" db:"platformCommission"
RefId string json:"refId" db:"refId"
}

func main(){
initMysql()
go mainwait()
offrand := 0
for offset := 0 ; ;offset += offrand {
offrand = RandInts(60,100)
queryUser(110,offrand*RandInts(15,30),offset, func(data *UserData){
trandata <- *data
})
}
}

//1547565 421086
func mainwait() {
file, _ := os.OpenFile("exeout.csv", os.O_WRONLY|os.O_CREATE, os.ModePerm)
w := csv.NewWriter(file)

betstr := [...]float32{0.1,0.5,1,2,5,10,25,50}
mins := [...]int{1,1,10,10,1,1,1,1}
maxs := [...]int{24,120,240,480,120,200,400,500}

rfile, _ := os.Open("exe.csv")
r := csv.NewReader(rfile)
arr := [...]int{6,11,16,21,26,31,36,41}
sumcount := 0
for i := 0;;i++ {
	strs, ok := r.Read()
	if ok != nil {
		break
	}
	if i > 1 {
		day := timeformal(strs[0])
		tolist := make([]*tosql, 0, 1000)
		for j := 0; j< len(arr); j++ {
			ind := arr[j]
			if strs[ind] == "0" || strs[ind] == "" {
				//out += "0 0 "
			} else {
				salary := strs[ind]
				intnum := 0
				devide := float32(1.0)
				if ind < 23 {
					snum, _ := strconv.ParseFloat(salary,32)
					intnum = int(math.Floor(snum*10 + 0.5))
					devide = float32(10.0)
				} else {
					intnum, _ = strconv.Atoi(salary)
				}

				count, _ := strconv.Atoi(strs[ind+1])

				arrind := ind/5 - 1
				retlist := alloc(intnum, count, mins[arrind], maxs[arrind])
				for retInd := 0; retInd < len(retlist); retInd++ {
					//out := []string{getTimeRand(day)}
					//outlog := append(out, betstr[arrind], float(retlist[retInd])/devide)
					tolist = append(tolist, &tosql{getTimeRand(day, len(retlist)), betstr[arrind], float32(retlist[retInd])/devide})
					//w.Write(outlog)
				}
			}
		}

		sort.Sort(tosqlLs(tolist))
		for _, todata := range tolist {
			for{
				data := <-trandata 
				if data.RegTime.Before(todata.createdAt) {
					fid := CreateRefId(110)
					sql := "insert into india_round_texas(createdAt, uid, region, playType, level, platformCommission, refId)" +
					"values(:createdAt, :uid, :region, :playType, :level, :platformCommission, :refId)"
					gdata := InData{todata.createdAt, data.Id,  data.Region, 110, todata.level, todata.platformCommission, fid}
					_, err := database.NamedExec(sql, gdata)
					if err != nil {
						fmt.Printf("writeData ERROR sql:%v, err:%+v \n", sql, err)
						return
					}
					//fmt.Printf("writeData success sql:%v \n", sql)
					sumcount ++
					break
				}
			}
		}
		print(fmt.Sprintf("[%d]sumcount:%d", i, sumcount))
	}
}
w.Flush()
file.Close()

}
`

go connect database and spown data of target

上一篇:mysql-MVCC


下一篇:MySql查询优化