Elasticsearch 与Springboot 的简单连接

1、主要Elasticsearch 包:

2、application.properties 配置

 

3、 Elasticsearch  java 代码

package com.allen.elasticsearch;

import java.net.InetAddress;

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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@Component
public class ESconfig {
	
	private Logger logger = LoggerFactory.getLogger(ESconfig.class);
	
	@Value("${elasticsearch.ip}")
	private String hostName;
	
	/**
     * 端口
     */
    @Value("${elasticsearch.port}")
    private String port;
    /**
     * 集群名称
     */
    @Value("${elasticsearch.cluster.name}")
    private String clusterName;

    /**
     * 连接池
     */
    @Value("${elasticsearch.pool}")
    private String poolSize;

    public TransportClient init() {
    	logger.info("初始化开始。。。。。");
        TransportClient transportClient = null;
        try {
            // 配置信息
            Settings esSetting = Settings.builder()
                    .put("client.transport.sniff", true)//增加嗅探机制,找到ES集群
                    .put("thread_pool.search.size", Integer.parseInt(poolSize))//增加线程池个数,暂时设为20
                    .put("cluster.name",clusterName)
                    .build();
            //配置信息Settings自定义,下面设置为EMPTY
            transportClient = new PreBuiltTransportClient(esSetting);
            TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
            transportClient.addTransportAddresses(transportAddress);


        } catch (Exception e) {
        	e.printStackTrace();
        	logger.error("elasticsearch TransportClient create error!!!", e);
        }

        return transportClient;
    }

}
package com.allen.elasticsearch;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/es")
public class ESController {
		
	@Autowired
	private ESconfig esconfig;
	
	
	
	@RequestMapping("/get")
	public void get() throws Exception{
		String index = "chat";
		String type ="msg";
		String id="3";
		
		TransportClient client = esconfig.init();
		GetResponse  result = client.prepareGet(index,type,id).get();
		System.out.println(result.getSourceAsString());
		System.out.println(result.getType());
		System.out.println(result.getVersion());
		System.err.println(result.getIndex());
		System.err.println(result.getId());
		
		client.close();
	}
	
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值