• Elasticsearch6.1.0 TransportClient聚合查询索引中所有数据


    import org.elasticsearch.action.index.IndexResponse;
    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.common.unit.TimeValue;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.elasticsearch.index.query.RangeQueryBuilder;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    import org.elasticsearch.search.aggregations.AggregationBuilders;
    import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
    import org.elasticsearch.transport.client.PreBuiltTransportClient;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    
    public class EsToExport {
    
        private TransportClient client;
    
        private static final String FILE_PATH = "C:\Users\admin\Desktop\大数据\es\";
    
        @BeforeEach
        public void test1() throws UnknownHostException {
            Settings settings = Settings.builder().put("cluster.name", "L_ES").build();
            client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.100.101"), 9301)).addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.100.102"), 9301));
        }
    
    
        /**
         * 聚合操作
         */
        @Test
        public void aggregation() {
            SearchResponse response = client.prepareSearch("w_app")
                    .setTypes("doc")
                    .addAggregation(AggregationBuilders.terms("x_dip").field("x_dip").size(1000))
                    .execute().actionGet();
            StringTerms terms = response.getAggregations().get("x_dip");
            for (StringTerms.Bucket bucket : terms.getBuckets()) {
                System.out.println(bucket.getKey() + "," + bucket.getDocCount());
            }
    
    
        }
    
    }
  • 相关阅读:
    git上传本地项目
    第十一章 持有对象
    java 闭包与回调
    类名.class 类名.this 详解
    匿名内部类
    第十章 内部类
    Java简单调用Zookeeper服务
    Linux下ZooKeeper集群安装
    Linux自动化安装JDK
    linux下初步实现Keepalived+Nginx高可用
  • 原文地址:https://www.cnblogs.com/chong-zuo3322/p/12974835.html
Copyright © 2020-2023  润新知