java - NoNodeAvailableException[没有配置的节点可用:[{#transport#-1}{XY6cYmf7Sn-DRZgzeq3PBA}{localhost}{127.0.0.1:9300}]]
问题描述
我正在使用 Elasticsearch 的 BULK API 和 5.6.16 版本的 Elasticsearch 和 KIbana。以下代码工作正常并将代码上传到 ES 的索引。
这些是我目前拥有的依赖项。
实施 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
编译组:'org.elasticsearch.client',名称:'transport'
编译组:'org.elasticsearch.plugin',名称:'transport-netty4-client'
@Service
public class BulkApiService {
private final Logger log = LoggerFactory.getLogger(BulkApiService.class);
private String FOLDER_PATH = "src/main/resources/allRecipesJson";
private String index = "test";
@Autowired
ElasticSearchConfig elasticSearchConfig;
public String loadBulkData() throws UnknownHostException {
Client client = elasticSearchConfig.client();
AtomicReference<BulkRequestBuilder> request = new AtomicReference<>(client.prepareBulk());
AtomicInteger counter = new AtomicInteger();
try (Stream<Path> filePathStream = Files.walk(Paths.get(FOLDER_PATH))) {
filePathStream.forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
counter.getAndIncrement();
try {
String content = Files.readString(filePath);
JSONObject jsonObject1 = new JSONObject(content);
HashMap yourHashMap1 = new Gson().fromJson(jsonObject1.toString(), HashMap.class);
request.get().add(client.prepareIndex(index, "default").setSource(yourHashMap1));
} catch (IOException ignore) {
log.error(ignore.toString());
}
}
});
BulkResponse bulkResponse = request.get().execute().actionGet();
} catch (Exception e) {
log.error(e.toString());
}
return "Bulk data loaded to index " + index + "";
}
以下是配置
@Configuration
public class ElasticSearchConfig {
private static final Logger log = LoggerFactory.getLogger(ElasticSearchConfig.class);
@Bean
public Client client() {
TransportClient client = null;
try {
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
} catch (UnknownHostException e) {
log.error(log.toString());
}
return client;
}
}
现在我想迁移到 Elasticserch 和 Kibana 版本 7.3.1。我无法将数据加载到弹性搜索索引。IDE 日志中显示以下错误
service.BulkApiService - NoNodeAvailableException[配置的节点都不可用:[{#transport#-1}{XY6cYmf7Sn-DRZgzeq3PBA}{localhost}{127.0.0.1:9300}]]
elasticsearch imagec上的错误
请帮我纠正它。
解决方案
以下是正确的代码。
@Service
public class BulkApiService { private final Logger log = LoggerFactory.getLogger(BulkApiService.class);
private String FOLDER_PATH = "src/main/resources/allRecipesJson";
private String index = "test1";
private static final String TYPE = "test_type";
@Autowired
private RestHighLevelClient restHighLevelClient;
public String loadBulkData() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
AtomicInteger counter = new AtomicInteger();
try (Stream<Path> filePathStream = Files.walk(Paths.get(FOLDER_PATH))) {
filePathStream.forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
counter.getAndIncrement();
System.out.println(filePath.toFile().getAbsoluteFile().toString());
try {
String content = Files.readString(filePath);
JSONObject jsonObject1 = new JSONObject(content);
HashMap yourHashMap1 = new Gson().fromJson(jsonObject1.toString(), HashMap.class);
IndexRequest indexRequest = new IndexRequest(index, TYPE).source(yourHashMap1);
bulkRequest.add(indexRequest);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
try {
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return "Bulk data loaded to index " + index + "";
}
}
添加以下依赖项并删除已放置的依赖项。
编译'org.elasticsearch.client:elasticsearch-rest-high-level-client:6.4.2'
推荐阅读
- postfix-mta - Postfix/Dovecot:无法接收外部电子邮件
- windows - 访问隐藏的 ini 文件部分(文件名上的冒号)
- r - R:ggplot 使用 facet_wrap 和 coord_flip 重新排序 x 轴
- c# - 点击新项目时折叠 Xamarin ListView 中的所有项目
- php - 将卡信息保存到条带并稍后向客户收费
- android - 如何将 HC-05 蓝牙模块从 COMMAND 模式切换到 DATA 模式
- entity-framework - 具有 List 类型属性的 EF-Entity 配置,其中包含抽象类的项目而不考虑鉴别器 - EF
- macos - 终端命令采取间歇性屏幕截图不起作用
- database - 为什么 Postgres 所谓的查询执行时间与客户端得到它的时间相差十倍?
- laravel - Laravel 7 使用 Laravel 存储从 AWS S3 下载文件并在之后删除它