zoukankan      html  css  js  c++  java
  • (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中的不一致,如下:

  • 相关阅读:
    10.21
    ROS的安装和卸载
    Ubuntu下的终端多标签切换快捷键
    Windows Live Wirter
    ubuntu解压和压缩文件
    Ubuntu更改源和搜狗输入法安装卸载
    PyTorch--常用的工具
    PyTorch--神经网络工具箱nn
    Pytorch--autograd
    PyTorch--Tensor
  • 原文地址:https://www.cnblogs.com/javasl/p/12057382.html
Copyright © 2011-2022 走看看