Java elasticsearch 基本使用
程序员文章站
2022-07-05 12:31:08
...
今天使用到,所以记录了下来,可以搭配 kinaba 使用会更加方便,废话少说。
以下示例是一个解析日志的场景
- 添加依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.3.2</version>
</dependency>
- 初始化客户端
private static final RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")
)
);
不是长连接的话,最好可以有 close 函数
public void close(RestHighLevelClient client) {
LOGGER.info("Closing elasticSearch client");
if (client != null) {
try {
client.close();
} catch (IOException e) {
LOGGER.error("Error closing ElasticSearch client: " + e.getMessage());
}
}
}
- 查询某条记录的详情
kinaba DTS
GET http-log/kong/hp8rpXABLfkHYzAW6xxf
java
if (index != null && type != null && id != null) {
try {
GetRequest getRequest = new GetRequest(index, type, id);
GetResponse getResponse = restHighLevelClient.get(getRequest);
if (getResponse.isExists()) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = getResponse.getSourceAsMap();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey() == "started_at") {
map.put("started_at", timeStampToDate(entry.getValue()));
}
}
list.add(map);
return list;
}
} catch (IOException e) {
LOGGER.info("catch IOException error: " + e.getMessage());
}
}
- 多条件查询: 获取某个时间段的 get 请求,不返回 hits 数据
kinaba DTS
GET /http-log/kong/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"match": {
"request.uri": "/get"
}
},
{
"range": {
"started_at": {
"gte": 1573429400000,
"lte": 1583852400000
}
}
}
]
}
}
}
java
// 开始时间 -> 结束时间 + request uri 查询
if (index != null && type != null && startTime != null && endTime != null && path != null) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder = paging(searchSourceBuilder, number, page);
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("started_at").gte(startTime).lte(endTime);
searchSourceBuilder.sort(new FieldSortBuilder("started_at").order(SortOrder.DESC));
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(ConstSet.REQUEST_URI, path);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(rangeQueryBuilder).must(termQueryBuilder);
searchSourceBuilder.query(boolQueryBuilder);
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types(type);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse response = restHighLevelClient.search(searchRequest);
SearchHits searchHits = response.getHits();
long totalHits = searchHits.getTotalHits();
LOGGER.info("totalHits value is: " + totalHits);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (SearchHit hit : searchHits) {
Map<String, Object> map = hit.getSourceAsMap();
map.put("total", totalHits);
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey() == "started_at") {
map.put("started_at", timeStampToDate(entry.getValue()));
}
}
list.add(hit.getSourceAsMap());
}
return list;
} catch (IOException e) {
LOGGER.info("catch IOException error: " + e.getMessage());
}
}
- 组合聚合查询,按照时间范围对请求方式进行分组
kinaba DTS
GET /http-log/kong/_search
{
"query": {
"range": {
"started_at": {
"gte": 1553515800000,
"lte": 1593852400000
}
}
},
"size": 0,
"aggs": {
"group": {
"terms": {
"field": "request.method.keyword",
"size": 10
}
}
}
}
java
if (index != null && type != null && startTime != null && endTime != null && path != null) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder = paging(searchSourceBuilder, number, page);
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("started_at").gte(startTime).lte(endTime);
searchSourceBuilder.sort(new FieldSortBuilder("started_at").order(SortOrder.DESC));
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(ConstSet.REQUEST_URI, path);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(rangeQueryBuilder).must(termQueryBuilder);
searchSourceBuilder.query(boolQueryBuilder);
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types(type);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse response = restHighLevelClient.search(searchRequest);
SearchHits searchHits = response.getHits();
long totalHits = searchHits.getTotalHits();
LOGGER.info("totalHits value is: " + totalHits);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (SearchHit hit : searchHits) {
Map<String, Object> map = hit.getSourceAsMap();
map.put("total", totalHits);
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey() == "started_at") {
map.put("started_at", timeStampToDate(entry.getValue()));
}
}
list.add(hit.getSourceAsMap());
}
return list;
} catch (IOException e) {
LOGGER.info("catch IOException error: " + e.getMessage());
}
}
上一篇: 使用双缓冲依然无法解决frame闪屏问题
下一篇: 分布式锁
推荐阅读
-
JavaEE基础day02 1.定义Java中的变量 四类八种 2.变量定义和使用的注意事项 3.数据类型的转换、强制数据类型转换4.算数运算符、比较运算符、逻辑运算符、赋值运算符、三元运算符
-
webpack3、4的基本的使用方法
-
JQuery 基本使用、操作样式、简单动画
-
Python爬虫之pandas基本安装与使用方法示例
-
记一次使用JAVA上转型对象出现的问题
-
Linux下MySQL的一些基本使用方法_MySQL
-
《PHP核心技术与最佳实践》PHP使用PHPRPC协议调用JAVA类
-
详解python中的json的基本使用方法
-
对于Java中常使用的注解,你都是是怎么实现的?
-
springboot整合MybatisPlus基本使用