关于golang:GOgorm

11次阅读

共计 4291 个字符,预计需要花费 11 分钟才能阅读完成。

参考资料
[1] 连贯到数据库
[2] 官网文档

1. 简介

gorm是应用的 orm 映射,所以须要定义要操作的表的 model,在 go 中须要定义一个structstruct 的名字就是对应数据库中的表名,留神 gorm 查找 struct 名对应数据库中的表名的时候会默认把你的 struct 中的大写字母转换为小写并加上“s”,所以能够加上 db.SingularTable(true)gorm 本义 struct 名字的时候不必加上“s”。

golang中,首字母大小写来示意 public 或者private,因而构造体中字段首字母必须大写。

定义 model,即struct 时,咱们能够只定义咱们须要从数据库中取回的特定字段:
gorm在本义表名的时候会把 struct 的大写字母 (首字母除外) 替换成“_”,所以上面的”GoSystemInfo”会本义成数据库中对应的“go_system_info”的表名,对应的字段名的查找会先依照 tag 外面的名称去外面查找,如果没有定义标签则依照struct 定义的字段查找,查找的时候 struct 字段中的大写会被本义成“_”,如:“SystemId”会去查找表中的 system_id 字段。

1. join 查问

https://blog.csdn.net/f95_slj…

type JobCKDao struct{}
 
func (dao *JobCKDao) MonitorQuery(ctx context.Context, gSession *gorm.DB) []*JobData {
   var db *gorm.DB
   var jobDatas []*JobData
   db = gSession.Table("job").Order("job.ysid DESC") //.Where("job.state in (?)", []int{0, 1, 3}) // 只查问状态为 0, 1, 3 的 job
   db.Select("job.ysid as job_id,job.project_id as project_id,user.name as user_name,job.name as job_name,job.state as status,project.maxproc as maxproc,job.created_at as create_time,job.end_time as end_time").
      Joins("join user on user.ysid = job.user_id").Joins("join project on job.project_id = project.ysid").Limit(5).Scan(&jobDatas)
   return jobDatas
}

2. 关联查问

https://www.jianshu.com/p/b2d…

3. 连贯 clickhouse 遇到的坑

如果在 go 中执行:

// 每时刻所有节点 cpu 的平均值
type AllNodeCpuAveData struct {
   User   float64 `json:"user" binding:"required"`
   Free   float64 `json:"free" binding:"required"`
   System float64 `json:"system" binding:"required"`
   Time   int64   `json:"time" binding:"required"`
}


type Cpu struct {
   Ysid        int64     `gorm:"primary_key;AUTO_INCREMENT;column:ysid;type:bigint;"`
   NodeName    string    `gorm:"column:node_name;type:varchar;size:128;"`
   NodeIp      string    `gorm:"column:node_ip;type:varchar;size:128;"`
   User        float64   `gorm:"column:user;type:decimal;"`     // 从系统启动开始累计到以后时刻,用户态的 CPU 工夫(单位:jiffies),不蕴含 nice 值为负过程。1jiffies=0.01 秒
   Nice        float64   `gorm:"column:nice;type:decimal;"`     // 从系统启动开始累计到以后时刻,nice 值为负的过程所占用的 CPU 工夫(单位:jiffies)System      float64   `gorm:"column:system;type:decimal;"`   // 从系统启动开始累计到以后时刻,内核态工夫(单位:jiffies)Idle        float64   `gorm:"column:idle;type:decimal;"`     // 从系统启动开始累计到以后时刻,闲暇工夫(单位:jiffies)
   IoWait      float64   `gorm:"column:io_wait;type:decimal;"`  // 从系统启动开始累计到以后时刻,闲暇工夫(单位:jiffies)
   Irq         float64   `gorm:"column:irq;type:decimal;" `     // 从系统启动开始累计到以后时刻,硬中断工夫(单位:jiffies)SoftIrq     float64   `gorm:"column:soft_irq;type:decimal;"` // 从系统启动开始累计到以后时刻,软中断工夫(单位:jiffies)Steal       float64   `gorm:"column:steal;type:decimal;"`
   Guest       float64   `gorm:"column:guest;type:decimal;"`
   GuestNice   float64   `gorm:"column:guest_nice;type:decimal;"`
   TotalLast   float64   `gorm:"column:total_last;type:decimal;"`  // 上次总工夫 User + Nice + System + Idle + Iowait + IRQ + SoftIRQ + Steal + Guest
   UsageRate   float64   `gorm:"column:usage_rate;type:decimal;"`  // 以后总应用百分比
   IowaitRate  float64   `gorm:"column:iowait_rate;type:decimal;"` // IO 期待应用百分比
   UserRate    float64   `gorm:"column:user_rate;type:decimal;"`   // 用户态百分比
   SystemRate  float64   `gorm:"column:system_rate;type:decimal;"` // 内核态百分比
   IdleRate    float64   `gorm:"column:idle_rate;type:decimal;"`   // 闲暇态百分比
   NanoTime    int64     `gorm:"column:nano_time;type:bigint;"`    // 工夫戳,单位:秒
   CollectTime time.Time `gorm:"column:collect_time;type:timestamp;default:CURRENT_TIMESTAMP;"`
   CreatedAt   time.Time `gorm:"column:created_at;type:timestamp;default:CURRENT_TIMESTAMP;"`
   UpdatedAt   time.Time `gorm:"column:updated_at;type:timestamp;default:CURRENT_TIMESTAMP;"`
}


db, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{})
if err!=nil{fmt.Print("报错了,err=",err)
   return
}
fmt.Println("连贯胜利!")

//var db1  *gorm.DB
var result []AllNodeCpuAveData
db.Table("cpu").Select("nano_time,avg(user) as user,avg(system)-avg(user) as free,avg(system) as system").Group("nano_time").Scan(&result) // 找出合乎时间段内并以工夫分组
fmt.Println("result=",result)

报错:

2021/02/04 20:00:29 ?[31;1mD:/wzz/study/clickHouseDemo/fl/fl.go:66 ?[35;1mcode: 184, message: Aggregate function avg(nano_time) is found inside another aggregate function in query: While processing avg(nano_time) AS user; code: 184,
 message: Aggregate function avg(nano_time) is found inside another aggregate function in query: While processing avg(nano_time) AS user
?[0m?[33m[1.995ms] ?[34;1m[rows:-]?[0m SELECT max(ysid) as system,avg(user) as free,avg(nano_time) as user FROM `cpu`
result= []

起因在于 AllNodeCpuAveData 的字段名称与 cpu 中的字段名称雷同。
解决办法

// 将字段名称改为不同
type AllNodeCpuAveData struct {
   Userd   float64 `json:"userd" binding:"required"`
   Freed   float64 `json:"freed" binding:"required"`
   Systemd float64 `json:"systemd" binding:"required"`
   Time   int64   `json:"time" binding:"required"`
}

db.Table("cpu").Select("nano_time,avg(user) as userd,avg(system)-avg(user) as freed,avg(system) as systemd").Group("nano_time").Scan(&result) // 找出合乎时间段内并以工夫分组
正文完
 0