ES Java API - 获取索引下数据量

需求

获取ES某个索引下的数据总量

代码示例

  • 引包

    import net.sf.json.JSONObject;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.elasticsearch.action.ActionFuture;
    import org.elasticsearch.action.admin.indices.stats.IndexStats;
    import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest;
    import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
    import org.elasticsearch.action.bulk.*;
    import org.elasticsearch.action.index.IndexRequest;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.Client;
    import org.elasticsearch.client.IndicesAdminClient;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.InetSocketTransportAddress;
    import org.elasticsearch.common.unit.ByteSizeUnit;
    import org.elasticsearch.common.unit.ByteSizeValue;
    import org.elasticsearch.common.unit.TimeValue;
    import org.elasticsearch.search.SearchHit;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.Map;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
  • 获取 es 连接

        public static void startupClient(String serverIp, String clusterName, Integer port) throws UnknownHostException {
        /**
         * 可以设置client.transport.sniff为true来使客户端去嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中,
         * 这样做的好 处是一般你不用手动设置集群里所有集群的ip到连接客户端,它会自动帮你添加,并且自动发现新加入集群的机器。
         */
        org.elasticsearch.common.settings.Settings settings = Settings.builder().put("cluster.name", clusterName)
                .build();
    
        // Settings settings =
        // ImmutableSettings.settingsBuilder().put("client.transport.sniff",
        // true).put("cluster.name", "liw_test").build();
        client = TransportClient.builder().settings(settings).build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(serverIp), port));
    
        // System.out.println(INDEX_DEMO_01 + "是否存在?-》" +
        // client.admin().indices().exists(new
        // IndicesExistsRequest(INDEX_DEMO_01)).actionGet().isExists());
    }
  • 执行操作

        /**
     * 获取索引doc数量
     * @param index,type
     * Elastic Search Java API 2.4
     */
    public static int getClusterStatus(String index, String type) {
    
        @Deprecated
        //long count = client.prepareCount(index).get().getCount();
        //System.out.println(count);
    
        String response = client.prepareSearch(index)
                .setSize(0)
                .execute()
                .actionGet()
                .toString();
    
        JSONObject JsonResponse = JSONObject.fromObject(response);
        JSONObject JsonResponse_hits = JSONObject.fromObject(JsonResponse.get("hits"));
        String count_num = JsonResponse_hits.get("total").toString();
    
        //System.out.println(JsonResponse.get("hits"));
    
        return Integer.valueOf(count_num);
    
    }
  • 关闭连接

    /**
    • on shutDownClient 停止es
      */
      public static void shutDownClient() {
      client.close();
      }

你可能感兴趣的:(ES)