为ElasticSearch客户端构建yammer度量标准并将其放在JMX端口上

时间:2015-05-22 07:41:33

标签: java elasticsearch jmx yammer

您好我已经编写了一个基本的ElasticSearch客户端,我想为它做一个包装器。客户端将通过该ES包装器触发查询。我希望包装器指示各种统计信息,例如查询频率,每个查询所花费的时间,所花费的总时间,以便我可以优先考虑服务器上的客户端。 为此,我发现yammer度量标准是一个不错的选择。以下是基本的本机客户端。我正在尝试为此制作一个包装器。

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.indices.IndexAlreadyExistsException;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.node.Node;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static org.elasticsearch.node.NodeBuilder.nodeBuilder;

public class PracticeES
{
    public Client client;
    public Node node;
    public Settings settings;
    BufferedReader br;
    public PracticeES(String cluster_name) throws IOException
    {
        node = nodeBuilder().clusterName(cluster_name).node();
        settings = ImmutableSettings.settingsBuilder()
                .put("cluster.name", "elasticsearch").put("client.transport.sniff", true).build();
        client = new TransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(host1, 9300));
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    public void indexES() throws IOException
    {
        System.out.println("Enter name, age & moto!");
        String name,age,moto;
        name=br.readLine();
        age=br.readLine();
        moto=br.readLine();
        String json = "{" +
                "\"user\":\""+name+"\"," +
                "\"age\":\""+age+"\"," +
                "\"moto\":\""+moto+"\"" +
                "}";
        System.out.println("Enter index!");
        String index=br.readLine();
        try
        {
            IndexResponse response = client.prepareIndex("practice", "ES", index)
                    .setSource(json)
                    .execute()
                    .actionGet();
        }
        catch(IndexAlreadyExistsException e)
        {
            System.out.println("Warning ! Index Already Exists. So document not indexed!");
        }
        finally
        {
            System.out.println("Executed");
        }
    }

    public void getES() throws IOException
    {
        System.out.println("Enter index!");
        String index=br.readLine();
        try
        {
            GetResponse response = client.prepareGet("practice", "ES", index).execute().actionGet();
            System.out.println("The response is " + response.getSourceAsString());
        }
        catch (IndexMissingException e)
        {
            System.out.println("Index is Missing !");
        }
        catch (Exception e)
        {
            System.out.println("Some Exception !");
            e.printStackTrace();
        }
    }

    public void deleteES() throws IOException {
        System.out.println("Enter index to be Deleted!");
        String index = br.readLine();
        try
        {
            DeleteResponse response = client.prepareDelete("practice", "ES", index)
                    .execute()
                    .actionGet();
            System.out.println("Deleted");
        }
        catch (IndexMissingException e)
        {
            System.out.println("Index is Missing !");
            return;
        }
        catch (Exception e)
        {
            System.out.println("Some Exception !");
            e.printStackTrace();
            return;
        }
    }
    public void searchES()throws IOException
    {

        SearchResponse response = client.prepareSearch("practice")
                .setTypes("ES")
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.termQuery("moto", "paise"))             // Query
                .setPostFilter(FilterBuilders.rangeFilter("age").from(15).to(18))   // Filter
               // .setFrom(0).setSize(60).setExplain(true)
                .execute()
                .actionGet();
        System.out.println(response.toString());
    }

    public void closeClient() throws IOException
    {
        client.close();
    }
}

我从http://www.javacodegeeks.com/2012/12/yammer-metrics-a-new-way-to-monitor-your-application.html发现了一些yammer指标。我想构建一个yammer度量标准并将其显示在JMX端口上。这样做的最佳/标准方法是什么?我应该如何将本机客户端代码与度量标准代码混合使用?

0 个答案:

没有答案