|
|
|
@ -1,30 +1,22 @@ |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import com.qniao.iot.machine.event.MachineIotDataReceivedEvent; |
|
|
|
import com.qniao.iot.machine.event.generator.config.ApolloConfig; |
|
|
|
import com.qniao.iot.machine.event.generator.constant.ConfigConstant; |
|
|
|
import org.apache.http.HttpHost; |
|
|
|
import org.apache.http.auth.AuthScope; |
|
|
|
import org.apache.http.auth.UsernamePasswordCredentials; |
|
|
|
import org.apache.http.client.CredentialsProvider; |
|
|
|
import org.apache.http.impl.client.BasicCredentialsProvider; |
|
|
|
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; |
|
|
|
import org.elasticsearch.action.search.SearchRequest; |
|
|
|
import org.elasticsearch.action.search.SearchResponse; |
|
|
|
import org.elasticsearch.client.*; |
|
|
|
import org.elasticsearch.client.indices.GetIndexRequest; |
|
|
|
import org.elasticsearch.client.indices.GetIndexResponse; |
|
|
|
import org.elasticsearch.cluster.metadata.AliasMetaData; |
|
|
|
import org.elasticsearch.index.query.QueryBuilders; |
|
|
|
import org.elasticsearch.client.RequestOptions; |
|
|
|
import org.elasticsearch.client.RestClient; |
|
|
|
import org.elasticsearch.client.RestHighLevelClient; |
|
|
|
import org.elasticsearch.rest.RestStatus; |
|
|
|
import org.elasticsearch.search.SearchHit; |
|
|
|
import org.elasticsearch.search.SearchHits; |
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders; |
|
|
|
import org.elasticsearch.search.aggregations.Aggregations; |
|
|
|
import org.elasticsearch.search.aggregations.metrics.ParsedSum; |
|
|
|
import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder; |
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder; |
|
|
|
import org.elasticsearch.search.sort.SortOrder; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.math.BigDecimal; |
|
|
|
|
|
|
|
public class DemoTes { |
|
|
|
|
|
|
|
@ -47,7 +39,7 @@ public class DemoTes { |
|
|
|
|
|
|
|
try { |
|
|
|
// 构建查询条件(注意:termQuery 支持多种格式查询,如 boolean、int、double、string 等,这里使用的是 string 的查询) |
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
|
|
|
/*SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
|
|
|
searchSourceBuilder.query(QueryBuilders.termQuery("machineIotMac", "102104060102")); |
|
|
|
searchSourceBuilder.sort("reportTime", SortOrder.DESC); |
|
|
|
searchSourceBuilder.size(1); |
|
|
|
@ -64,6 +56,27 @@ public class DemoTes { |
|
|
|
MachineIotDataReceivedEvent receivedEvent = JSONUtil |
|
|
|
.toBean(reqHit.getSourceAsString(), MachineIotDataReceivedEvent.class); |
|
|
|
System.out.println(receivedEvent); |
|
|
|
}*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SearchSourceBuilder searchSourceBuilder1 = new SearchSourceBuilder(); |
|
|
|
searchSourceBuilder1.size(0); |
|
|
|
SumAggregationBuilder sumCurrStoppingDuration = AggregationBuilders.sum("sum_currStoppingDuration").field("currStoppingDuration"); |
|
|
|
SumAggregationBuilder sumCurrWaitingDuration = AggregationBuilders.sum("sum_currWaitingDuration").field("currWaitingDuration"); |
|
|
|
searchSourceBuilder1.aggregation(sumCurrStoppingDuration); |
|
|
|
searchSourceBuilder1.aggregation(sumCurrWaitingDuration); |
|
|
|
SearchRequest request1 = new SearchRequest("machine_iot_data_received_event_*"); |
|
|
|
request1.source(searchSourceBuilder1); |
|
|
|
// 执行请求 |
|
|
|
SearchResponse response1 = restHighLevelClient.search(request1, RequestOptions.DEFAULT); |
|
|
|
Aggregations aggregations1 = response1.getAggregations(); |
|
|
|
if (RestStatus.OK.equals(response1.status()) || aggregations1 != null) { |
|
|
|
ParsedSum agg = aggregations1.get("sum_currStoppingDuration"); |
|
|
|
ParsedSum agg1 = aggregations1.get("sum_currWaitingDuration"); |
|
|
|
double value = agg.getValue(); |
|
|
|
BigDecimal bigDecimal = new BigDecimal(value); |
|
|
|
System.out.println(bigDecimal.longValueExact()); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
|