下午尝试 用ElasticSearch 的java客户端去做数据检索工作,测试了一下批量更新,代码如下:
public static void bulkUpdateGoods(List<Goods> goods) throws IOException, InterruptedException, ExecutionException { Client client = null; try { client = TransportClient.builder().build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hadoop-slave4"), 9300)); } catch (Exception e) { e.printStackTrace(); } BulkRequestBuilder req = client.prepareBulk(); for (Goods good : goods) { req.add(client.prepareIndex("smms", "goods", good.getId() + "") .setSource(jsonBuilder().startObject().field("unit", good.getUnit()).field("price", good.getPrice()) .field("name", good.getName()).field("barcode", good.getBarCode()).endObject())); } BulkResponse res = req.execute().actionGet(); if (res.hasFailures()) { System.out.println("Error"); } else { System.out.println("Done"); } client.close(); } }
但是测试的时候老是给我报这个错误:NoNodeAvailableException[None of the configured nodes are available。具体没截图。
然后我改正两点后解决问题,一处是用9300端口 而不是9200.9200端口只是HTTP端口。
第二处是我把配置文件里cluter.name给注释掉了,就能用了。具体原理不清楚。
时间: 2024-10-08 13:44:58