elasticsearch(7) : javaApi
<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.4.2</version> <exclusions> <exclusion> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </exclusion> <exclusion> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.4.2</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.4.2</version> </dependency>
springboot java代码
import com.alibaba.fastjson.JSON; import com.aliyun.autodeploy.agent.service.bo.LogSourceBO; import lombok.extern.slf4j.Slf4j; import org.apache.commons.beanutils.BeanUtils; import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * @Author: liyue * @Date: 2021/12/10/17:41 * @Description: */ @Service @Slf4j public class ElasticsearchService { private RestHighLevelClient restHighLevelClient; @Value("${es.hostname}") private String esHostname; @Value("${es.port}") private Integer esPort; @PostConstruct private void run() { // 设置IP HttpHost esHost = new HttpHost(esHostname, esPort); RestClientBuilder restClientBuilder = RestClient.builder(esHost); setTimeout(restClientBuilder); restHighLevelClient = new RestHighLevelClient(restClientBuilder); } /** * 设置超时时间 */ private void setTimeout(RestClientBuilder restClientBuilder) { restClientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) { return builder.setConnectTimeout(3000) .setSocketTimeout(50000); } }); } /** * 新增 * @return 保存结果 * @throws Exception exception */ public IndexResponse save(Object o, String index){ try { IndexRequest indexRequest = new IndexRequest(index); indexRequest.source(JSON.toJSONString(o), XContentType.JSON); indexRequest.id(); return restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT); } catch (Exception e) { log.error("插入数据失败",e); return null; } } /** * 批量新增 * * @param list 数据list * @return 返回保存结果 * @throws Exception exception */ public BulkResponse batchSave(List<LogSourceBO> list, String index){ try { log.info("数据发送ES,size:{},index:{}",list.size(),index); BulkRequest bulkRequest = new BulkRequest(); IndexRequest indexRequest; for (LogSourceBO item : list) { indexRequest = new IndexRequest(index); indexRequest.source(JSON.toJSONString(item), XContentType.JSON); indexRequest.id(); bulkRequest.add(indexRequest); } return restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); }catch (Exception e){ log.error("批量保存失败",e); return null; } } /** * 查询符合查询条件的所有数据 * * @return 返回结果list * @throws Exception exception */ public List<LogSourceBO> query(Long startTime, Long endTime, String index) { List<LogSourceBO> logSourceBOS = new LinkedList<>(); try { SearchRequest request = new SearchRequest(); request.indices(index); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); QueryBuilder queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.rangeQuery("timestamp") .gte(startTime) .lte(endTime)); searchSourceBuilder.query(queryBuilder); request.source(searchSourceBuilder); SearchResponse res = restHighLevelClient.search(request, RequestOptions.DEFAULT); if (res.getHits().getHits() != null) { SearchHit[] hits = res.getHits().getHits(); for (SearchHit hit : hits) { Map<String, Object> resultOne = hit.getSourceAsMap(); logSourceBOS.add(convertMap2Model(resultOne)); } } }catch (Exception e){ log.error("查询失败",e); } return logSourceBOS; } private LogSourceBO convertMap2Model(Map<String, Object> map) throws Exception { LogSourceBO logSourceBO = new LogSourceBO(); BeanUtils.populate(logSourceBO, map); return logSourceBO; } }
https://blog.csdn.net/Lxinccode/article/details/121934702
https://blog.csdn.net/qq_30468551/article/details/118120336
https://www.cnblogs.com/qlqwjy/p/13520785.html
https://mp.weixin.qq.com/s/EYEw101cmFfqWK_jcuG9MQ
故乡明