java api 调用es集群(1.7版本)

public static void main(String[] args) {
Settings settings = ImmutableSettings.settingsBuilder()
// client.transport.sniff=true
// 客户端嗅探整个集群的状态,把集群中其它机器的ip地址自动添加到客户端中,并且自动发现新加入集群的机器
.put("client.transport.sniff", true).put("client", true)// 仅作为客户端连接
.put("data", false).put("cluster.name", "elasticsearch")// 集群名称
.build();
TransportClient client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("192.168.10.140", 9300))// TCP
// 连接地址
.addTransportAddress(new InetSocketTransportAddress("192.168.10.124", 9300));

List<String> jsonData = BlogController.initData();
String json = "{" + "\"user\":\"金旭东\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"测试elsaticsearch\"" + "}";
for (int i = 0; i < 1; i++) {
IndexResponse response = client.prepareIndex("blog", "article").setSource(json).get();
if (response.isCreated()) {
System.out.println("创建成功!");
}
}
client.close();
}

posted @ 2017-03-14 10:28  洞玄巅峰  阅读(2056)  评论(0编辑  收藏  举报