Indices Stats using the NodeClient with the Java API

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Indices Stats using the NodeClient with the Java API

Marc-2
Hi,

I would like to get a list of the available indices in my cluster using the java api using the node client.

Currently the request is done via the REST interface similar to this:
http://localhost:9200/logstash-*/_stats/indices

Cheers
Marc

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/e3966486-9cdc-49b7-a657-ff22b2b5a772%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Indices Stats using the NodeClient with the Java API

InquiringMind
Marc,

Maybe these snippets will help?

The enclosing class's constructor sets the client data member to either a Node client or Transport client (both work fine; I prefer the TransportClient).

The source string contains one or more index names, with a comma between each pair of names. A name may contain wildcards as supported by ES.

This code works for 1.3.4. Not sure if 4.X has yet another breaking change, but if that's the case, it is usually no big deal to handle.

public static String[] parseIndex(String source)
{
  return indexSplitter.split(source);
}

public String[] getIndexNames(String indexPattern) throws UtilityException
{
  if (indexPattern.trim().isEmpty())
    throw new UtilityException("Cannot resolve empty index pattern ["
        + indexPattern + "]");

  try
  {
    /*
     * Parse the index pattern on commas, if present. Then pass the list of
     * individual names (which may include wildcards and - signs) to
     * Elasticsearch for final resolution
     */
    String[] indexSpecList = parseIndex(indexPattern);

    /*
     * Get the list of individual index names, along with their status
     * information (which we will ignore in this method)
     */
    IndicesAdminClient iac = client.admin().indices();
    RecoveryRequestBuilder isrb = iac.prepareRecoveries();
    isrb.setIndices(indexSpecList);
    RecoveryResponse isr = isrb.execute().actionGet();

    /* Create an array of just the names of the indices */
    ArrayList<String> indices = new ArrayList<String>();
    Map<String, List<ShardRecoveryResponse>> sr = isr.shardResponses();
    for (String index : sr.keySet())
    {
      indices.add(index.trim());
    }

    /* Be sure there is at least one index that matches the pattern */
    if (indices.isEmpty())
      throw new UtilityException("Cannot resolve index pattern ["
          + indexPattern + "] to at least one existing index");

    /* Convert to String[] and return */
    return indices.toArray(new String[indices.size()]);
  }
  catch (ElasticsearchException e)
  {
    throw new UtilityException("Cannot resolve index pattern ["
        + indexPattern + "]: " + e);
  }
}

private final Client         client;

private static final Pattern indexSplitter = Pattern.compile(Pattern
                                                 .quote(","));


Brian


On Tuesday, January 13, 2015 at 6:56:07 AM UTC-5, Marc wrote:
Hi,

I would like to get a list of the available indices in my cluster using the java api using the node client.

Currently the request is done via the REST interface similar to this:
<a href="http://localhost:9200/logstash-*/_stats/indices" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Flocalhost%3A9200%2Flogstash-*%2F_stats%2Findices\46sa\75D\46sntz\0751\46usg\75AFQjCNF7cGUMfBBTTVJln_Qt0D-SW4u23Q';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Flocalhost%3A9200%2Flogstash-*%2F_stats%2Findices\46sa\75D\46sntz\0751\46usg\75AFQjCNF7cGUMfBBTTVJln_Qt0D-SW4u23Q';return true;">http://localhost:9200/logstash-*/_stats/indices

Cheers
Marc

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/49ed97eb-6c2f-4c56-a551-ebc86c9559ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.