資源簡介
Elasticsearch5.5.1 JAVA客戶端RestClient示例代碼,詳細介紹:http://blog.csdn.net/u011781521/article/details/77853571

代碼片段和文件信息
package?com.fendo.RestClient;
import?java.io.IOException;
import?java.util.Collections;
import?org.apache.http.HttpEntity;
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.entity.ContentType;
import?org.apache.http.impl.client.BasicCredentialsProvider;
import?org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import?org.apache.http.nio.entity.NStringEntity;
import?org.apache.http.util.EntityUtils;
import?org.elasticsearch.action.search.SearchResponse;
import?org.elasticsearch.client.Response;
import?org.elasticsearch.client.RestClient;
import?org.elasticsearch.client.RestClientBuilder;
import?org.elasticsearch.index.query.QueryBuilder;
import?org.elasticsearch.index.query.QueryBuilders;
import?org.junit.Before;
import?org.junit.Test;
/**
?*?Elasticserach?RestClient示例
?*?@author?fendo
?*
?*/
public?class?Rest?{
private?static?RestClient?restClient;
public?void?getRestClient(){
????????final?CredentialsProvider?credentialsProvider?=?new?BasicCredentialsProvider();
????????credentialsProvider.setCredentials(AuthScope.ANY
????????????????new?UsernamePasswordCredentials(“elastic“?“changeme“));
????
????????restClient?=?RestClient.builder(new?HttpHost(“localhost“9200“http“))
????????????????.setHttpClientConfigCallback(new?RestClientBuilder.HttpClientConfigCallback()?{
????????????????????@Override
????????????????????public?HttpAsyncClientBuilder?customizeHttpClient(HttpAsyncClientBuilder?httpClientBuilder)?{
????????????????????????return?httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
????????????????????}
????????????????}).build();
????????
}
@Before
public?void?getRest(){
restClient?=?RestClient.builder(new?HttpHost(“localhost“?9200?“http“)).build();?
}
/**
?*?查看api信息
?*?@throws?Exception
?*/
@Test
public?void?CatApi()?throws?Exception{
????????String?method?=?“GET“;
????????String?endpoint?=?“/_cat“;
????????Response?response?=?restClient.performRequest(methodendpoint);
????????System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
?*?創建索引
?*?@throws?Exception
?*/
@Test
public?void?CreateIndex()?throws?Exception{
????????String?method?=?“PUT“;
????????String?endpoint?=?“/test-index“;
????????Response?response?=?restClient.performRequest(methodendpoint);
????????System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
?*?創建文檔
?*?@throws?Exception
?*/
@Test
public?void?CreateDocument()throws?Exception{
????????String?method?=?“PUT“;
????????String?endpoint?=?“/test-index/test/1“;
????????HttpEntity?entity?=?new?NStringEntity(
????????????????“{\n“?+
????????????????????????“????\“user\“?:?\“kimchy\“\n“?+
????????????????????????“????\“post_date\“?:?\“2009-11-15T14:12:12\“\n“?+
????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-09-05?16:26??RestClient\
?????文件????????1024??2017-09-05?16:39??RestClient\.classpath
?????文件?????????562??2017-09-05?16:26??RestClient\.project
?????目錄???????????0??2017-09-05?16:26??RestClient\.settings\
?????文件?????????119??2017-09-05?16:26??RestClient\.settings\org.eclipse.core.resources.prefs
?????文件?????????736??2017-09-05?16:38??RestClient\.settings\org.eclipse.jdt.core.prefs
?????文件??????????90??2017-09-05?16:26??RestClient\.settings\org.eclipse.m2e.core.prefs
?????文件????????2272??2017-09-05?16:48??RestClient\pom.xm
?????目錄???????????0??2017-09-05?16:26??RestClient\src\
?????目錄???????????0??2017-09-05?16:26??RestClient\src\main\
?????目錄???????????0??2017-09-05?16:26??RestClient\src\main\java\
?????目錄???????????0??2017-09-05?16:26??RestClient\src\main\java\com\
?????目錄???????????0??2017-09-05?16:26??RestClient\src\main\java\com\fendo\
?????目錄???????????0??2017-09-05?16:50??RestClient\src\main\java\com\fendo\RestClient\
?????文件????????6883??2017-09-05?16:59??RestClient\src\main\java\com\fendo\RestClient\Rest.java
?????目錄???????????0??2017-09-05?16:26??RestClient\src\test\
?????目錄???????????0??2017-09-05?16:26??RestClient\src\test\java\
?????目錄???????????0??2017-09-05?16:26??RestClient\src\test\java\com\
?????目錄???????????0??2017-09-05?16:26??RestClient\src\test\java\com\fendo\
?????目錄???????????0??2017-09-05?16:26??RestClient\src\test\java\com\fendo\RestClient\
?????文件?????????686??2017-09-05?16:26??RestClient\src\test\java\com\fendo\RestClient\AppTest.java
?????目錄???????????0??2017-09-05?16:26??RestClient\target\
?????目錄???????????0??2017-09-05?16:50??RestClient\target\classes\
?????目錄???????????0??2017-09-05?16:50??RestClient\target\classes\com\
?????目錄???????????0??2017-09-05?16:50??RestClient\target\classes\com\fendo\
?????目錄???????????0??2017-09-05?16:50??RestClient\target\classes\com\fendo\RestClient\
?????文件????????1291??2017-09-05?16:59??RestClient\target\classes\com\fendo\RestClient\Rest$1.class
?????文件????????5462??2017-09-05?16:59??RestClient\target\classes\com\fendo\RestClient\Rest.class
?????目錄???????????0??2017-09-05?16:50??RestClient\target\classes\me
?????文件?????????107??2017-09-05?16:50??RestClient\target\classes\me
?????目錄???????????0??2017-09-05?16:50??RestClient\target\classes\me
............此處省略9個文件信息
- 上一篇:android歡迎界面源碼
- 下一篇:httpclient-4.3.6.jar
評論
共有 條評論