来自 java 的 KIBANA ElasticQuery

KIBANA ElasticQuery from java

我想启动这个 REST GET API cal from java

 curl -x 'http://localhost:9200' -d '{
     "size": 0,
     "aggs": {
       "2": {
         "terms": {
           "field": "message_user_uid",
           "size": 10,
           "order": {
             "_count": "desc"
           }
         }
       }
     },
     "query": {
       "filtered": {
         "query": {
           "query_string": {
             "query": "_key: \"my-demo-exchange-*\" AND message_message: \"success\"",
             "analyze_wildcard": true
           }
         },
         "filter": {
           "bool": {
             "must": [
               {
                 "range": {
                   "@timestamp": {
                     "gte": 1457261289759,
                     "lte": 1457347689760,
                     "format": "epoch_millis"
                   }
                 }
               }
             ],
             "must_not": []
           }
         }
       }
     },
     "highlight": {
       "pre_tags": [
         "@kibana-highlighted-field@"
       ],
       "post_tags": [
         "@/kibana-highlighted-field@"
       ],
       "fields": {
         "*": {}
       },
       "require_field_match": false,
       "fragment_size": 2147483647
     }
    }'

JAVA 代码

try {
            URL url = new URL("above curl expression");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));
            String output="",line;
            System.out.println("Output from Server .... \n");
            while ((line = br.readLine()) != null) {
                    output+=line+"\n";
            }
            conn.disconnect();
            System.out.println(output);
        } catch (Exception e) {
            e.printStackTrace();
        } 

但是它给出了

的错误
java.net.MalformedURLException: no protocol: curl -x

那么如何从服务器获取这种弹性搜索的响应。 我在 google 和堆栈上找到了很多,但没有得到任何正确的回应。 我希望这个 java 程序应该 运行 这个查询并且 return 我那个输出。 但是这个 SIMPLE REST GET Call 程序在这种情况下不起作用。

现在我正在使用这个代码

Client client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("kibana host IP Address"), 9200));
        GetResponse getResponse=client.prepareGet().execute().get();
        System.out.println(getResponse.toString());

但是它给我一个错误

Error : None of the configured nodes are available: [{#transport#-1}{host IP}{host IP:9200}]

您需要确保在 URL 构造函数中没有出现 "curl ..."。

您只需要一个以 "http://..." 开头的 URL,尽管看起来您拥有的是

new URL("curl -x GET http://.../") 

但你只需要

new URL("http://.../")

curl 是您在 shell 中 运行 的独立命令,它与 Java 无关,也不是有效的协议。

更新

为了在负载中发送查询,您需要将代码更改为:

    URL url = new URL("http://localhost:9200");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Accept", "application/json");
    connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
    writer.write(" input your query in here");
    writer.close();
    // proceed to read the response