package com.zf.es.test;
public class Logger {
private String info;
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
package com.zf.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.core.io.ClassPathResource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class ESTest_Doc_Insert_logger {
public static Properties prop = null;
public static int port;
public static int threadNum;
public static int bulkNum;
public static int corePoolSize;
public static int maximumPoolSize;
public static int keepAliveTime;
public static int workQueue;
public static String[] nameArr;
public static String[] infoArr;
public static String index = "";
private static ThreadPoolExecutor threadPoolExecutor = null;
//private static AtomicInteger atomicInteger = new AtomicInteger(0);
private static RestHighLevelClient esClient = null;
public static void main(String[] args) throws Exception {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String startTime = df.format(new Date());
System.out.println(startTime + "---程序开始执行!");
try {
//读取配置文件
if (prop == null) {
prop = new Properties();
try {
prop.load(new FileInputStream("es.properties")); //读取配置文件
} catch (IOException e) {
e.printStackTrace();
}
}
port = Integer.parseInt(prop.getProperty("port"));//端口号
index = prop.getProperty("index"); //索引名称
threadNum = Integer.parseInt(prop.getProperty("threadNum"));//线程数
bulkNum = Integer.parseInt(prop.getProperty("bulkNum")); //单次条数
corePoolSize = Integer.parseInt(prop.getProperty("corePoolSize"));//核心线程池大小
maximumPoolSize = Integer.parseInt(prop.getProperty("maximumPoolSize"));//最大线程池大小
keepAliveTime = Integer.parseInt(prop.getProperty("keepAliveTime"));//线程最大空闲时间
workQueue = Integer.parseInt(prop.getProperty("workQueue"));//线程等待数列
//创建客户端
esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", port, "http"))
);
//用户数组
nameArr = new String[]{"白百合", "白冰", "陈钰琪", "陈冲", "陈红", "陈妍希", "陈意涵", "陈乔恩", "陈紫涵", "楚月", "程愫",
"蔡依林", "陈数", "蔡少芬", "陈美琪", "陈晓旭", "陈瑶", "程瑷瑗", "迪丽热巴", "邓家佳"};
//模块信息数组
infoArr = new String[]{"组织架构", "员工管理", "考勤管理", "薪资核算", "预算管理", "绩效考核", "招聘管理", "会议管理", "行政管理", "报销管理",
"调休申请", "采购管理", "转正申请", "请假管理", "培训管理", "用户注册", "运维管理", "会议管理", "文化建设", "财务管理"};
//线程池
threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>(workQueue), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < threadNum; i++) {
threadPoolExecutor.execute(() -> {
try {
create();
} catch (IOException e) {
e.printStackTrace();
}
});
}
threadPoolExecutor.shutdown();
while (!threadPoolExecutor.awaitTermination(2, TimeUnit.SECONDS)) {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
String endTime = df.format(new Date());
System.out.println(endTime + "---程序执行完成!共插入"+threadNum*bulkNum+"条数据");
esClient.close();
System.exit(0);
}
}
public static void create() throws IOException {
//System.out.println("执行子线程...");
// 批量插入数据
BulkRequest request = new BulkRequest();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
// StringBuffer buffer = new StringBuffer();
Random random = new Random();
//往ES批量插入日志
for (int i = 0; i < bulkNum; i++) {
int nameRandom = random.nextInt(nameArr.length);
int infoRandom = random.nextInt(infoArr.length);
String time = df.format(new Date());
String s = time + "-----用户[" + nameArr[nameRandom] + "]-----访问了" + infoArr[infoRandom] + "模块接口!";
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
request.add(new IndexRequest().index(index).id(uuid).source(XContentType.JSON, "info", s));
//System.out.println(time + "---用户[" + nameArr[nameRandom] + "]---访问了" + infoArr[infoRandom] + "模块接口!");
}
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
}
}
port=9200
index=logger2
threadNum=100
bulkNum=10000
corePoolSize=2
maximumPoolSize=6
keepAliveTime=5000
workQueue=4