Search in sources :

Example 1 with BulkWriterResponse

use of org.apache.metron.common.writer.BulkWriterResponse in project metron by apache.

the class ElasticsearchWriterTest method testSingleFailure.

@Test
public void testSingleFailure() throws Exception {
    Tuple tuple1 = mock(Tuple.class);
    BulkResponse response = mock(BulkResponse.class);
    when(response.hasFailures()).thenReturn(true);
    Exception e = new IllegalStateException();
    BulkItemResponse itemResponse = buildBulkItemFailure(e);
    when(response.iterator()).thenReturn(ImmutableList.of(itemResponse).iterator());
    BulkWriterResponse expected = new BulkWriterResponse();
    expected.addError(e, tuple1);
    ElasticsearchWriter esWriter = new ElasticsearchWriter();
    BulkWriterResponse actual = esWriter.buildWriteReponse(ImmutableList.of(tuple1), response);
    assertEquals("Response should have one error and zero successes", expected, actual);
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Tuple(org.apache.storm.tuple.Tuple) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Test(org.junit.Test)

Example 2 with BulkWriterResponse

use of org.apache.metron.common.writer.BulkWriterResponse in project metron by apache.

the class SolrWriter method write.

@Override
public BulkWriterResponse write(String sourceType, WriterConfiguration configurations, Iterable<Tuple> tuples, List<JSONObject> messages) throws Exception {
    for (JSONObject message : messages) {
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", getIdValue(message));
        document.addField("sensorType", sourceType);
        for (Object key : message.keySet()) {
            Object value = message.get(key);
            document.addField(getFieldName(key, value), value);
        }
        UpdateResponse response = solr.add(document);
    }
    if (shouldCommit) {
        solr.commit(getCollection(configurations));
    }
    // Solr commits the entire batch or throws an exception for it.  There's no way to get partial failures.
    BulkWriterResponse response = new BulkWriterResponse();
    response.addAllSuccesses(tuples);
    return response;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse)

Example 3 with BulkWriterResponse

use of org.apache.metron.common.writer.BulkWriterResponse in project metron by apache.

the class NoopWriter method write.

@Override
public BulkWriterResponse write(String sensorType, WriterConfiguration configurations, Iterable<Tuple> tuples, List<JSONObject> messages) throws Exception {
    if (sleepFunction != null) {
        sleepFunction.apply(null);
    }
    BulkWriterResponse response = new BulkWriterResponse();
    response.addAllSuccesses(tuples);
    return response;
}
Also used : BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse)

Example 4 with BulkWriterResponse

use of org.apache.metron.common.writer.BulkWriterResponse in project metron by apache.

the class HdfsWriter method write.

@Override
public BulkWriterResponse write(String sourceType, WriterConfiguration configurations, Iterable<Tuple> tuples, List<JSONObject> messages) throws Exception {
    BulkWriterResponse response = new BulkWriterResponse();
    // Currently treating all the messages in a group for pass/failure.
    try {
        // Messages can all result in different HDFS paths, because of Stellar Expressions, so we'll need to iterate through
        for (JSONObject message : messages) {
            String path = getHdfsPathExtension(sourceType, (String) configurations.getSensorConfig(sourceType).getOrDefault(IndexingConfigurations.OUTPUT_PATH_FUNCTION_CONF, ""), message);
            SourceHandler handler = getSourceHandler(sourceType, path, configurations);
            handler.handle(message, sourceType, configurations, syncPolicyCreator);
        }
    } catch (Exception e) {
        response.addAllErrors(e, tuples);
    }
    response.addAllSuccesses(tuples);
    return response;
}
Also used : JSONObject(org.json.simple.JSONObject) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse)

Example 5 with BulkWriterResponse

use of org.apache.metron.common.writer.BulkWriterResponse in project metron by apache.

the class SimpleHbaseEnrichmentWriter method write.

@Override
public BulkWriterResponse write(String sensorType, WriterConfiguration configurations, Iterable<Tuple> tuples, List<JSONObject> messages) throws Exception {
    Map<String, Object> sensorConfig = configurations.getSensorConfig(sensorType);
    HTableInterface table = getTable(sensorConfig);
    KeyTransformer transformer = getTransformer(sensorConfig);
    Object enrichmentTypeObj = Configurations.ENRICHMENT_TYPE.get(sensorConfig);
    String enrichmentType = enrichmentTypeObj == null ? null : enrichmentTypeObj.toString();
    Set<String> valueColumns = new HashSet<>(getColumns(Configurations.VALUE_COLUMNS.get(sensorConfig), true));
    List<Put> puts = new ArrayList<>();
    for (JSONObject message : messages) {
        EnrichmentKey key = getKey(message, transformer, enrichmentType);
        EnrichmentValue value = getValue(message, transformer.keySet, valueColumns);
        if (key == null || value == null) {
            continue;
        }
        Put put = converter.toPut(this.cf, key, value);
        if (put != null) {
            LOG.debug("Put: {Column Family: '{}', Key: '{}', Value: '{}'}", this.cf, key, value);
            puts.add(put);
        }
    }
    BulkWriterResponse response = new BulkWriterResponse();
    try {
        table.put(puts);
    } catch (Exception e) {
        response.addAllErrors(e, tuples);
        return response;
    }
    // Can return no errors, because put will throw Exception on error.
    response.addAllSuccesses(tuples);
    return response;
}
Also used : HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) Put(org.apache.hadoop.hbase.client.Put) EnrichmentKey(org.apache.metron.enrichment.converter.EnrichmentKey) IOException(java.io.IOException) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) EnrichmentValue(org.apache.metron.enrichment.converter.EnrichmentValue) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse)

Aggregations

BulkWriterResponse (org.apache.metron.common.writer.BulkWriterResponse)27 Test (org.junit.Test)20 JSONObject (org.json.simple.JSONObject)15 Tuple (org.apache.storm.tuple.Tuple)13 HashMap (java.util.HashMap)8 ParserConfigurations (org.apache.metron.common.configuration.ParserConfigurations)7 BaseBoltTest (org.apache.metron.test.bolt.BaseBoltTest)7 WriterConfiguration (org.apache.metron.common.configuration.writer.WriterConfiguration)6 TopologyContext (org.apache.storm.task.TopologyContext)6 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)6 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 FileInputStream (java.io.FileInputStream)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ParserWriterConfiguration (org.apache.metron.common.configuration.writer.ParserWriterConfiguration)3 MetronError (org.apache.metron.common.error.MetronError)3 Context (org.apache.metron.stellar.dsl.Context)3 BaseEnrichmentBoltTest (org.apache.metron.test.bolt.BaseEnrichmentBoltTest)3 BulkMessageWriterBolt (org.apache.metron.writer.bolt.BulkMessageWriterBolt)3