標籤:show 分享 als transport splay except void cat sock
測試彙總查詢功能
1 package com.juyun.test; 2 3 import java.net.InetAddress; 4 import java.util.List; 5 6 import org.elasticsearch.action.search.SearchResponse; 7 import org.elasticsearch.client.Client; 8 import org.elasticsearch.common.settings.Settings; 9 import org.elasticsearch.common.transport.InetSocketTransportAddress;10 import org.elasticsearch.index.query.QueryBuilder;11 import org.elasticsearch.index.query.QueryBuilders;12 import org.elasticsearch.search.SearchHit;13 import org.elasticsearch.search.SearchHits;14 import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;15 import org.elasticsearch.search.aggregations.AggregationBuilder;16 import org.elasticsearch.search.aggregations.AggregationBuilders;17 import org.elasticsearch.search.aggregations.bucket.children.Children;18 import org.elasticsearch.search.aggregations.bucket.terms.Terms;19 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;20 import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;21 import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;22 import org.elasticsearch.transport.client.PreBuiltTransportClient;23 24 public class ElasticsearchAggregation {25 26 private static Client client;27 28 /**29 * 彙總查詢30 * @param args31 */32 public static void main(String[] args) {33 34 try {35 // 設定叢集名稱 36 Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();37 // 建立client38 client = new PreBuiltTransportClient(settings)39 .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.16.0.157"), 9300));40 41 long startTime=System.currentTimeMillis(); // 擷取開始時間 42 43 agg("flow", "data");44 //childQuery("newindex","http");45 46 long endTime=System.currentTimeMillis(); //擷取結束時間47 System.out.println("程式已耗用時間: "+(endTime-startTime)+"ms");48 49 50 // 關閉client51 client.close();52 53 } catch (Exception e) {54 e.printStackTrace();55 }56 }57 58 // 計算inbyte和outbyte的平均值59 public static void agg(String indexName, String typeName) {60 SearchResponse response = client61 .prepareSearch(indexName)62 .setTypes(typeName)63 .addAggregation(AggregationBuilders.avg("avgOfInbyte").field("inbyte"))64 .addAggregation(AggregationBuilders.avg("avgOfOutbyte").field("outbyte"))65 .addAggregation(AggregationBuilders.sum("sumOfInbyte").field("inbyte"))66 .addAggregation(AggregationBuilders.sum("sumOfOutbyte").field("outbyte"))67 .get();68 69 InternalAvg avgOfInbyte = response.getAggregations().get("avgOfInbyte");70 InternalAvg avgOfOutbyte = response.getAggregations().get("avgOfOutbyte");71 InternalSum sumOfInbyte = response.getAggregations().get("sumOfInbyte");72 InternalSum sumOfOutbyte = response.getAggregations().get("sumOfOutbyte");73 System.out.println("inbyte的平均值是:"+avgOfInbyte.getValue());74 System.out.println("outbyte的平均值是:"+avgOfOutbyte.getValue());75 System.out.println("inbyte的總值是:"+sumOfInbyte.getValue());76 System.out.println("outbyte的總值是:"+sumOfOutbyte.getValue());77 }78 }ElasticsearchAggregation
Elasticsearch5.0 Java Api(七) -- 彙總查詢