我将使用BulkApi加载大量数据,但在编译代码时会向我显示错误。
private static final Logger log = LoggerFactory.getLogger(BulkApiService.class);
@Autowired
public ElasticSearchConfig elasticSearchConfig;
private static String FOLDER_PATH = "src/main/resources/allFiles";
public void loadAllDataUsingBulkApi() {
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 contentJson = new JSONObject(content);
HashMap contentMap = new Gson().fromJson(contentJson.toString(), HashMap.class);
request.get().add(client.prepareIndex("indexName", "default").setSource(contentMap));
} catch (IOException ignore) {
log.error(ignore.toString());
}
}
});
BulkResponse bulkResponse = request.get().execute().actionGet();
} catch (Exception e) {
log.error(e.toString());
}
}
}
预期输出:它将在指定路径上将所有数据加载到ES。 实际输出:
关于“字符串内容= Files.readString(filePath);”的错误找不到符号。 符号:方法readString(Path) 位置:类文件