elasticsearch5.4的java api

0、先说一个题外话,安装jar包到maven仓库
这里写图片描述
1、 创建maven项目,要把create a simple project勾选上
这里写图片描述
2、 配置pom.xml。
这步很重要,由于要使用spring boot,所以这里要添加

  <properties>
    <elasticsearch.version>5.4.3</elasticsearch.version>
</properties>

Spring boot的配置

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
</parent>
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

由于使用了x-pack管理elasticsearch,所以参见官网:

https://www.elastic.co/guide/en/x-pack/5.4/api-java.html

  <repositories>
      <!-- add the elasticsearch repo -->
      <repository>
         <id>elasticsearch-releases</id>
         <url>https://artifacts.elastic.co/maven</url>
         <releases>
            <enabled>true</enabled>
         </releases>
         <snapshots>
            <enabled>false</enabled>
         </snapshots>
      </repository>
 </repositories>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>x-pack-transport</artifactId>
        <version>5.4.3</version>
   </dependency>

Elasticsearch的配置

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>5.4.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.8.2</version><!--$NO-MVN-MAN-VER$-->
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8.2</version><!--$NO-MVN-MAN-VER$-->
  </dependency>

3、 创建源码


package org.cma.hlj.bigdata.controller;

import java.net.InetAddress;

import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.cluster.health.ClusterIndexHealth;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;




public class Test {

     public static void main(String[] args){

         Settings set=Settings.builder().put("cluster.name","cmabd")
                 .put("xpack.security.transport.ssl.enabled",false)
                 .put("xpack.security.user", "xxx:xxx")
                 .put("client.transport.sniff",true).build();
         TransportClient  client=null;

         try{
             client=new PreBuiltXPackTransportClient(set)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.96.xx.xx"), 9300))
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.96.xx.xx"), 9300));

             ClusterHealthResponse healths = client.admin().cluster().prepareHealth().get();
             String clusterName = healths.getClusterName();
             int numberOfDataNodes = healths.getNumberOfDataNodes();
             int numberOfNodes = healths.getNumberOfNodes();

             System.out.println(clusterName+" "+numberOfDataNodes+" "+numberOfNodes);

             for (ClusterIndexHealth health : healths.getIndices().values()) { 
                    String index = health.getIndex();                       
                    int numberOfShards = health.getNumberOfShards();        
                    int numberOfReplicas = health.getNumberOfReplicas();    
                    ClusterHealthStatus status = health.getStatus();
                    System.out.println(index+" "+numberOfShards+" "+numberOfReplicas+" "+status);
                }

             GetResponse response = client.prepareGet("cts_recv", "comlog", "AV01rfXRF3gNro0o5jWE").get();
             System.out.println(response);
         }
         catch(Exception e)
         {

         }
         finally{
             client.close();
         }
     }

}

4、 注意事项
Jdk的版本一定要8以上,要不然会报错。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Elasticsearch 5.4 提供了一种称为 SQL REST API 的方式,允许你使用 SQL 语句来查询 Elasticsearch。你可以使用 Java 客户端来调用 SQL REST API,实现 SQL 查询。 以下是一个使用 Java 客户端调用 Elasticsearch SQL REST API 的示例: ```java import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.sniff.Sniffer; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.transport.client.PreBuiltTransportClient; import java.net.InetAddress; import java.util.concurrent.TimeUnit; public class ElasticsearchSqlExample { public static void main(String[] args) throws Exception { // 创建 RestClient RestClient restClient = RestClient.builder( new HttpHost("localhost", 9200, "http")) .build(); // 创建 RestHighLevelClient RestHighLevelClient client = new RestHighLevelClient(restClient); // 创建 SQL 查询 XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field("query", "SELECT * FROM my_index WHERE age > 30"); } builder.endObject(); // 创建 SearchSourceBuilder SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.wrapperQuery(builder.string())); // 发起查询 SearchHit[] searchHits = client.search(searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS))) .getHits().getHits(); // 输出查询结果 for (SearchHit searchHit : searchHits) { System.out.println(searchHit.getSourceAsString()); } // 关闭客户端连接 client.close(); restClient.close(); } } ``` 在这个示例中,我们首先创建了一个 RestClient,然后使用它创建了一个 RestHighLevelClient。接着,我们使用 XContentBuilder 创建了一个 SQL 查询,并将其包装在一个 SearchSourceBuilder 中。最后,我们使用 RestHighLevelClient 发起查询,并输出查询结果。 需要注意的是,为了能够使用 SQL REST API,需要在 Elasticsearch 的配置文件中启用 xpack.sql.enabled 参数。同时,SQL 查询的语法和限制与传统的 SQL 有所不同,具体可以参考 Elasticsearch 的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值