Java连接ElasticSearch(low-level-rest-client)的配置和数据的增加/查询

作者: admin 分类: ELK 发布时间: 2018-12-28 22:15  阅读: 616 views

配置ELK之后,可以发挥elasticsearch的更大功效,就是用java连接elastic进行数据的存储,达到快速读取的目的。可以取代redis、或者solr的功能。在一些搜索服务功能、或者列表服务功能处可以恰当的使用。

POM依赖

<dependency>
	  <groupId>org.elasticsearch</groupId>
	  <artifactId>elasticsearch</artifactId>
	  <version>6.4.2</version>
</dependency>
<dependency>
	  <groupId>org.elasticsearch.client</groupId>
	  <artifactId>elasticsearch-rest-high-level-client</artifactId>
	  <version>6.4.2</version>
</dependency>
<dependency>
	  <groupId>org.elasticsearch.client</groupId>
	  <artifactId>elasticsearch-rest-client</artifactId>
	  <version>6.4.2</version>
</dependency>
<dependency>
	  <groupId>org.apache.httpcomponents</groupId>
	  <artifactId>httpcore-nio</artifactId>
	  <version>4.4.6</version>
</dependency>
<dependency>
	  <groupId>org.apache.httpcomponents</groupId>
	  <artifactId>httpasyncclient-osgi</artifactId>
	  <version>4.0.1</version>
</dependency>
<dependency>
	  <groupId>org.apache.httpcomponents</groupId>
	  <artifactId>httpcore-niossl</artifactId>
	  <version>4.0-alpha5</version>
</dependency>
<dependency>
	  <groupId>org.elasticsearch.plugin</groupId>
	  <artifactId>shield</artifactId>
	  <version>2.4.3-atlassian-shaded-1</version>
</dependency>

下面是Java代码处理

package com.kaistart;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.ParseException;
import org.apache.http.RequestLine;
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.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseListener;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;

public class Test {

  @SuppressWarnings("deprecation")
  public static void main(String args[]) {
    
    /** 用户认证对象 */
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    /** 设置账号密码 */
    credentialsProvider.setCredentials(AuthScope.ANY,
        new UsernamePasswordCredentials("elastic", "123456"));
    /** 创建rest client对象 */
    RestClientBuilder builder = RestClient.builder(new HttpHost("xx.xxx.xx.xx", 9200))
        .setHttpClientConfigCallback(new HttpClientConfigCallback() {
          @Override
          public HttpAsyncClientBuilder customizeHttpClient(
              HttpAsyncClientBuilder httpClientBuilder) {
            return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
          }
        });

    /**
     * 创建客户端实例
     */
    RestClient client = builder.build();

    /**
     * 发送同步查询, zeus的索引模板信息
     */
    Response response;
    try {
      response = client.performRequest("GET", "_template/zeus*",
          Collections.singletonMap("pretty", "true"));
      System.out.println(EntityUtils.toString(response.getEntity()));
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    // 索引的处理 //索引/类型/唯一ID
    /**
     * 创建请求信息 参数(请求方式, 请求地址【索引名称/文档类型/文档唯一编号】) 如:/project/test/1 项目索引/test类型/编号1
     * 
     * 增加索引文档方式一   "PUT","/project/test/1"  需要唯一ID
     * 增加索引文档方式二   "POST","/project/test/"  不需要指定唯一ID,会随机生成唯一字符串
     * 查询索引文档方式     "GET","/project/test/1"  根据搜索语法可以进行查询
     */
    Request request = new Request("GET", "/project/test/1");

    /**
     * 增加请求参数, 拼接在url后。请求后的连接地址为: /project/test/1?pretty=true
     */
    request.addParameter("pretty", "true");

    /**
     * 设置 context内容 两种方式 
     * 1. request.setEntity(new NStringEntity("{\"json\":\"text\"}",ContentType.APPLICATION_JSON));
     * 2. request.setJsonEntity("{\"json\":\"text\"}");
     */
    request.setJsonEntity("{\"json\":\"text\"}");

    /***
     * 增加header参数
     */
    RequestOptions.Builder builder_optioins = RequestOptions.DEFAULT.toBuilder();
    builder_optioins.addHeader("Auth", "token");
    request.setOptions(builder_optioins);
    
    /***
     * 创建异步的查询方式
     */
    client.performRequestAsync(request, new ResponseListener() {
      @Override
      public void onSuccess(Response arg0) {
        // TODO Auto-generated method stub
        try {
          //输出响应信息
          RequestLine requestline = arg0.getRequestLine();
          HttpHost host = arg0.getHost();
          int statusCode = arg0.getStatusLine().getStatusCode();
          Header[] headers = arg0.getHeaders();
          String responseBody = EntityUtils.toString(arg0.getEntity());
          System.out.println("requestline=" + requestline);
          System.out.println("host=" + host.toHostString());
          System.out.println("statusCode=" + statusCode);
          System.out.println("headers=" + headers.toString());
          System.out.println("responseBody=" + responseBody);
        } catch (ParseException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

      @Override
      public void onFailure(Exception arg0) {
        // TODO Auto-generated method stub
        System.out.println(arg0.toString());
      }
    });

    /***
     * 异步处理多个文档列表 【官方推荐这种做法】 
     */
    ArrayList<HttpEntity> documents = new ArrayList<HttpEntity>();
    final CountDownLatch latch = new CountDownLatch(documents.size());
    for (int i = 0; i < documents.size(); i++) {
      Request requestM = new Request("PUT", "/posts/doc/" + i);
      // let's assume that the documents are stored in an HttpEntity array
      requestM.setEntity(documents.get(i));
      client.performRequestAsync(requestM, new ResponseListener() {
        @Override
        public void onSuccess(Response response) {
          latch.countDown();
        }

        @Override
        public void onFailure(Exception exception) {
          latch.countDown();
        }
      });
    }
    try {
      latch.await();
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

下面是输出的信息\第一个sysout

{
    "test-*":{
        "order":0,
        "index_patterns":[
            "test*"
        ],
        "settings":{
            "index":{
                "number_of_shards":"3",
                "number_of_replicas":"0"
            }
        },
        "mappings":{
        },
        "aliases":{
        }
    }
}

第二个sysout

 requestline=GET /project/test/1?pretty=true HTTP/1.1
 host=xxx.xx.xxx.xxx:9200
 statusCode=200
 headers=[Lorg.apache.http.Header;@797419fc
 responseBody={
 "_index" : "project",
 "_type" : "test",
 "_id" : "1",
 "_version" : 1,
 "found" : true,
 "_source" : {
 "json" : "text"
 }
 }

出现问题还是多看看官网比较好。附原址:

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low-usage-requests.html

环境搭建之ELK(ElasticSearch+LogStash+Kibana)Mac/Linux系统上搭建6.4.2


   原创文章,转载请标明本文链接: Java连接ElasticSearch(low-level-rest-client)的配置和数据的增加/查询

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

更多阅读