zoukankan      html  css  js  c++  java
  • 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());
            }
    
    
        }
    
    }
  • 相关阅读:
    使用C#编写SqlHelper类
    编译器perspective oo 对象模型(1) 之 初窥c++对象模型
    浅谈 编译器 & 自然语言处理
    基于c#的角色扮演游戏设计与实现
    开源的EtherCAT Master简介
    如何在Windows中编译Linux Unix的代码(采用cygwin)?
    sql拼语句例子
    IOC介绍-手写一个简单的IOC
    protocalBuffer_java版详解(转thanks)
    ProtocalBuffer_数据结构(转thanks)
  • 原文地址:https://www.cnblogs.com/chong-zuo3322/p/12974835.html
Copyright © 2011-2022 走看看