Search in sources :

Example 1 with JsonExtractor

use of org.graylog2.inputs.extractors.JsonExtractor in project graylog2-server by Graylog2.

the class JsonTesterResource method testJsonExtractor.

private JsonTesterResponse testJsonExtractor(String testString, boolean flatten, String listSeparator, String keySeparator, String kvSeparator, boolean replaceKeyWhitespace, String keyWhitespaceReplacement, String keyPrefix) {
    final Map<String, Object> config = ImmutableMap.<String, Object>builder().put("flatten", flatten).put("list_separator", listSeparator).put("key_separator", keySeparator).put("kv_separator", kvSeparator).put("replace_key_whitespace", replaceKeyWhitespace).put("key_whitespace_replacement", keyWhitespaceReplacement).put("key_prefix", keyPrefix).build();
    final JsonExtractor extractor;
    try {
        extractor = new JsonExtractor(new MetricRegistry(), "test", "Test", 0L, Extractor.CursorStrategy.COPY, "test", "test", config, getCurrentUser().getName(), Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, "");
    } catch (Extractor.ReservedFieldException e) {
        throw new BadRequestException("Trying to overwrite a reserved message field", e);
    } catch (ConfigurationException e) {
        throw new BadRequestException("Invalid extractor configuration", e);
    }
    final Map<String, Object> result;
    try {
        result = extractor.extractJson(testString);
    } catch (IOException e) {
        throw new BadRequestException("Failure running JSON extractor: " + e.getMessage(), e);
    }
    return JsonTesterResponse.create(result, flatten, listSeparator, keySeparator, kvSeparator, testString);
}
Also used : ConfigurationException(org.graylog2.ConfigurationException) MetricRegistry(com.codahale.metrics.MetricRegistry) JsonExtractor(org.graylog2.inputs.extractors.JsonExtractor) Converter(org.graylog2.plugin.inputs.Converter) BadRequestException(javax.ws.rs.BadRequestException) Extractor(org.graylog2.plugin.inputs.Extractor) JsonExtractor(org.graylog2.inputs.extractors.JsonExtractor) IOException(java.io.IOException)

Example 2 with JsonExtractor

use of org.graylog2.inputs.extractors.JsonExtractor in project graylog2-server by Graylog2.

the class JsonExtractorTest method testRunWithFlattenedObjectAndDifferentKVSeparator.

@Test
public void testRunWithFlattenedObjectAndDifferentKVSeparator() throws Exception {
    final JsonExtractor jsonExtractor = new JsonExtractor(new MetricRegistry(), "json", "title", 0L, Extractor.CursorStrategy.COPY, "source", "target", ImmutableMap.<String, Object>of("flatten", true, "kv_separator", ":"), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, "");
    final String value = "{\"object\": {\"text\": \"foobar\", \"number\": 1234.5678, \"bool\": true, \"nested\": {\"text\": \"foobar\"}}}";
    final Extractor.Result[] results = jsonExtractor.run(value);
    assertThat(results).contains(new Extractor.Result("text:foobar, number:1234.5678, bool:true, nested:{text=foobar}", "object", -1, -1));
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Extractor(org.graylog2.plugin.inputs.Extractor) Test(org.junit.Test)

Example 3 with JsonExtractor

use of org.graylog2.inputs.extractors.JsonExtractor in project graylog2-server by Graylog2.

the class JsonExtractorTest method testRunWithWhitespaceInNestedKey.

@Test
public void testRunWithWhitespaceInNestedKey() throws Exception {
    final String value = "{\"foo\":{\"b a r\":{\"b a z\": 42}}}";
    final JsonExtractor jsonExtractor = new JsonExtractor(new MetricRegistry(), "json", "title", 0L, Extractor.CursorStrategy.COPY, "source", "target", ImmutableMap.of("replace_key_whitespace", true, "key_whitespace_replacement", "-"), "user", Collections.emptyList(), Extractor.ConditionType.NONE, "");
    assertThat(jsonExtractor.run(value)).containsOnly(new Extractor.Result(42, "foo_b-a-r_b-a-z", -1, -1));
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Extractor(org.graylog2.plugin.inputs.Extractor) Test(org.junit.Test)

Example 4 with JsonExtractor

use of org.graylog2.inputs.extractors.JsonExtractor in project graylog2-server by Graylog2.

the class JsonExtractorTest method testRunWithKeyPrefix.

@Test
public void testRunWithKeyPrefix() throws Exception {
    final String value = "{\"text string\": \"foobar\", \"num   b er\": 1234.5678, \"bool\": true, \"null\": null}";
    final JsonExtractor jsonExtractor1 = new JsonExtractor(new MetricRegistry(), "json", "title", 0L, Extractor.CursorStrategy.COPY, "source", "target", ImmutableMap.of("key_prefix", "test_"), "user", Collections.emptyList(), Extractor.ConditionType.NONE, "");
    assertThat(jsonExtractor1.run(value)).contains(new Extractor.Result("foobar", "test_text string", -1, -1), new Extractor.Result(1234.5678, "test_num   b er", -1, -1), new Extractor.Result(true, "test_bool", -1, -1));
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Extractor(org.graylog2.plugin.inputs.Extractor) Test(org.junit.Test)

Example 5 with JsonExtractor

use of org.graylog2.inputs.extractors.JsonExtractor in project graylog2-server by Graylog2.

the class JsonExtractorTest method testRunWithWhitespaceInKey.

@Test
public void testRunWithWhitespaceInKey() throws Exception {
    final String value = "{\"text string\": \"foobar\", \"num   b er\": 1234.5678, \"bool\": true, \"null\": null}";
    final JsonExtractor jsonExtractor1 = new JsonExtractor(new MetricRegistry(), "json", "title", 0L, Extractor.CursorStrategy.COPY, "source", "target", Collections.emptyMap(), "user", Collections.emptyList(), Extractor.ConditionType.NONE, "");
    final JsonExtractor jsonExtractor2 = new JsonExtractor(new MetricRegistry(), "json", "title", 0L, Extractor.CursorStrategy.COPY, "source", "target", ImmutableMap.of("replace_key_whitespace", true), "user", Collections.emptyList(), Extractor.ConditionType.NONE, "");
    final JsonExtractor jsonExtractor3 = new JsonExtractor(new MetricRegistry(), "json", "title", 0L, Extractor.CursorStrategy.COPY, "source", "target", ImmutableMap.of("replace_key_whitespace", true, "key_whitespace_replacement", ":"), "user", Collections.emptyList(), Extractor.ConditionType.NONE, "");
    assertThat(jsonExtractor1.run(value)).contains(new Extractor.Result("foobar", "text string", -1, -1), new Extractor.Result(1234.5678, "num   b er", -1, -1), new Extractor.Result(true, "bool", -1, -1));
    assertThat(jsonExtractor2.run(value)).contains(new Extractor.Result("foobar", "text_string", -1, -1), new Extractor.Result(1234.5678, "num___b_er", -1, -1), new Extractor.Result(true, "bool", -1, -1));
    assertThat(jsonExtractor3.run(value)).contains(new Extractor.Result("foobar", "text:string", -1, -1), new Extractor.Result(1234.5678, "num:::b:er", -1, -1), new Extractor.Result(true, "bool", -1, -1));
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Extractor(org.graylog2.plugin.inputs.Extractor) Test(org.junit.Test)

Aggregations

MetricRegistry (com.codahale.metrics.MetricRegistry)8 Extractor (org.graylog2.plugin.inputs.Extractor)8 Test (org.junit.Test)7 IOException (java.io.IOException)1 BadRequestException (javax.ws.rs.BadRequestException)1 ConfigurationException (org.graylog2.ConfigurationException)1 JsonExtractor (org.graylog2.inputs.extractors.JsonExtractor)1 Converter (org.graylog2.plugin.inputs.Converter)1