Python Golang Java DevOps Programmer

GORM,MySQL,PG

package main import ( "GORM/orm/dao" "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" "time" ) type Gist struct { Id int64 Url string Snip string Kword string Md5 string language string } func main() { const ( mysqladdr = "root:[email protected](127.0.0.1:3306)/goods?charset=utf8mb4&parseTime=true&loc=Local" postgaddr = `host=5432 user=postgres dbname=goods sslmode=disable password=123456` ) //psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", "127.0.0.1", 5432, "postgres", "123456", "goods") db, err := gorm.Open("mysql", mysqladdr) db.LogMode(true) if err != nil { fmt. [Read More]

postgres01

pg常用命令 \password:设置当前登录用户的密码 \h:查看SQL命令的解释,比如\h select。 \?:查看psql命令列表。 \l:列出所有数据库。 \c [database_name]:连接其他数据库。 \d:列出当前数据库的所有表格。 \d [table_name]:列出某一张表格的结构。 \du:列出所有用户。 \e:打开文本编辑器。 \conninfo:列出当前数据库和连接的信息。 \password [user]: 修改用户密码 \q:退出 # Connect to PostgreSQL, using a superuser named 'postgres' psql -U postgres -h localhost pg 测试数据生成 select id from generate_series(1,10) t(id); #随机数 select (random()*100)::int from generate_series(1,10); #生成随机字符串 select md5(random()::text) from generate_series(1,10); #随机汉字 create or replace function gen_hanzi(int) returns text as $$ declare res text; begin if $1 >=1 then select string_agg(chr(19968+(random()*20901)::int), '') into res from generate_series(1,$1); return res; end if; return null; end; $$ language plpgsql strict; select gen_hanzi(10) from generate_series #随机数组 create or replace function gen_rand_arr(int,int) returns int[] as $$ select array_agg((random()*$1)::int) from generate_series(1,$2); $$ language sql strict; # 随机身份证 create or replace function gen_id( a date, b date ) returns text as $$ select lpad((random()*99)::int::text, 2, '0') || lpad((random()*99)::int::text, 2, '0') || lpad((random()*99)::int::text, 2, '0') || to_char(a + (random()*(b-a))::int, 'yyyymmdd') || lpad((random()*99)::int::text, 2, '0') || random()::int || (case when random()*10 >9 then 'X' else (random()*9)::int::text end ) ; $$ language sql strict; select gen_id('1900-01-01', '2017-10-16') from generate_series(1,10); gin 倒排索引 CREATE INDEX "name " ON "tablename" USING gin(to_tsvector('jiebacfg', "columnname")); CREATE INDEX rumidx ON rum_test USING rum (c1 rum_tsvector_ops); pg 查询 4#全文检索 select * from tbl where tsvector_col @@ 'postgres & china | digoal:A' order by ts_rank(tsvector_col, 'postgres & china | digoal:A') limit xx; 5正则查询。(可以使用pg_trgm和gin索引) select * from tbl where col ~ '^a[0-9]{1,5}\ +digoal$'; 6、相似查询。(可以使用pg_trgm和gin索引) select * from tbl order by similarity(col, 'postgre') desc limit 10; 7ADHOC查询,任意字段组合查询。(通过bloom index, multi-index bitmap scan, gin-index bitmap scan 等索引都可以实现) select * from tbl where a=? [Read More]

postgres02

pg upsert .5+ UPSERT用法举例 创建一张测试表,其中一个字段为唯一键或者主键。 create table test(id int primary key, info text, crt_time timestamp); 1. 不存在则插入,存在则更新 test03=# insert into test values (1,'test',now()) on conflict (id) do update set info=excluded.info,crt_time=excluded.crt_time; INSERT 0 1 test03=# select * from test; id | info | crt_time ----+------+---------------------------- 1 | test | 2017-04-24 15:27:25.393948 (1 row) test03=# insert into test values (1,'hello ,now()) on conflict (id) do update set info=excluded.info,crt_time=excluded.crt_time; INSERT 0 1 test03=# select * from test; id | info | crt_time ----+--------------+---------------------------- 1 | hello | 2017-04-24 15:27:39. [Read More]

k8s struct

package k8s import ( "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" "cloud/util" "cloud/models/hosts" ) // 配置服务存储数据 type StorageData struct { // 宿主机路径 HostPath string // 容器挂载路径 ContainerPath string // 分布式卷名称 Volume string // 读写权限 Model int // 只读挂载 ReadOnly bool } // 亲和性配置结构 // 必须在服务器进行标签配置 // 2018-01-11 type Affinity struct { // 类型 disk service server Type string // 值 Value string } // 选择节点配置 // 2018-01-11 15;57 // 只能选择一个机器 // kubernetes. [Read More]

k8s replicas-set

apiVersion: v1
kind: ReplicationController 
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

k8s pods

Pod 基础

$ kubectl get pods   # 获取所有正在运行的POD
$ kubectl get pods -o wide   # 获取pod的更多信息,比如在哪台k8s机器上
$ kubectl describe pod <pod>   #获取一个POD的详细信息
$ kubectl exec <pod> <cmd>     #在pod里的container里执行一个命令,如果这个pod有多个container,默认会在第一个里执行,或者通过-c去指
定哪个

pod_nginx.yml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

golang 实现blockchain

package main import ( "crypto/sha256" "encoding/json" "flag" "fmt" "io" "log" "net/http" "sort" "strings" "time" "golang.org/x/net/websocket" ) const ( queryLatest = iota queryAll responseBlockchain ) var genesisBlock = &Block{ Index: 0, PreviousHash: "0", Timestamp: 1465154705, Data: "my genesis block!!", Hash: "816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7", } var ( sockets []*websocket.Conn blockchain = []*Block{genesisBlock} httpAddr = flag.String("api", ":3001", "api server address.") p2pAddr = flag.String("p2p", ":6001", "p2p server address.") initialPeers = flag.String("peers", "ws://localhost:6001", "initial peers") ) type Block struct { Index int64 `json:"index"` PreviousHash string `json:"previousHash"` Timestamp int64 `json:"timestamp"` Data string `json:"data"` Hash string `json:"hash"` } func (b *Block) String() string { return fmt. [Read More]

golang 读写excel

golang 读取excel package main import ( "fmt" "net/http" "errors" "strings" "gitee.com/johng/gf/g/os/gtime" "gitee.com/johng/gf/g/util/gregex" "github.com/GiterLab/aliyun-sms-go-sdk/dysms" "github.com/astaxie/beego/logs" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/json" "github.com/golang/glog" "github.com/pborman/uuid" "github.com/tealeg/xlsx" ) var ( accessKeyId = "" accessKeySecret = "" signName = "" templateParam = make(map[string]string) failList = []string{} ) func main() { r := gin.Default() r.Static("/", "./static") r.POST("/upload", Upload) r.Run(":6000") } func Upload(c *gin.Context) { templatecode := c.PostForm("templatecode") if templatecode == "" { return } file, err := c.FormFile("file") if err ! [Read More]

k8s

mac 安装minikube brew install kubectl curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v0.25.2/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/ minikube start --registry-mirror=https://registry.docker-cn.com minikube dashboard kubectl cluster-info 查看当前集群信息 我们可以通过minikube ssh进到虚机里,然后看看是否有一些container运行起来了 ➜ ~ minikube ssh _ _ _ _ ( ) ( ) ___ ___ (_) ___ (_)| |/') _ _ | |_ __ /' _ ` _ `\| |/' _ `\| || , < ( ) ( )| '_`\ /'__`\ | ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )( ___/ (_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____) $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c1aec3464788 gcr. [Read More]

GORM 高级用法

``` %like%查询 result :=[]*dao.Product{} dao.DB().Table("product").Where("name like ?","%提高%").Find(&result) fmt.Println(result) ## 自定义JsonP 类型 type JsonP struct { T map[string]interface{} } func (p JsonP) Value() (driver.Value, error) { if p ==nil{ return "",nil } j, _ := json.Marshal(p.T) return j, nil } func (p *JsonP) Scan(src interface{}) error { if p ==nil{ return errors.New("p is nil") } source, ok := src.([]byte) if !ok { return errors.New("Type assertion .([]byte) failed.") } var i interface{} err := json. [Read More]