(17)ElasticSearch java项目连接ElasticSearch示例

  1、添加elasticsearch客户端依赖

<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>transport</artifactId>
  <version>6.2.4</version>
</dependency>

  完整pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.edu</groupId>
    <artifactId>elasticSearch</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>elasticSearch</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.2.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
View Code

  2、添加log4j2.xml文件,elasticsearch客户端使用log4j2日志,不添加会运行会报错

  log4j2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appenders>
        <Console name="CONSOLE" target="system_out" follow="true">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level[%thread] %c [%L] -| %msg%n" />
        </Console>
    </appenders>
    <loggers>
        <root level="info">
            <appenderref ref="CONSOLE" />
        </root>
    </loggers>
</configuration>
View Code

  3、写测试类ESTest.java

package com.edu.elasticSearch;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

public class ESTest {

    @Test
    public void test1() throws UnknownHostException {
        
        //指定es集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        
        //创建访问es服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                    .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //查询索引是lib3,类型是user,id是1的数据
        GetResponse response = client.prepareGet("lib3","user","1").execute().actionGet();
        
        //得到查询出的数据
        System.out.println(response.getSourceAsString());
        client.close();//关闭客户端
        
    }
}
View Code

  4、启动es服务和页面客户端kibana,添加测试数据

put /lib3/user/1
{
    "name":"zhaoliu",
    "address":"hei long jiang sheng tie ling shi",
    "age":50,
    "birthday":"1970-12-12",
    "interests":"xi huang hejiu,duanlian,lvyou"
}
View Code

  5、测试,结果如下:

  以下几点需要注意:

  (1)指定es集群,也适用于单机,配置文件elasticsearch.yml中cluster.name要取消注释,如下:

  (2)java中指定的端口是9300,与elasticsearch.yml中的不一致,如下:

posted @ 2019-12-17 22:49  雷雨客  阅读(1738)  评论(0编辑  收藏  举报