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()); } } }