zoukankan      html  css  js  c++  java
  • java 连接带认证的 elasticsearch

    1.使用elasticsearch 官方提供客户端:org.elasticsearch.client.RestClientBuilder

    pom.xml 添加依赖:

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>7.6.2</version>
    </dependency>

    java 代码:

     1         RestClientBuilder builder = RestClient.builder(new HttpHost(
     2                 "localhost",
     3                 9200,
     4                 "http")
     5         );
    // 此处添加认证 header 值为 Base64编码后的 user:password
    6 Header[] myheaders = { 7 new BasicHeader("Authorization","Basic YWJkaV9hZG1pbjpBRkVEb3ZNclRSeGdjcUpjRW5GREJPcUxIYUdLVHRkOXM5d0Uxd3NpVnI5TVZDenc=") 8 }; 9 builder.setDefaultHeaders(myheaders);
    10 RestClient client = builder.build(); 11 String index = "secvisual.system_state"; 12 String query = "{"query":{"match_all":{}}}"; 13 try { 14 String searchIndex = String.format("/%s/_search?ignore_unavailable=%s", index, true); 15 Request request = new Request("GET", searchIndex); 16 request.setJsonEntity(query); 17 18 Response response = client.performRequest(request); 19 if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { 20 System.out.println("query failed!"); 21 } 22 23 String body = EntityUtils.toString(response.getEntity()); 24 System.out.println("query success, reuslt:" + body); 25 } catch (Exception e) { 26 System.out.println(e); 27 } 28 finally { 29 client.close(); 30 }
    人生还有意义。那一定是还在找存在的理由
  • 相关阅读:
    JS之四舍五入有小数点
    c# table 怎么在前台循环展示 ViewBag
    c# ajax从后台获取数据list数组 $.each再显示数据
    c# 快捷键
    c#_导出table功能
    c# 缓存详解
    c# url链接转成二维码图片,再转成byte[]二进制流,输出到前段ajax
    JS ajax 返回的json对象 新增属性值(干货)
    tomcat配置HTTPS
    zookeeper集群安装
  • 原文地址:https://www.cnblogs.com/shiqi17/p/14781413.html
Copyright © 2011-2022 走看看