Search in sources :

Example 1 with BulkDocumentWriterResults

use of org.apache.metron.elasticsearch.bulk.BulkDocumentWriterResults in project metron by apache.

the class ElasticsearchWriterTest method shouldWriteManySuccessfullyWithSetDocumentId.

@Test
public void shouldWriteManySuccessfullyWithSetDocumentId() {
    when(writerConfiguration.isSetDocumentId("bro")).thenReturn(true);
    when(writerConfiguration.getFieldNameConverter("bro")).thenReturn("NOOP");
    // create a few message ids and the messages associated with the ids
    List<BulkMessage<JSONObject>> messages = createMessages(3);
    // documents should have field converted
    MessageIdBasedDocument document1 = createDocument(messages.get(0));
    MessageIdBasedDocument document2 = createDocument(messages.get(1));
    MessageIdBasedDocument document3 = createDocument(messages.get(2));
    // documents should have guid as documentID
    document1.setDocumentID(document1.getGuid());
    document2.setDocumentID(document1.getGuid());
    document3.setDocumentID(document1.getGuid());
    // create a document writer which will successfully write all
    BulkDocumentWriterResults<MessageIdBasedDocument> results = new BulkDocumentWriterResults<>();
    results.addSuccess(document1);
    results.addSuccess(document2);
    results.addSuccess(document3);
    BulkDocumentWriter<MessageIdBasedDocument> docWriter = mock(BulkDocumentWriter.class);
    when(docWriter.write()).thenReturn(results);
    // attempt to write
    ElasticsearchWriter esWriter = spy(ElasticsearchWriter.class);
    doReturn(new SimpleDateFormat()).when(esWriter).getIndexFormat(globals());
    doReturn("bro_index").when(esWriter).getIndexName(eq("bro"), any(), eq(writerConfiguration));
    esWriter.setDocumentWriter(docWriter);
    esWriter.init(stormConf, writerConfiguration);
    BulkWriterResponse response = esWriter.write("bro", writerConfiguration, messages);
    // documents should have metron guid as documentID
    verify(docWriter, times(1)).addDocument(document1, "bro_index");
    verify(docWriter, times(1)).addDocument(document1, "bro_index");
    verify(docWriter, times(1)).addDocument(document1, "bro_index");
    // response should only contain successes
    assertFalse(response.hasErrors());
    assertTrue(response.getSuccesses().contains(new MessageId("message1")));
    assertTrue(response.getSuccesses().contains(new MessageId("message2")));
    assertTrue(response.getSuccesses().contains(new MessageId("message3")));
}
Also used : BulkMessage(org.apache.metron.common.writer.BulkMessage) BulkDocumentWriterResults(org.apache.metron.elasticsearch.bulk.BulkDocumentWriterResults) SimpleDateFormat(java.text.SimpleDateFormat) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) MessageId(org.apache.metron.common.writer.MessageId) Test(org.junit.jupiter.api.Test)

Example 2 with BulkDocumentWriterResults

use of org.apache.metron.elasticsearch.bulk.BulkDocumentWriterResults in project metron by apache.

the class ElasticsearchWriterTest method shouldHandlePartialFailures.

@Test
public void shouldHandlePartialFailures() {
    // create a few message ids and the messages associated with the ids
    int count = 2;
    List<BulkMessage<JSONObject>> messages = createMessages(count);
    Exception cause = new Exception();
    // create a document writer that will fail one and succeed the other
    BulkDocumentWriterResults<MessageIdBasedDocument> results = new BulkDocumentWriterResults<>();
    results.addFailure(createDocument(messages.get(0)), cause, "error");
    results.addSuccess(createDocument(messages.get(1)));
    BulkDocumentWriter<MessageIdBasedDocument> docWriter = mock(BulkDocumentWriter.class);
    when(docWriter.write()).thenReturn(results);
    // attempt to write
    ElasticsearchWriter esWriter = new ElasticsearchWriter();
    esWriter.setDocumentWriter(docWriter);
    esWriter.init(stormConf, writerConfiguration);
    BulkWriterResponse response = esWriter.write("bro", writerConfiguration, messages);
    // response should contain some successes and some failures
    assertEquals(1, response.getSuccesses().size());
    assertEquals(1, response.getErrors().size());
    assertTrue(response.getErrors().get(cause).contains(new MessageId("message1")));
    assertTrue(response.getSuccesses().contains(new MessageId("message2")));
}
Also used : BulkMessage(org.apache.metron.common.writer.BulkMessage) BulkDocumentWriterResults(org.apache.metron.elasticsearch.bulk.BulkDocumentWriterResults) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) MessageId(org.apache.metron.common.writer.MessageId) Test(org.junit.jupiter.api.Test)

Example 3 with BulkDocumentWriterResults

use of org.apache.metron.elasticsearch.bulk.BulkDocumentWriterResults in project metron by apache.

the class ElasticsearchWriterTest method shouldHandleManyWriteFailures.

@Test
public void shouldHandleManyWriteFailures() {
    // create a few message ids and the messages associated with the ids
    int count = 3;
    List<BulkMessage<JSONObject>> messages = createMessages(count);
    Exception cause = new Exception();
    // create a document writer which will fail all writes
    BulkDocumentWriterResults<MessageIdBasedDocument> results = new BulkDocumentWriterResults<>();
    results.addFailure(createDocument(messages.get(0)), cause, "error");
    results.addFailure(createDocument(messages.get(1)), cause, "error");
    results.addFailure(createDocument(messages.get(2)), cause, "error");
    BulkDocumentWriter<MessageIdBasedDocument> docWriter = mock(BulkDocumentWriter.class);
    when(docWriter.write()).thenReturn(results);
    // attempt to write
    ElasticsearchWriter esWriter = new ElasticsearchWriter();
    esWriter.setDocumentWriter(docWriter);
    esWriter.init(stormConf, writerConfiguration);
    BulkWriterResponse response = esWriter.write("bro", writerConfiguration, messages);
    // the writer response should only contain failures
    assertEquals(0, response.getSuccesses().size());
    assertEquals(1, response.getErrors().size());
    Collection<MessageId> errors = response.getErrors().get(cause);
    assertTrue(errors.contains(new MessageId("message1")));
    assertTrue(errors.contains(new MessageId("message2")));
    assertTrue(errors.contains(new MessageId("message3")));
}
Also used : BulkMessage(org.apache.metron.common.writer.BulkMessage) BulkDocumentWriterResults(org.apache.metron.elasticsearch.bulk.BulkDocumentWriterResults) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) MessageId(org.apache.metron.common.writer.MessageId) Test(org.junit.jupiter.api.Test)

Example 4 with BulkDocumentWriterResults

use of org.apache.metron.elasticsearch.bulk.BulkDocumentWriterResults in project metron by apache.

the class ElasticsearchWriterTest method shouldWriteManySuccessfully.

@Test
public void shouldWriteManySuccessfully() {
    // create a few message ids and the messages associated with the ids
    List<BulkMessage<JSONObject>> messages = createMessages(3);
    // create a document writer which will successfully write all
    BulkDocumentWriterResults<MessageIdBasedDocument> results = new BulkDocumentWriterResults<>();
    results.addSuccess(createDocument(messages.get(0)));
    results.addSuccess(createDocument(messages.get(1)));
    results.addSuccess(createDocument(messages.get(2)));
    BulkDocumentWriter<MessageIdBasedDocument> docWriter = mock(BulkDocumentWriter.class);
    when(docWriter.write()).thenReturn(results);
    // attempt to write
    ElasticsearchWriter esWriter = new ElasticsearchWriter();
    esWriter.setDocumentWriter(docWriter);
    esWriter.init(stormConf, writerConfiguration);
    BulkWriterResponse response = esWriter.write("bro", writerConfiguration, messages);
    // response should only contain successes
    assertFalse(response.hasErrors());
    assertTrue(response.getSuccesses().contains(new MessageId("message1")));
    assertTrue(response.getSuccesses().contains(new MessageId("message2")));
    assertTrue(response.getSuccesses().contains(new MessageId("message3")));
}
Also used : BulkMessage(org.apache.metron.common.writer.BulkMessage) BulkDocumentWriterResults(org.apache.metron.elasticsearch.bulk.BulkDocumentWriterResults) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) MessageId(org.apache.metron.common.writer.MessageId) Test(org.junit.jupiter.api.Test)

Example 5 with BulkDocumentWriterResults

use of org.apache.metron.elasticsearch.bulk.BulkDocumentWriterResults in project metron by apache.

the class ElasticsearchWriterTest method shouldWriteSuccessfullyWhenMessageTimestampIsString.

@Test
public void shouldWriteSuccessfullyWhenMessageTimestampIsString() {
    List<BulkMessage<JSONObject>> messages = createMessages(1);
    JSONObject message = messages.get(0).getMessage();
    // the timestamp is a String, rather than a Long
    message.put(Constants.Fields.TIMESTAMP.getName(), new Long(System.currentTimeMillis()).toString());
    // create the document
    String timestamp = (String) message.get(Constants.Fields.TIMESTAMP.getName());
    String guid = (String) message.get(Constants.GUID);
    String sensorType = (String) message.get(Constants.SENSOR_TYPE);
    MessageIdBasedDocument document = new MessageIdBasedDocument(message, guid, sensorType, Long.parseLong(timestamp), new MessageId("message1"));
    // create a document writer which will successfully write that document
    BulkDocumentWriterResults<MessageIdBasedDocument> results = new BulkDocumentWriterResults<>();
    results.addSuccess(document);
    BulkDocumentWriter<MessageIdBasedDocument> docWriter = mock(BulkDocumentWriter.class);
    when(docWriter.write()).thenReturn(results);
    // attempt to write
    ElasticsearchWriter esWriter = new ElasticsearchWriter();
    esWriter.setDocumentWriter(docWriter);
    esWriter.init(stormConf, writerConfiguration);
    BulkWriterResponse response = esWriter.write("bro", writerConfiguration, messages);
    // response should only contain successes
    assertFalse(response.hasErrors());
    assertTrue(response.getSuccesses().contains(new MessageId("message1")));
}
Also used : BulkMessage(org.apache.metron.common.writer.BulkMessage) JSONObject(org.json.simple.JSONObject) BulkDocumentWriterResults(org.apache.metron.elasticsearch.bulk.BulkDocumentWriterResults) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) MessageId(org.apache.metron.common.writer.MessageId) Test(org.junit.jupiter.api.Test)

Aggregations

BulkMessage (org.apache.metron.common.writer.BulkMessage)8 BulkWriterResponse (org.apache.metron.common.writer.BulkWriterResponse)8 MessageId (org.apache.metron.common.writer.MessageId)8 BulkDocumentWriterResults (org.apache.metron.elasticsearch.bulk.BulkDocumentWriterResults)8 Test (org.junit.jupiter.api.Test)8 SimpleDateFormat (java.text.SimpleDateFormat)1 JSONObject (org.json.simple.JSONObject)1