码迷,mamicode.com
首页 > 其他好文 > 详细

2:elasticsearch客户端创建

时间:2014-11-11 12:50:46      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:elasticsearch client

根据elasticsearch的API,首先,要创建一个客户端实例Client,代码如下


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.netty.util.internal.ConcurrentHashMap;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;


/**
 * client客户端帮助类
 * 
 * @author tuoqiantu
 * @date 2013-8-9 下午3:55:38
 * 
 */
public class ClientHelper {

    private static Settings setting;

    public static List<InetSocketTransportAddress> transportAddress = new ArrayList<InetSocketTransportAddress>();

    private static Map<String, Client> clientMap = new ConcurrentHashMap<String, Client>();

    private static Map<String, Integer> ips=new HashMap<>();
    
    private static String clusterName = "node1";
    static {
        ips.put("192.168.1.100", 9300);
        ips.put("192.168.1.101", 9300);
        init();
    }

    /**
     * 初始化默认的client
     */
    public static void init() {
        setting = ImmutableSettings
                .settingsBuilder()
                .put("client.transport.sniff",true)
                .put("client",true)
                .put("data",false)
                .put("cluster.name","elasticsearch").build();
        transportAddress.addAll(getAllAddress(ips));
        Client client = new TransportClient(setting)
                .addTransportAddresses(transportAddress
                        .toArray(new InetSocketTransportAddress[transportAddress
                                .size()]));
        
        clientMap.put(clusterName, client);
    }

    /**
     * 获得所有的地址端口
     * 
     * @return
     */
    public static List<InetSocketTransportAddress> getAllAddress(Map<String, Integer> ips) {
        List<InetSocketTransportAddress> addressList = new ArrayList<InetSocketTransportAddress>();
        for (String ip : ips.keySet()) {
            addressList.add(new InetSocketTransportAddress(ip, ips.get(ip)));
        }
        return addressList;

    }

    public static Client getClient() {
        return getClient(clusterName);
    }

    public static Client getClient(String clusterName) {
        
        return clientMap.get(clusterName);
    }

    public static void addClient(Settings setting,
            List<InetSocketTransportAddress> transportAddress) {
        Client client = new TransportClient(setting)
                .addTransportAddresses(transportAddress
                        .toArray(new InetSocketTransportAddress[transportAddress
                                .size()]));
        clientMap.put(setting.get("cluster.name"), client);
    }
}


本文出自 “陈砚羲” 博客,请务必保留此出处http://chenyanxi.blog.51cto.com/4599355/1575245

2:elasticsearch客户端创建

标签:elasticsearch client

原文地址:http://chenyanxi.blog.51cto.com/4599355/1575245

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!