1. 程式人生 > >Elasticsearch 與Springboot 的簡單連線

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();
	}
	
}