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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM