Search in sources :

Example 1 with SolrWriter

use of org.apache.metron.solr.writer.SolrWriter in project metron by apache.

the class SchemaValidationIntegrationTest method test.

public void test(String sensorType) throws Exception {
    SolrComponent component = null;
    try {
        component = createSolrComponent(sensorType);
        component.start();
        component.addCollection(String.format("%s", sensorType), String.format("src/main/config/schema/%s", sensorType));
        Map<String, Object> globalConfig = getGlobalConfig(sensorType, component);
        List<BulkMessage<JSONObject>> messages = new ArrayList<>();
        Map<String, Map<String, Object>> index = new HashMap<>();
        int i = 0;
        for (String message : getData(sensorType)) {
            if (message.trim().length() > 0) {
                Map<String, Object> m = JSONUtils.INSTANCE.load(message.trim(), JSONUtils.MAP_SUPPLIER);
                String guid = getGuid(m);
                index.put(guid, m);
                messages.add(new BulkMessage<>(String.format("message%d", ++i), new JSONObject(m)));
            }
        }
        assertTrue(messages.size() > 0);
        SolrWriter solrWriter = new SolrWriter();
        WriterConfiguration writerConfig = new WriterConfiguration() {

            @Override
            public int getBatchSize(String sensorName) {
                return messages.size();
            }

            @Override
            public int getBatchTimeout(String sensorName) {
                return 0;
            }

            @Override
            public List<Integer> getAllConfiguredTimeouts() {
                return new ArrayList<>();
            }

            @Override
            public String getIndex(String sensorName) {
                return sensorType;
            }

            @Override
            public boolean isEnabled(String sensorName) {
                return true;
            }

            @Override
            public Map<String, Object> getSensorConfig(String sensorName) {
                return new HashMap<String, Object>() {

                    {
                        put("index", sensorType);
                        put("batchSize", messages.size());
                        put("enabled", true);
                    }
                };
            }

            @Override
            public Map<String, Object> getGlobalConfig() {
                return globalConfig;
            }

            @Override
            public boolean isDefault(String sensorName) {
                return false;
            }

            @Override
            public String getFieldNameConverter(String sensorName) {
                return null;
            }
        };
        solrWriter.init(null, writerConfig);
        BulkWriterResponse response = solrWriter.write(sensorType, writerConfig, messages);
        assertTrue(response.getErrors().isEmpty());
        for (Map<String, Object> m : component.getAllIndexedDocs(sensorType)) {
            Map<String, Object> expected = index.get(getGuid(m));
            for (Map.Entry<String, Object> field : expected.entrySet()) {
                if (field.getValue() instanceof Collection && ((Collection) field.getValue()).size() == 0) {
                    continue;
                }
                if (m.get(field.getKey()) instanceof Number) {
                    Number n1 = ConversionUtils.convert(field.getValue(), Double.class);
                    Number n2 = (Number) m.get(field.getKey());
                    boolean isSame = Math.abs(n1.doubleValue() - n2.doubleValue()) < 1e-3;
                    if (!isSame) {
                        String s1 = "" + n1.doubleValue();
                        String s2 = "" + n2.doubleValue();
                        isSame = s1.startsWith(s2) || s2.startsWith(s1);
                    }
                    assertTrue(isSame, "Unable to validate " + field.getKey() + ": " + n1 + " != " + n2);
                } else {
                    assertEquals("" + field.getValue(), "" + m.get(field.getKey()), "Unable to find " + field.getKey());
                }
            }
        }
    } finally {
        if (component != null) {
            component.stop();
        }
    }
}
Also used : SolrComponent(org.apache.metron.solr.integration.components.SolrComponent) WriterConfiguration(org.apache.metron.common.configuration.writer.WriterConfiguration) BulkMessage(org.apache.metron.common.writer.BulkMessage) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) SolrWriter(org.apache.metron.solr.writer.SolrWriter) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse)

Aggregations

WriterConfiguration (org.apache.metron.common.configuration.writer.WriterConfiguration)1 BulkMessage (org.apache.metron.common.writer.BulkMessage)1 BulkWriterResponse (org.apache.metron.common.writer.BulkWriterResponse)1 SolrComponent (org.apache.metron.solr.integration.components.SolrComponent)1 SolrWriter (org.apache.metron.solr.writer.SolrWriter)1 JSONObject (org.json.simple.JSONObject)1