SpringBoot连接Elasticsearch并插入数据

一、添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

二、创建配置文件中自定义内容

elasticsearch:
  urls: localhost:9200
  username: elastic
  password: changeme

三、配置连接池

package com.example.zhang.bootes.config;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.util.StringUtils;

@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {

    @Value("${elasticsearch.username}")
    private String USERNAME;
    @Value("${elasticsearch.password}")
    private String PASSWORD;
    @Value("${elasticsearch.urls}")
    private String URLS;// 都好分割

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        if (StringUtils.isEmpty(URLS)) {
            throw new RuntimeException("配置有问题,elasticsearch.urls为空");
        }
        String[] urls = URLS.split(",");
        HttpHost[] httpHostArr = new HttpHost[urls.length];
        for (int i=0; i<urls.length; i++) {
            String urlStr = urls[i];
            if(StringUtils.isEmpty(urlStr)) {
                continue;
            }
            httpHostArr[i] = HttpHost.create(urlStr);
        }

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USERNAME, PASSWORD));  //es账号密码(默认用户名为elastic)
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(httpHostArr)
                        .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                            @Override
                            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                                httpClientBuilder.disableAuthCaching();
                                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                            }
                        }));
        return client;
    }
}

四、创建实体类

package com.example.zhang.bootes.config;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

import java.util.Date;

/**
 * @author 风行烈
 * @version 1.0
 * @date 2020/11/2 14:23
 */
@Data
// 索引名称
@Document(indexName = "systems_log")
public class Systems {

    @Id
    private Integer id;

    private String name;

    private Date createTime;

}

四、创建数据连接层

package com.example.zhang.bootes.mapper;

import com.example.zhang.bootes.config.Systems;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * @author 风行烈
 * @version 1.0
 * @date 2020/11/6 16:58
 */
public interface SystemMapper extends ElasticsearchRepository<Systems,String> {
}

五、插入数据并且测试

    @Autowired
    private SystemMapper systemMapper;

    /**
     * 在es中添加数据
     */
    /**
     * 测试添加文档
     *
     * @throws IOException
     */
    @Test
    public void createDocument() throws IOException {
        Systems systems = new Systems();
        systems.setId(3);
        systems.setName("张三再次操作了数据");
        systems.setCreateTime(new Date());
        systemMapper.save(systems);


    }

SpringBoot连接Elasticsearch并插入数据_第1张图片

你可能感兴趣的:(SpringBoot,es,elasticsearch,spring,boot)