【发布时间】:2016-03-08 06:24:23
【问题描述】:
我想从 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
那么我如何从服务器获取响应以进行这种弹性搜索。 我在谷歌和堆栈上发现了很多,但没有得到任何适当的回应。 我希望这个 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}]
【问题讨论】:
-
你确定你在
new URL("localhost:9200/");中的URL真的是你编译的吗?除了 curl 你需要写curl -XPOST而不仅仅是curl -x -
实际上有一些 URL,但我不允许在堆栈上发布该 URL,所以我将其保留为 localhost,我也尝试使用 -XGET 但同样的问题来了
-
你能确定你的 URL 构造函数
new URL("...")中没有“curl ...”吗?您只需要一个以“http://...”开头的 URL。看起来你拥有的是new URL("curl -x GET http://.../"),但你只需要new URL("http://.../") -
new URL("") 在这个构造函数中我已经传递了完整的 curl 代码,这是有问题的,然后它给我一个没有协议的错误:curl
-
我修改了问题并发布了使用 Elastic Get Calls 的新代码
标签: java rest elasticsearch get kibana