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以上,要不然会报错。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值