java—为elasticsearch客户机构建yammer度量并将其放在jmx端口上

kfgdxczn  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(0)|浏览(231)

嗨,我已经写了一个基本的elasticsearch客户端,我想做一个 Package 。客户机将通过该es Package 器触发查询。我希望 Package 器指示各种统计信息,如查询频率、每个查询所用的时间、所用的总时间,以便我可以对服务器上的客户机进行优先级排序。为此,我发现yammer度量是一个很好的选择。下面是基本的本机客户端。我想给这个做个 Package 。

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

我从中发现了一些关于yammer metric的信息http://www.javacodegeeks.com/2012/12/yammer-metrics-a-new-way-to-monitor-your-application.html . 我想构造一个yammer度量并在jmx端口上显示它。最好的/标准的方法是什么?如何将本机客户机代码与度量代码混合?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题