1.首先在配置文件中需要配置kafaka和es服务器的参数(建议作为配置参数,不要在代码写死)http请求的端口是9200,客户端是9300
需要在pom.xml中添加jar依赖:
org.elasticsearch.client transport 6.4.22.以下为es链接查询的一个java实例,包括了新增日志,分页查询,排序,模糊查询等。
参考官网的说明文档:
https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.6/index.html
package com.znv.aiot.server.utils;
import com.github.pagehelper.PageInfo;
import com.znv.aiot.server.cache.Cache;
import com.znv.aiot.server.config.EnvConfig;
import com.znv.aiot.server.module.logs.model.Logs;
import com.znv.log.log2kafka.LogMsg;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.*;
public class LogManageUtils {
private static Logger logger = LoggerFactory.getLogger(LogManageUtils.class);
private static String logName="log";
private static TransportClient getClient() throws IOException {
String host = "10.72.66.145";
int port = 9300;
try {
port = EnvConfig.getLogsPort();
host = EnvConfig.getLogsHost();
logName