SpringBoot-2.2.6操作Elasticsearch-6.2.4

SpringBoot-2.2.6操作Elasticsearch-6.2.4

## Kibana版本 kibana-6.2.4-darwin-x86_64

Kibana安装Elasticsearch安装请自行解决.

  		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
以下方法官方已经不推荐了.
#spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
#spring.data.elasticsearch.cluster-name=elasticsearch

在这里插入图片描述

所以我们通过java代码配置, 需要注意端口是Es的Tcp端口9300. 而不是Http协议的9200


package com.example.config;

import org.elasticsearch.client.Client;
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.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
import org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.EntityMapper;

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


/**
 * @author wajn
 * @date 2020/04/25  19:50
 */
@Configuration
public class EsConf extends ElasticsearchConfigurationSupport {

    @Bean
    public Client elasticsearchClient() throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        return client;
    }

    @Bean(name = "elasticsearchTemplate")
    public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
        return new ElasticsearchTemplate(elasticsearchClient(), entityMapper());
    }

    // use the ElasticsearchEntityMapper
    @Bean
    @Override
    public EntityMapper entityMapper() {
        ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(elasticsearchMappingContext(),
                new DefaultConversionService());
        entityMapper.setConversions(elasticsearchCustomConversions());
        return entityMapper;
    }

}


package com.example.esentity;

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

import java.util.Date;


/**
 * @author wajn
 * @date 2020/04/25  19:50
 */
@ToString
@Data
//indexName索引库名,个人建议以项目名称命名
//type类型,个人建议以实体类名称命名
@Document(indexName = "log",type = "test")
public class LogEntity {
    //代表唯一标识
    @Id
    private String id;
    private String exceptionInformation;
    private String logType;
    private String operating;
    private String userName;
    private Date responseTime;
    private String ip;
    private Date time;
}

package com.example.repository;

import com.example.esentity.LogEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;


/**
 * @author wajn
 * @date 2020/04/25  19:50
 */
@Repository
public interface LogRepository extends ElasticsearchRepository<LogEntity,String> {

}


package com.example.repository;


import com.example.ApplicationTest;
import com.example.esentity.LogEntity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Optional;


/**
 * @author wajn
 * @date 2020/04/25  19:50
 */
public class LogRepositoryTest extends ApplicationTest {

    @Autowired
    LogRepository logRepository;

    @Test
    public void find() {
        Optional<LogEntity> y8fAsHEBZjLCY8DIlCsl = logRepository.findById("y8fAsHEBZjLCY8DIlCsl");
        System.out.println(y8fAsHEBZjLCY8DIlCsl.get());
    }

    @Test
    public void save() {
        LogEntity logEntity = new LogEntity();
        logEntity.setExceptionInformation("loading...");
        logEntity.setLogType("1");
        logEntity.setOperating("delete");
        logEntity.setUserName("zs");
        logEntity.setResponseTime(new Date());
        logEntity.setIp("192.168.1.1");
        logEntity.setTime(new Date());
        LogEntity index = logRepository.save(logEntity);
        System.out.println(index);
    }
}
package com.example;

import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author wajn
 * @date 2020/04/25  19:50
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsDemoApplication.class)
public class ApplicationTest {

    @Before
    public void init() {
        System.out.println("init ok");
    }
}

控制台:
LogEntity(id=zMf-sHEBZjLCY8DIwytV, exceptionInformation=loading…, logType=1, operating=delete, userName=zs, responseTime=Sat Apr 25 19:00:18 CST 2020, ip=192.168.1.1, time=Sat Apr 25 19:00:18 CST 2020)

es数据结果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值