工作记录
- java日常
- 判断字符串是否为小数,正式
- java保留两位小数
- 代码实现list数据分组
- 异常
- feign.FeignException: status 500 reading
- feign.RetryableException: timeout executing POST
- java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0
- Java source1.6不支持diamond运算符,请使用source 7或更高版本以启用diamond运算符
- clickhouse jdbc报错:Too many partitions for single INSERT block (more than 100)
- 数据库
- 泛型对象将指定字段制空
- 笛卡尔积方法
- Mac 相关
java日常
将1变成001
//定义long型count为1
Long count1 =1;
//实例化format,格式为“000”
Format f1=new DecimalFormat("000");
//将1变为001
String count =f1.format(count1);
对数计算
public static void main(String[] args) {
double v = Math.log(new Double(100)) / Math.log(new Double(10));
System.out.println(v);
//2.0
}
判断字符串是否为小数,正式
public static void main(String[] args) {
Pattern p1 = Pattern.compile("-?\\d+");//判断数字
Pattern p2 = Pattern.compile("(\\d+\\.\\d+)");//判断小数
Matcher m = p1.matcher("64.7");
System.out.println(m.matches());
}
java保留两位小数
double f = 111231.5585;
System.out.println(String.format("%.2f", f));
代码实现list数据分组
poiStatistics.stream().skip((bo.getPageNum() - 1) * bo.getPageSize()).limit(bo.getPageSize()).collect(Collectors.toList());
即:跳过【(pageNum-1)*pageSize】个数据之后的,前pageSize个数据
异常
feign.FeignException: status 500 reading
解决:被远程调用的方法或是使用远程调用方法的方法存在异常
参考博客
feign.RetryableException: timeout executing POST
解决:看到是http请求报错超时,feign的调用分两层,ribbon的调用和hystrix的调用,高版本的hystrix默认是关闭的,所以在application.properties配置文件中设置ribbon即可
#请求处理的超时时间
ribbon.ReadTimeout: 120000
#请求连接的超时时间
ribbon.ConnectTimeout: 30000
java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0
Feign调用远程微服务接口的参数中在注解@RequestParam上书写错误,未指定参数key
- 错误代码
public DtoResponseList<CityCodeDto> getCityCodeByCityLevel( @RequestParam(required = true) String cityLevel, @RequestParam(required = true) Integer city300);
- 修改
Java source1.6不支持diamond运算符,请使用source 7或更高版本以启用diamond运算符
未指定Maven编译的JDK版本,如果未指定编译的JDK版本,maven会默认使用1.5版本,就会报diamond 表达式不支持;
maven pom文件新增JDK编译版本
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
clickhouse jdbc报错:Too many partitions for single INSERT block (more than 100)
调整了建表时PARTITION BY分区字段,这个字段在同一批次写入里包括大于100个分区值,clickhouse认为这样会存在性能问题,抛出异常;
数据库
mysql group by
sql查询出现1055 this is incompatible with sql_mode=only_full_group_by
原SQL
select city_name AS cityName, city_code AS cityCode, sum(supply_plan_num) as supplyPlan, sum(initail_land_num) as initailLand, sum(zpg_land_num) as zpgLand, sum(hbxy_land_num) as hbxyLand, sum(deal_land_num ) as dealLand, sum(hp_land_num)as hpLand, sum(hp_zpg_land_num) as hpZpgLand, sum(hp_supply_plan_num) as hpSupplyPlan
FROM dws_land_statistics
WHERE logday='2021-08-09' group by city_code;
修改之后的
select ANY_VALUE(city_name) AS cityName, ANY_VALUE(city_code) AS cityCode, sum(supply_plan_num) as supplyPlan, sum(initail_land_num) as initailLand, sum(zpg_land_num) as zpgLand, sum(hbxy_land_num) as hbxyLand, sum(deal_land_num ) as dealLand, sum(hp_land_num)as hpLand, sum(hp_zpg_land_num) as hpZpgLand, sum(hp_supply_plan_num) as hpSupplyPlan
FROM dws_land_statistics
WHERE logday='2021-08-09' group by city_code;
原因分析
在MySQL5.7.5后,默认开启了ONLY_FULL_GROUP_BY,所以导致了之前的一些SQL无法正常执行,其实,是我们的SQL不规范造成的,因为group by 之后,返回的一些数据是不确定的,所以才会出现这个错误。
批量删除数据库表
生成删除语句,做一个sql脚本,执行sql脚本
Select CONCAT( 'drop table ', table_name, ';' )
FROM information_schema.tables
Where table_name LIKE 'workfrom_10000194_null_%' or table_name LIKE 'workfrom_10000118_null_%';
然后把sql语句保存为脚本
执行sql脚本;
Mysql AB表切换
LOCK TABLES t1 WRITE, t2 WRITE;
ALTER TABLE t1 RENAME TO t3;
ALTER TABLE t2 RENAME TO t1;
ALTER TABLE t3 RENAME TO t2;
UNLOCK TABLES;
SQLServer 实现分页操作
参考博客
此方法对SQLserver 版本有要求
select q.cityName AS cityName,q.POIName AS POIName,q.Address AS Address, q.level AS level
FROM (
SELECT a.iID as id, a.scity AS cityName,a.sName AS POIName,a.sAddress AS Address, d.name AS level
FROM City_POI_Standard_NanJing a
INNER JOIN city_poi_grade d
ON a.sCityID = d.city_id and a.grade_id = d.id and d.is_del = 0
WHERE a.iState=1 and a.uniform_type_id=43 ) q,
(
SELECT TOP 20 row_number() OVER (ORDER BY dAddTime DESC) n, iID as id
FROM City_POI_Standard_NanJing a
INNER JOIN city_poi_grade d
ON a.sCityID = d.city_id and a.grade_id = d.id and d.is_del = 0
WHERE a.iState=1 and a.uniform_type_id=43
) w
WHERE q.id = w.id AND w.n >=1 ORDER BY w.n ASC;
解除输入的安全模式
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.
这是因为MySql运行在safe-updates模式下,该模式会导致非主键条件下无法执行update或者delete命令。
-- 查看开关状态。
show variables like ‘SQL_SAFE_UPDATES’;
-- 修改下数据库模式
执行命令SET SQL_SAFE_UPDATES = 0;
泛型对象将指定字段制空
/**
* 方法描述
* @param bo
* @return java.lang.Object
* @author guochao.bj@fang.com
* @date 2022/1/14
*/
private static <T> T changeToNull( T bo,String fieldName){
Class<?> aClass = bo.getClass();
try {
Field declaredField = aClass.getDeclaredField(fieldName);
declaredField.setAccessible(true);
declaredField.set(bo,null);
} catch (Exception e) {
e.printStackTrace();
}
return bo;
}
笛卡尔积方法
/***
* @description: 笛卡尔集
* @param: lists
* @return: java.util.List<java.lang.String>
* @author guochao.bj@fang.com
* @date: 2022/11/8 19:24
*/
public static List<String> descartes(List<Set<String>> lists) {
List<String> tempList = new ArrayList<>();
for (Set<String> list : lists) {
if (tempList.isEmpty()) {
tempList = list.stream().collect(Collectors.toList());
} else {
tempList = tempList.stream().flatMap(item ->
list.stream().map(item2 -> item + item2))
.collect(Collectors.toList());
}
}
return tempList;
}
Mac 相关
mac相关命令
scp的基本语法
$ scp [option] /path/to/source/file user@server-ip:/path/to/destination/directory
1
/path/to/source/file – 这是打算复制到远程主机的源文件。
user@server-IP: – 这是远程系统的用户名和 IP 地址。请注意 IP 地址后面加冒号。
/path/to/destination/directory – 这是文件将复制到的远程系统上的目标目录。
以下是scp命令常用的几个选项:
-C - 这会在复制过程中压缩文件或目录。
-P - 如果默认 SSH 端口不是 22,则使用此选项指定 SSH 端口。
-r - 此选项递归复制目录及其内容。
-p - 保留文件的访问和修改时间。
ssh远程连接
-
连接其他主机
可以通过ssh命令连接其他主机,例如:
ssh username@192.168.100.100 -
连接其他服务器
也可以通过ssh命令连接其他服务器,例如:ssh -p 端口 用户名@服务器ip地址