• 大小: 14KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-14
  • 语言: Java
  • 标签: ES  

资源简介

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.xml
     目录           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\meta-INF\
     文件         107  2017-09-05 16:50  RestClient\target\classes\meta-INF\MANIFEST.MF
     目录           0  2017-09-05 16:50  RestClient\target\classes\meta-INF\maven\
............此处省略9个文件信息

评论

共有 条评论