gpt4 book ai didi

java - 来自 java 的 KIBANA 弹性查询

转载 作者:行者123 更新时间:2023-11-30 07:32:43 25 4
gpt4 key购买 nike

我想从 java 触发此 REST GET API cal

 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

那么我如何从服务器获取此类 Elasticsearch 的响应。我在谷歌和堆栈上找到了很多,但没有得到任何适当的回应。我希望这个 java 程序应该运行这个查询并返回该输出。但这个 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 无关,也不是有效的协议(protocol)。

更新

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

    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

关于java - 来自 java 的 KIBANA 弹性查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35860452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com