Commit c062e29f authored by Bashar's avatar Bashar

Configured ElasticSearch

parent cef61db6
......@@ -2,6 +2,8 @@ package com.example.Processing.configuration;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.cluster.PutComponentTemplateRequest;
import co.elastic.clients.elasticsearch.core.BulkRequest;
import co.elastic.clients.elasticsearch.core.BulkResponse;
import co.elastic.clients.elasticsearch.ilm.IlmPolicy;
import co.elastic.clients.elasticsearch.ilm.PutLifecycleRequest;
import co.elastic.clients.elasticsearch.indices.CreateDataStreamRequest;
......@@ -10,27 +12,41 @@ import co.elastic.clients.elasticsearch.indices.PutIndexTemplateRequest;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.example.Processing.entities.ProcessedTrap;
import com.example.Processing.entities.SeverityLevel;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.stereotype.Component;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
@Configuration
@Component
@Getter
public class elasticConfig {
@Value("${workingPath}")
private String workingPath;
private RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
new HttpHost("192.168.25.254", 9200)).build();
// Create the transport with a Jackson mapper
private ElasticsearchTransport transport = new RestClientTransport(
private ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
// And create the API client
private final ElasticsearchClient elasticClient = new ElasticsearchClient(transport);
private ElasticsearchClient elasticClient = new ElasticsearchClient(transport);
public void addPolicy(String path,String name){
FileReader r = null;
try {
......@@ -85,20 +101,57 @@ public class elasticConfig {
}
}
public void addDataStream(String name){
CreateDataStreamRequest createDataStreamRequest = new CreateDataStreamRequest.Builder()
.name(name).build();
try {
elasticClient.indices().createDataStream(createDataStreamRequest);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Exception e) {
return;
// throw new RuntimeException(e);
}
}
@PostConstruct
public void ElasticDataStreamConfig() throws JsonProcessingException, FileNotFoundException {
addPolicy("src/main/resources/utils/policy.json","Bashar");
addComponent("src/main/resources/utils/settings.json","bashar_setting");
addComponent("src/main/resources/utils/mappings.json","bashar_mapping");
addIndexTemplate("src/main/resources/utils/index_template.json","bashar_template");
addDataStream("bashar-data-stream-2");
String currentDirectory = System.getProperty("user.dir");
System.out.println("Current working directory is: " + currentDirectory);
addPolicy(workingPath+"/policy.json","Traps_Policy");
addComponent(workingPath+"/settings.json","traps_setting");
addComponent(workingPath+"/mappings.json","traps_mapping");
addIndexTemplate(workingPath+"/index_template.json","traps_template");
addDataStream("traps-data-stream");
//Bulk Schedule Send
}
public void addDoc(List<ProcessedTrap> jsons){
BulkRequest.Builder br = new BulkRequest.Builder();
for (ProcessedTrap s:jsons){
br.operations(op -> op
.create(cr -> cr.index("traps-data-stream").document(s))
);
}
BulkResponse result = null;
System.out.println("Hey I am here");
try {
System.out.println("Hey I am here");
result = elasticClient.bulk(br.build());
System.out.println(result);
} catch (IOException e) {
System.out.println(e);
throw new RuntimeException(e);
}
/*
Reader input = new StringReader(json);
IndexRequest<JsonData> request = IndexRequest.of(i -> i
.index("bashar-data-stream-2")
.withJson(input)
);
IndexResponse response = null;
try {
response = elasticClient.index(request);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("Indexed with version " + response.version());*/
}
}
......@@ -5,8 +5,34 @@
"type": "date",
"format": "date_optional_time||epoch_millis"
},
"message": {
"type": "wildcard"
"enterprise": {
"type": "text"
},
"agentAddress": {
"type": "text"
},
"genericTrap": {
"type": "long"
},
"specificTrap": {
"type": "long"
},
"severity": {
"type": "text"
},
"variableBindings": {
"type": "nested",
"properties": {
"oid": {
"type": "keyword"
},
"value": {
"type": "text"
}
}
},
"location": {
"type": "geo_point"
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment