工作笔记

常用脚本

linux后台跑jar包

1
nohup java -jar xxx.jar > /dev/null 2>&1 &

定时重启jar

restart.sh

  • 不指定日志输出 使用log组件输出日志
1
2
3
4
#!/bin/bash
pid=`ps -ef | grep xxx.jar | grep -v grep| awk {'print $2'}`
kill -9 $pid
nohup java -jar xxx.jar > /dev/null 2>&1 &
  • 指定日志输出
1
2
3
4
5
6
#!/bin/bash
current_date="`date +%Y-%m-%d`"
pid=`ps -ef | grep xxx.jar | grep -v grep| awk {'print $2'}`
log_path="./logs/${current_date}.log"
kill -9 $pid
nohup java -jar xxx.jar > ${log_path} 2>&1 &
1
0 3 * * * restart.sh

linux后台跑py

1
nohup python main.py > /dev/null 2>&1 &

fetch_data.sh

1
2
ls_date=`date +%Y%m%d`
nohup python /usr/sql_tool/datax_tool/main.py > /usr/sql_tool/datax_tool/logs/${ls_date}.log 2>&1 &
1
crontab -e
1
0 1 * * * fetch_data.sh

windows后台跑jar

start.bat

1
2
3
4
5
6
7
8
@echo off
title job start ...
color 0A
start javaw -jar xxx.jar >> D:\log.file &
echo .
echo ----job start success----
pause
exit

stop.bat

1
2
3
4
5
6
7
8
@echo off
title job stop ...
color 0A
taskkill -f -t -im javaw.exe
echo .
echo ---job stop sucess---
pause
exit

windows powerShell发http请求

1
curl -Uri 'http://localhost:8080/homePage/upload' -Method 'GET'

定时清理日志

编辑定时任务

1
crontab -e

添加定时任务

1
0 0 * * * cat /dev/null > xxx.log

删除一个月前的日志文件

1
rm -f `find / -name "*.log" -mtime +30`

导入jar包到本地maven仓库

1
mvn install:install-file -Dfile=xxx.jar -DgroupId=com.xxx -DartifactId=mqsdk -Dversion=1.0 -Dpackaging=jar

windows查看端口占用

1
netstat -ano | find 端口号

杀掉进程

1
taskkill /f /t /im 进程id

jar包解压

1
jar -xvf xxx.jar

Mysql查看表名的所有字段

1
SELECT column_name FROM information_schema.columns WHERE table_schema='dbNamw' AND table_name='tableName';

Mysql查看列的属性

1
describe table_name column_name;

Mysql修改列属性

1
alter table table_name modify column column_name varchar(255);

Mysql添加列

1
alter table table_name add column column_name varchar(255);

Mysql重命名表

1
ALTER TABLE table_name_old RENAME table_name_new;

sqlplus连接oracle

1
sqlplus64 username/password@ip:port/orcl

orcle查询最近7天

1
admission_date between to_char(sysdate-7,'yyyy-mm-dd hh24:mi:ss') and to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')

查找文件路径

1
find / -name xxx.jar

根据进程号查看文件路径

1
ll /proc/PID

常用方法

时间段切分方法

1
2
3
4
5
6
7
8
9
10
11
public class DateRange {
/**
* 起始时间
*/
private String startDate;

/**
* 结束时间
*/
private String endDate;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

public static List<DateRange> getDateRangesByHours(String startDateStr, String endDateStr, int step) {
LocalDateTime startDate = LocalDateTime.parse(startDateStr, FORMATTER);
LocalDateTime endDate = LocalDateTime.parse(endDateStr, FORMATTER);
List<DateRange> dateRanges = new ArrayList<>();
while (startDate.isBefore(endDate)) {
DateRange dateRange = new DateRange();
LocalDateTime tempEndDate = startDate.plusHours(step);
if (tempEndDate.isBefore(endDate)) {
dateRange.setStartDate(startDate.format(FORMATTER));
dateRange.setEndDate(tempEndDate.format(FORMATTER));
} else {
dateRange.setStartDate(startDate.format(FORMATTER));
dateRange.setEndDate(endDate.format(FORMATTER));
}
startDate = startDate.plusHours(step);
dateRanges.add(dateRange);
}
return dateRanges;
}

&lt;&gt;转换成<>

1
2
3
4
5
String unescapeStr = "<name>zhangsan</name>";
escapeStr = StringEscapeUtils.escapeXml(unescapeStr);
log.info(escapeStr);//打印&lt;name&gt;zhangsan&lt;/name&gt;
unescapeStr1 = StringEscapeUtils.unescapeXml(escapeStr);
log.info(unescapeStr1);//打印<name>zhangsan</name>

正则去除<![CDATA[]]>

1
2
3
private static final String regex = "<!\\[CDATA\\[(.*?)\\]\\]>";
String strWithCdata = "<![CDATA[<name>zhangsan</name>]]>";
String strWithoutCdata = strWithCdata.replaceAll(regex, "$1");// <name>zhangsan</name>

数据加密与解密

DES对称加密算法

工具类 DESUtils

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 解密
*
* @param src 数据源
* @param key 密钥,长度必须是8的倍数
* @return 返回解密后的原始数据
* @throws Exception
*/
public static String decrypt(byte[] src, byte[] key) {
try{
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建一个DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
// 现在,获取数据并解密
// 正式执行解密操作
return new String(cipher.doFinal(src)).replaceAll("\\*", "-").trim();
}catch(Exception e){
log.error(e.getMessage());
}
}

/**
* 加密
*
* @param src 数据源
* @param key 密钥,长度必须是8的倍数
* @return 返回加密后的数据
*/
public static byte[] encrypt(byte[] src, byte[] key) {
try {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
// 现在,获取数据并加密
// 正式执行加密操作
return cipher.doFinal(src);
} catch (Exception e) {
log.error(e.getmessage());
}
}
public static String encryptBASE64(byte[] key) {
return Base64.getEncoder().encodeToString(key);
}

调用

1
2
3
4
5
6
String mobileNo = "15027075282"
private static final String SECRET_KEY = "@WSX3edc";
// 加密
String mobileNoEncryt = DESUtil.encryptBASE64(DESUtil.encrypt(mobileNo.getBytes(StandardCharsets.UTF_8), SECRET_KEY)
// 解密
String mobileNoDecrypt = new String(DESUtils.decrypt(DESUtils.decryptBASE64(mobileNoEncryt), SECRET_KEY.getBytes())

多线程数据造测试数据

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
static class BaseDao {  
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

// 获取数据库连接对象
public static Connection getConn() {
Connection conn = null;
try {
// rewriteBatchedStatements=true,一次插入多条数据,只插入一次
conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1/common-test?autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8",
"root", "1111");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return conn;
}

public static void closeAll(AutoCloseable... autoCloseables) {
for (AutoCloseable autoCloseable : autoCloseables) {
if (autoCloseable != null) {
try {
autoCloseable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

@Test
public void testMultiThreadLoadData() throws InterruptedException {

ThreadPoolExecutor executor = ThreadPoolService.executor;
final CountDownLatch countDownLatch = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
executor.execute(()->{
testLoadData();
countDownLatch.countDown();
});

}
try {
//主线程等待所有统计指标执行完毕
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public void testLoadData() {
long start = System.currentTimeMillis();
Connection conn = BaseDao.getConn();
String sql = "insert into xl_flup_appointment_test(`name`, `content`, " +
"`survey_type`) " +
"VALUES(?,?,?)";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
// 不断产生sql
for (int i = 0; i < 1000; i++) {
ps.setString(1, "糖尿病数据");
ps.setString(2, "test111");
ps.setString(3, "2");
ps.addBatch(); // 将一组参数添加到此 PreparedStatement 对象的批处理命令中。
}
int[] ints = ps.executeBatch();// 将一批命令提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。
if (ints.length > 0) {
System.out.println("已成功添加1000条数据!!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
BaseDao.closeAll(conn, ps);
}
long end = System.currentTimeMillis();
System.out.println("所用时长:" + (end - start) / 1000 + "秒");
}

List对象指定字段去重

1
List<User> userList = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getId))),ArrayList::new));

List分批

1
List<List<User>> users = Lists.partition(list,1000);