我正在使用 Spring Data Elasticsearch 4.2.5,我们有一项工作对特定的数据库表执行 ETL(提取、转换和加载数据)。我在作业运行时使用 Elasticsearch 为这些数据建立索引。数据将达到数百万条甚至更多。目前,我正在对每次迭代进行索引。我读到,在每次迭代中使用 elasticsearch 索引可能需要一些时间。我想使用像bulk-index这样的东西,但为此我需要将indexQuery对象添加到List中。添加数百万条记录到列表并进行批量索引可能会带来内存问题。
我需要应用类似的删除过程。当根据一些常见的ID删除记录时,我需要删除相关的弹性文档,这也将是数百万甚至更多。
无论如何,是否可以针对此要求非常快速地进行索引/删除?非常感谢任何帮助,如果我的理解不正确,请纠正我。
INDEXING
for (Map.Entry<Integer, ObjectDetails> key : objectDetailsHashMap.entrySet()) {
indexDocument(elasticsearchOperations, key, oPath);
// other code to insert data in db table...
}
private void indexDocument(ElasticsearchOperations elasticsearchOperations,
Map.Entry<Integer, ObjectDetails> key, String oPath) {
String docId = "" + key.getValue().getCatalogId() + key.getValue().getObjectId();
byte[] nameBytes = key.getValue().getName();
byte[] physicalNameBytes = key.getValue().getPhysicalName();
byte[] definitionBytes = key.getValue().getDefinition();
byte[] commentBytes = key.getValue().getComment();
IndexQuery indexQuery = new IndexQueryBuilder()
.withId(docId)
.withObject(new MetadataSearch(
key.getValue().getObjectId(),
key.getValue().getCatalogId(),
key.getValue().getParentId(),
key.getValue().getTypeCode(),
key.getValue().getStartVersion(),
key.getValue().getEndVersion(),
nameBytes != null ? new String(nameBytes, StandardCharsets.UTF_8) : "-",
physicalNameBytes != null ? new String(physicalNameBytes, StandardCharsets.UTF_8) : "-",
definitionBytes != null ? new String(definitionBytes, StandardCharsets.UTF_8) : "-",
commentBytes != null ? new String(commentBytes, StandardCharsets.UTF_8) : "-",
oPath
))
.build();
elasticsearchOperations.index(indexQuery, IndexCoordinates.of("portal_idx"));
}
DELETING
private void deleteElasticDocuments(String catalogId) {
String queryText = martServerContext.getQueryCacheInstance().getQuery(QUERY_PORTAL_GET_OBJECTS_IN_PORTAL_BY_MODEL);
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();
mapSqlParameterSource.addValue("cId", Integer.parseInt(catalogId));
namedParameterJdbcTemplate.query(queryText, mapSqlParameterSource, (resultSet -> {
int objectId = resultSet.getInt(O_ID);
String docId = catalogId + objectId;
elasticsearchOperations.delete(docId, IndexCoordinates.of("portal_idx"));
}));
}