fix: 调整 pcap 文件解析 index_name= workspace-{workspace.name}-{pcap.md5}

This commit is contained in:
shizhendong
2024-08-02 18:27:22 +08:00
parent af0d0e55ca
commit a837a160f9

View File

@@ -17,6 +17,7 @@ import org.opensearch.client.opensearch.core.BulkRequest;
import org.opensearch.client.opensearch.core.BulkResponse;
import org.opensearch.client.opensearch.core.bulk.BulkResponseItem;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
import org.opensearch.client.opensearch.indices.DeleteIndexRequest;
import org.opensearch.client.opensearch.indices.ExistsRequest;
import org.opensearch.client.opensearch.indices.IndexSettings;
@@ -152,8 +153,12 @@ public class PcapParserThread implements Runnable {
* @param jsonArray
*/
private void uploadToOpenSearch(JSONArray jsonArray) {
String pcapPath = pcapEntity.getPath();
String md5Hex = T.DigestUtil.md5Hex(T.FileUtil.file(pcapPath));
String workspaceName = pcapEntity.getWorkspace().getName();
String indexName = String.format("workspace-%s", workspaceName);
String indexName = String.format("workspace-%s-%s", workspaceName, md5Hex);
try {
// check if index exists
boolean indexExists = openSearchClient.indices()
@@ -162,23 +167,25 @@ public class PcapParserThread implements Runnable {
if (log.isDebugEnabled()) {
log.debug("[uploadToOpenSearch] [index: {}] [exists: {}]", indexName, indexExists);
}
// if index not exists, create index with default settings
if (!indexExists) {
// if index exists, delete
if (indexExists) {
openSearchClient.indices().delete(new DeleteIndexRequest.Builder().index(indexName).build());
log.debug("[uploadToOpenSearch] [index: {}] [deleted]", indexName);
}
// create index with default settings
openSearchClient.indices().create(
new CreateIndexRequest.Builder()
.index(indexName)
.settings(new IndexSettings.Builder().build())
.build()
);
log.debug("[uploadToOpenSearch] [index: {}] [created]", indexName);
}
// upload data in bulk
BulkRequest.Builder br = new BulkRequest.Builder();
String pcapId = pcapEntity.getId();
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
String id = pcapId + "-" + String.valueOf(i);
String id = String.valueOf(i);
br.operations(op -> op.index(
idx -> idx.index(indexName)
.id(id)