-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathes_query1.java
More file actions
63 lines (58 loc) · 2.73 KB
/
es_query1.java
File metadata and controls
63 lines (58 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package es_test;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class es_query1 {
public static void main(String[] args) //throws Exception
{
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("elastic", "oracle"));
RestClientBuilder builder = RestClient.builder(new HttpHost("100.73.18.55", 9201),
new HttpHost("100.73.18.56", 9201)).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
RestHighLevelClient client = new RestHighLevelClient(builder);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
SearchRequest request = new SearchRequest("antmq_broker_consumer-2018-09-05");
request.source(searchSourceBuilder);
try {
SearchResponse response = client.search(request);
SearchHits hits = response.getHits();
SearchHit[] result = hits.getHits();
for (SearchHit searchHit : result) {
Map<String, Object> json = searchHit.getSourceAsMap();
// String s=searchHit.getSourceAsString();
Set keyset=json.keySet();
Iterator<String> iterator=keyset.iterator();
while (iterator.hasNext()){
String key=iterator.next();
Object value=json.get(key);
System.out.println(key+" "+value.toString());
}
}
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}