HiSEN

个人技术博客


  • 归档

  • 分类

  • 标签

  • 书单

  • 关于

  • 搜索
close
HiSEN

MySQL统计成功率等 - 利用case when

发表于 2019-02-18 | 分类于 sql |

CONCAT:连接字符串,CONCAT(55.00,’%’)->55.00%
truncate:处理小数点位数,truncate(10.1111,2)->10.11

统计sql

1
2
3
4
5
6
7
8
9
10
11
select res.*, CONCAT(truncate((res.succ / res.`all`) * 100, 2), '%') as 'success rate'
from (select tpc.USER_NO,
tpc.TYPE,
count(*) as 'all',
sum(case when tpc.STATUS = 0 then 1 else 0 end) as 'succ',
sum(case when tpc.STATUS = 1 then 1 else 0 end) as 'fail',
sum(case when tpc.STATUS != 1 && tpc.STATUS != 0 then 1 else 0 end) as 'other'
from t_hisen tpc
where tpc.DATE between '20190216' and '20190217'
group by tpc.USER_NO, tpc.TYPE
order by tpc.USER_NO) res

统计结果

USER_NOTYPEallsuccfailothersuccess rate
4561010010.00%
1231029230679.31%
HiSEN

MySQL - Data truncation:Incorrect datetime value

发表于 2019-02-01 | 分类于 sql |

一、问题

1
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '2039-01-07 12:58:20.625' for column

二、原因

由于程序没有控制好,计算下一次更新时间失误,造成数值过大。

1
2
3
update table_a
SET UPDATE_TIME = '2039-01-07 12:58:20.625'
WHERE ID = 6241

以下是MySQL官方的说法,就是时间超过了范围。

1
2
The TIMESTAMP data type is used for values that contain both date and time parts.
TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

HiSEN

mac docker compose timezone 问题解决

发表于 2018-12-21 | 分类于 docker |

一、背景介绍

想用docker搭建一个lnmp环境

使用这个脚本:

1
https://github.com/buxiaomo/docker-compose/tree/master/lnmp

执行命令之后报错

1
2
3
4
5
6
7
8
9
10
11
12
13
docker-compose -f lnmp.yml up -d
WARNING: Some services (mysql, nginx, php, redis) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
WARNING: Some services (mysql, nginx, php) use the 'configs' key, which will be ignored. Compose does not support 'configs' configuration - use `docker stack deploy` to deploy to a swarm.
lnmp_mysql_1 is up-to-date
Starting lnmp_redis_1 ...
lnmp_nginx_1 is up-to-date
Starting lnmp_redis_1 ... error

ERROR: for lnmp_redis_1 Cannot start service redis: b'Mounts denied: \r\nThe path /usr/share/zoneinfo/Asia/Shanghai\r\nis not shared from OS X and is not known to Docker.\r\nYou can configure shared paths from Docker -> Preferences... -> File Sharing.\r\nSee https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info.\r\n.'

memory: 100M
ERROR: for redis Cannot start service redis: b'Mounts denied: \r\nThe path /usr/share/zoneinfo/Asia/Shanghai\r\nis not shared from OS X and is not known to Docker.\r\nYou can configure shared paths from Docker -> Preferences... -> File Sharing.\r\nSee https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info.\r\n.'
ERROR: Encountered errors while bringing up the project.

二、报错原因

  1. mac机器的时间路径与linux不一样
  2. lnmp.yml redis用到了这个时间

三、解决办法

  1. 查看当前机器时间文件真实位置(/etc/localtime 这个路径是不让共享给docker的)

    1
    2
    ls -la /etc/localtime
    lrwxr-xr-x 1 root wheel 39 10 19 11:13 /etc/localtime -> /var/db/timezone/zoneinfo/Asia/Shanghai
  2. 设置位置 :Docker -> Preferences… -> File Sharing

  3. 新增共享目录:/var/db/timezone
  4. apply & restart
  5. 修改lnmp.yml配置文件 全局替换:/usr/share/zoneinfo/Asia/Shanghai 为:/var/db/timezone/zoneinfo/Asia/Shanghai

四、成功信息

1
2
3
4
5
6
7
8
docker-compose -f lnmp.yml up -d
WARNING: Some services (mysql, nginx, php, redis) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
WARNING: Some services (mysql, nginx, php) use the 'configs' key, which will be ignored. Compose does not support 'configs' configuration - use `docker stack deploy` to deploy to a swarm.
Removing lnmp_redis_1
Starting lnmp_nginx_1 ... done
Starting lnmp_mysql_1 ... done
Recreating f5b9db566588_lnmp_redis_1 ... done
Starting lnmp_php_1 ... done
HiSEN

定时任务的一点思考 - Java

发表于 2018-10-27 | 分类于 java |

一、背景交代

  1. 部署方式:多点
  2. 业务概要:处理一批数据,可以失败重试

二、实现方式

  1. 主表入一条控制数据,两个控制字段:next,process(0:空闲,1:处理中),子表具体处理业务
  2. 定时任务只扫描主表,通过其他业务字段,加上主表两个控制字段(now>netx,process=0),捞出需要处理的数据
  3. 当开始处理数据时候,先更新process=1,如果更新成功,说明拿到锁(假装是一个分布式锁,因为两台机器)
  4. 如果拿到3的锁,则开始处理子表的业务数据,一次查100条(limit 0,100 desc update_time),直到子表全部处理完成
  5. 如果遇到错误,把错误数据的id存起来,更新子表重试次数,和更新时间,如果达到最大失败次数,更改状态(让4查不出这条数据)
  6. 如果遇到当前处理数据的id存在List中,那么说明当前所有的数据都处理过一次了,退出(如果不判断,失败后会无限循环,直到全部成功)

三、伪代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public void process(MainTable mainTable) {

// 1. 锁控制表
boolean lock = service.lock(mainTable);
LOGGER.info("lockFlag:,", lock, ",mainTableId:", mainTable.getId());
if (!lockFlag) {
return;
}
List<Long> repeatIds = new ArrayList<>();
// 2. 根据主表id,下次扫描时间,每次查询100条
List<SubTable> subTables;
do {
//扫描子表中的数据
subTables = service.querySub(mainTable.getId(), 100);
for (SubTable sub : subTables) {
if (repeatIds.contains(batch.getId())) {
subTables = null;
break;
}
// 处理业务
handle(mainTable, subTables, repeatIds);
}
} while (!CollectionUtils.isEmpty(subTables));

// 5. 检查是否全部终态
checkHandleDone(mainTable);
// 7. 释放锁
boolean unLock = service.unLock(mainTable);
LOGGER.info("lockFlag:,", unLock, ",mainTableId:", mainTable.getId());
}
HiSEN

go程序设计语言练习 - 同统计重复 - gop1.io/ch1/dup2/dup2.go

发表于 2018-10-27 | 分类于 go |

一直在写java,看语法逻辑什么的没有问题

但是刚刚看书上写出来的这个程序,居然不知道怎么在goland里面运行…

于是曲线救国,了解到打印输入的参数。

这断代码就是为了找出输入数据中的重复行

  1. 直接启动,不带参数,启动之后输入参数

    1
    2
    3
    4
    5
    参数0: /private/var/folders/lk/p8gbq87n6rvfndk7wlk23y8r0000gn/T/___go_build_dup2_go
    1 # 一行输入一个
    1 # 一行输入一个
    ^D #这是command + D
    2 1 # 输出的结果:个数 输入值
  2. 在命令行启动,带

    1
    2
    3
    4
    5
    6
    $ go run dup2.go 'hisen.txt'
    参数0: /var/folders/lk/p8gbq87n6rvfndk7wlk23y8r0000gn/T/go-build525680776/b001/exe/dup2
    参数1: hisen.txt #代码同级目录的文件名
    2 hisen
    2 hisenyuan
    2 123

代码如下:

阅读全文 »
HiSEN

MySQL批量删除重复数据,保留id最小的一条

发表于 2018-10-16 | 分类于 sql |

stu数据如下:

idname
1hisen
2hisen
3hisen
4hisenyuan
5hisenyuan

删除重复的name,保留id最小的。

思路就是先找出重复数据,然后再找出需要保留的数据(重复中id最小的)

然后删除id不在需要保留的id中的所有数据

1
2
3
4
delete
from stu
where id not in (select t.minId
from (select min(id) minId from stu group by name having count(name) > 1) t)
HiSEN

Could not initialize class io.jsonwebtoken.impl.DefaultJwtBuilder

发表于 2018-10-15 | 分类于 java |

使用jsonwebtoken出现如下错误

1
2
3
Could not initialize class io.jsonwebtoken.impl.DefaultJwtBuilder
java.lang.NoClassDefFoundError: Could not initialize class io.jsonwebtoken.impl.DefaultJwtBuilder
at io.jsonwebtoken.Jwts.builder(Jwts.java:116) ~[jjwt-0.7.0.jar:0.7.0]

原因,因为jackson-databindb版本冲突,直接去掉了依赖

jwt必须依赖Jackson所以报错了

出错时候的配置

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
<exclusions>
<exclusion>
<artifactId>jackson-databind</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>

解决办法

1
2
3
4
5
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>

HiSEN

《Go语言程序设计》 - ch1/lissajous - GIF动画

发表于 2018-09-08 | 分类于 go |

一、程序说明

  1. 可以以web的方式查看,也可以生成一个图片
  2. 直接go run输出的是一丢乱码(图片)
  3. 需要go build 然后运行程序(具体看代码里面注释)

具体的内容可以看下面的程序代码(虽然是抄书)

二、程序代码

阅读全文 »
HiSEN

《穿布鞋的马云》 - 部分文字摘录

发表于 2018-09-08 | 分类于 reading |

《穿布鞋的马云》 2018.10.01 ~ 2018.10.02

国庆假期没有什么安排,看了几部电影之后感觉蛮愧疚,又浪费了大把的时间

于是乎在书架上找了本决定看起来压力不那么大的书来看

这本书感觉整体上写的一般,可能定位就是通俗易懂吧

但是对于了解一些细节,还是很有帮助

由于目前待在创业公司,看到很多文字的时候还是很有感触的

如果你想成功,积极乐观地看待任何问题

以下为摘抄:

阅读全文 »
HiSEN

mac安装Go环境 & IDE(GoLand) & 基本运行方法(标准输入)

发表于 2018-09-08 | 分类于 go |

一、安装GO

1.1 使用Homebrew安装go环境(如果很慢,可以换个源)

1
brew install go

1.2 查看安装信息

1
go env

主要关注如下输出

1
GOROOT="/usr/local/Cellar/go/1.10.3/libexec" # 安装目录

1.3 配置环境变量

1
vi ~/.bash_profile # 没有的话会新建一个文件

输入如下内容,第一行是安装的目录,第三行是工作目录(可以改成自己喜欢的路径)

1
2
3
4
5
GOROOT=/usr/local/Cellar/go/1.10.3/libexec
export GOROOT
export GOPATH=/Users/hisenyuan/golang
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN:$GOROOT/bin

1.4 让配置文件生效并且查看环境变量

1
2
source ~/.bash_profile
go env # 这时你会发现环境变量已经有改变

二、安装GoLand

我是习惯了用jetbrains的idea

发现它家也有go语言的IDE GoLand

于是就去官网下载,安装,找个注册码,修改一下host防止注册码失效

这里就不再累赘了

三、运行需要输入的程序

买了一本《Go语言程序设计》

阅读全文 »
12…18
HiSEN

HiSEN

Java R & D

178 日志
18 分类
83 标签
GitHub
Links
  • 梦殇国际
© 2016 - 2019 HiSEN
由 Hexo 强力驱动
您是第  个访问者    |   
主题 - NexT.Mist