Docker启动测试写入查询Elasticsearch.
安装
docker pull elasticsearch:7.5.1 # 拉镜像
docker pull kibana:7.5.1 #拉kibana 镜像
#创建相同的网络
docker network create somenetwork
docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.5.1
docker run -d --name kibana --net somenetwork -p 5601:5601 kibana:7.5.1
打开 http://127.0.0.1:5601 查看 kibana 是否启动成功,如果出现 kibana not start xxx yet , 说明kibana启动没有成功,此时应该关闭重启。
docker ps
docker stop xxxxxxxx
docker start xxxxxxxx # 重启镜像
出现kibana界面说明启动成功
Java环境
添加依赖
SpringBoot版本: 2.2.2.RELEASE
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
/**
* @author chenshun00
* @since 2020/1/3 3:36 PM
*/
@Service
public class ESClientFactory {
//todo 换
private static final String HOST = "es01";
//todo 换
private static final int PORT = 9200;
private static final String SCHEMA = "http";
private static final int CONNECT_TIME_OUT = 5000;
private static final int SOCKET_TIME_OUT = 40000;
private static final int CONNECTION_REQUEST_TIME_OUT = 5000;
private static final int MAX_CONNECT_NUM = 100;
private static final int MAX_CONNECT_PER_ROUTE = 100;
private static HttpHost HTTP_HOST = new HttpHost(HOST, PORT, SCHEMA);
private static RestClientBuilder builder;
private static RestHighLevelClient restHighLevelClient;
static {
init();
}
private static void init() {
builder = RestClient.builder(HTTP_HOST);
setConnectTimeOutConfig();
setMutiConnectConfig();
restHighLevelClient = new RestHighLevelClient(builder);
}
private static void setConnectTimeOutConfig() {
builder.setRequestConfigCallback(requestConfigBuilder -> {
requestConfigBuilder.setConnectTimeout(CONNECT_TIME_OUT);
requestConfigBuilder.setSocketTimeout(SOCKET_TIME_OUT);
requestConfigBuilder.setConnectionRequestTimeout(CONNECTION_REQUEST_TIME_OUT);
return requestConfigBuilder;
});
}
private static void setMutiConnectConfig() {
builder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setMaxConnTotal(MAX_CONNECT_NUM);
httpClientBuilder.setMaxConnPerRoute(MAX_CONNECT_PER_ROUTE);
return httpClientBuilder;
});
}
public static RestHighLevelClient getHighLevelClient() {
return restHighLevelClient;
}
}
客户端中使用 RestHighLevelClient 进行查询插入操作.
bulk
class Go implements Runnable {
@Override
public void run() {
BulkRequest request = new BulkRequest();
for (int j = 0; j < 500; j++) {
int ii = RandomUtils.nextInt(0, 100);
Map<String, Object> result = new HashMap<>();
result.put("app_id", "xxxx");
result.put("user_id", "xxxx-" + ii);
result.put("created", new Date());
result.put("updated", new Date());
result.put("data_status", "normal");
result.put("int_1", RandomUtils.nextInt(10000, 1000000));
result.put("float_1", RandomUtils.nextFloat(10000F, 1000000F));
result.put("input_1", RandomStringUtils.randomAlphabetic(100, 300));
result.put("text_1", textMap.get(RandomUtils.nextInt(0, 16)));
result.put("date_1", new Date());
request.add(new IndexRequest(index, "_doc", RandomStringUtils.randomAlphanumeric(30)).source(result, XContentType.JSON));
try {
Thread.sleep(20L);
} catch (InterruptedException ignore) {
}
}
try {
//批量操作
BulkResponse bulk = ESClientFactory.getHighLevelClient().bulk(request, RequestOptions.DEFAULT);
log.info("[{}]", JSONObject.toJSONString(bulk));
} catch (IOException e) {
e.printStackTrace();
}
}
}