下午嘗試 用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給注釋掉了,就能用了。具體原理不清楚。