• 1.5 GO json转Map


    使用GO将show slave status查询返回的json串转为Map类型

    package main
    
    import (
        "encoding/json"
        "fmt"
        "strconv"
    )
    
    /*
    以mysql 从库show slave status查询返回的json串为例
    查询show slave status以json方式返回
    将之转为map[string]interface{}
    遍历map转为map[string]string
    map[key]返回查询的参数值
     */
    func getJSONValue(key string) string {
        ss := []byte(`{"Auto_Position":0,"Channel_Name":"","Connect_Retry":60,"Exec_Master_Log_Pos":450949253,"Executed_Gtid_Set":"","Last_Errno":0,"Last_Error":"","Last_IO_Errno":0,"Last_IO_Error":"","Last_IO_Error_Timestamp":"","Last_SQL_Errno":0,"Last_SQL_Error":"","Last_SQL_Error_Timestamp":"","Master_Bind":"","Master_Host":"10.10.10.10","Master_Info_File":"/data/mysql/data/tanpf/master.info","Master_Log_File":"mysql-bin.000269","Master_Port":3309,"Master_Retry_Count":86400,"Master_SSL_Allowed":"No","Master_SSL_CA_File":"","Master_SSL_CA_Path":"","Master_SSL_Cert":"","Master_SSL_Cipher":"","Master_SSL_Crl":"","Master_SSL_Crlpath":"","Master_SSL_Key":"","Master_SSL_Verify_Server_Cert":"No","Master_Server_Id":330901,"Master_TLS_Version":"","Master_UUID":"5a1c5dfb-b721-11e8-a53e-6c92bf2e0119","Master_User":"db_slave","Read_Master_Log_Pos":450949253,"Relay_Log_File":"mysql-relay-bin.000811","Relay_Log_Pos":450949466,"Relay_Log_Space":285022926812,"Relay_Master_Log_File":"mysql-bin.000269","Replicate_Do_DB":"","Replicate_Do_Table":"","Replicate_Ignore_DB":"","Replicate_Ignore_Server_Ids":"","Replicate_Ignore_Table":"","Replicate_Rewrite_DB":"","Replicate_Wild_Do_Table":"","Replicate_Wild_Ignore_Table":"performance_schema.%,information_schema.%,test.%","Retrieved_Gtid_Set":"","SQL_Delay":0,"SQL_Remaining_Delay":null,"Seconds_Behind_Master":0,"Skip_Counter":0,"Slave_IO_Running":"Yes","Slave_IO_State":"Waiting for master to send event","Slave_SQL_Running":"Yes","Slave_SQL_Running_State":"Slave has read all relay log; waiting for more updates","Until_Condition":"None","Until_Log_File":"","Until_Log_Pos":0}`)
        var f interface{}
        err := json.Unmarshal(ss, &f)
        if err != nil {
            fmt.Println(err)
        }
        m := f.(map[string]interface{})
        mnew := make(map[string]string)
        //val := m[key].(string)
        res := ""
        for k, v := range m {
            switch vv := v.(type) {
            case string:
                //fmt.Println(k, "is string", vv)
                res = vv
                mnew[k] = res
            case int, int8, int16, int32, int64,uint, uint8, uint16, uint32, uint64:
                res = fmt.Sprintf("%v",vv)
                mnew[k] = res
                //fmt.Println(k, "is int", vv)
            case float32:
                res = strconv.FormatFloat(float64(vv),'f',0,64)
                mnew[k] = res
                //fmt.Println(k, "is float32", vv)
            case float64:
                res = strconv.FormatFloat(vv,'f',0,64)
                mnew[k] = res
                //fmt.Println(k, "is float64", res)
            case []interface{}:
                fmt.Println(k, "is an array:")
                for i, u := range vv {
                    fmt.Println(i, u)
                }
            default:
                //fmt.Println(k, "is of a type I don't know how to handle")
                res = fmt.Sprintf("%v",vv)
                mnew[k] = res
            }
        }
        res = mnew[key]
        return res
    }
    
    func main() {
        //获取主从延迟
        res := getJSONValue("Seconds_Behind_Master")
        fmt.Println(res)
    }

     show slave status其实只用到了string,float64两种类型,有其他类型的可以再对方法进行完善

  • 相关阅读:
    基于spark-streaming实时推荐系统
    xgb
    FM算法解析及Python实现
    FM算法
    计算广告
    转发推荐系统文章
    【spark】dataframe常见操作
    VS Code WSL 2 配置 Spring Boot 2
    Makefile
    Paper English
  • 原文地址:https://www.cnblogs.com/perfei/p/10331461.html
Copyright © 2020-2023  润新知