赞
踩
package com.rodge.elasticSearch.firstDemo;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
public class ESFirstDemo {
@SuppressWarnings("unchecked")
public TransportClient client() throws UnknownHostException {
// es地址, 地址为192.168.253.129(此处地址不能加"http://"), 此处的端口为es的tcp端口为9300,
// 而不是之前的http9200端口, 如果es有多可节点, 可以创建多个node, 然后再client中add进去
// 注意,这里的端口号不是9200,而是9300。9200端口是用来让HTTP REST
// API来访问ElasticSearch,而9300端口是传输层监听的默认端口。
InetSocketTransportAddress node = new InetSocketTransportAddress(
InetAddress.getByName("192.168.253.129"), 9300);
// es的配置类
Settings settings = Settings.builder().put("cluster.name", "wali").build();
// TransportClient是es启动的核心类, 后续的关于es的开发都是围绕着TransportClient进行的
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(node);
return client;
}
@Test
public void get() throws UnknownHostException {
GetResponse result = client().prepareGet("book", "novel", "1").get();
System.out.println(result.getSource().toString());
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。