本文共 909 字,大约阅读时间需要 3 分钟。
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(); }转载地址:http://vjjka.baihongyu.com/