Search in sources :

Example 41 with DefaultObjectMapper

use of org.apache.druid.jackson.DefaultObjectMapper in project druid by druid-io.

the class ExtractionDimensionSpecTest method testSerdeBackwardsCompatibility.

@Test
public void testSerdeBackwardsCompatibility() throws Exception {
    final ObjectMapper objectMapper = new DefaultObjectMapper();
    final String oldJson = "{\n" + "    \"type\": \"extraction\",\n" + "    \"outputName\": \"first3Letters\",\n" + "    \"dimension\": \"myDim\"," + "    \"dimExtractionFn\": {\n" + "        \"type\": \"regex\",\n" + "        \"expr\": \"(...).*\"\n" + "    }\n" + "}";
    final ExtractionDimensionSpec extractionDimensionSpec = (ExtractionDimensionSpec) objectMapper.readValue(oldJson, DimensionSpec.class);
    Assert.assertEquals("first3Letters", extractionDimensionSpec.getOutputName());
    Assert.assertEquals("myDim", extractionDimensionSpec.getDimension());
    Assert.assertNotNull(extractionDimensionSpec.getExtractionFn());
    Assert.assertTrue(extractionDimensionSpec.getExtractionFn() instanceof RegexDimExtractionFn);
    Assert.assertEquals(extractionDimensionSpec, objectMapper.readValue(objectMapper.writeValueAsBytes(extractionDimensionSpec), DimensionSpec.class));
    // new trumps old
    final String oldAndNewJson = "{\n" + "    \"type\": \"extraction\",\n" + "    \"outputName\": \"first3Letters\",\n" + "    \"dimension\": \"myDim\"," + "    \"extractionFn\": {\n" + "        \"type\": \"partial\",\n" + "        \"expr\": \"(...).*\"\n" + "    },\n" + "    \"dimExtractionFn\": {\n" + "        \"type\": \"regex\",\n" + "        \"expr\": \"(...).*\"\n" + "    }\n" + "}";
    Assert.assertTrue(objectMapper.readValue(oldAndNewJson, DimensionSpec.class).getExtractionFn() instanceof MatchingDimExtractionFn);
}
Also used : MatchingDimExtractionFn(org.apache.druid.query.extraction.MatchingDimExtractionFn) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) Test(org.junit.Test)

Example 42 with DefaultObjectMapper

use of org.apache.druid.jackson.DefaultObjectMapper in project druid by druid-io.

the class FloatLastAggregationTest method testSerde.

@Test
public void testSerde() throws Exception {
    DefaultObjectMapper mapper = new DefaultObjectMapper();
    String floatSpecJson = "{\"type\":\"floatLast\",\"name\":\"billy\",\"fieldName\":\"nilly\"}";
    AggregatorFactory deserialized = mapper.readValue(floatSpecJson, AggregatorFactory.class);
    Assert.assertEquals(floatLastAggregatorFactory, deserialized);
    Assert.assertArrayEquals(floatLastAggregatorFactory.getCacheKey(), deserialized.getCacheKey());
}
Also used : DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 43 with DefaultObjectMapper

use of org.apache.druid.jackson.DefaultObjectMapper in project druid by druid-io.

the class ResponseContextTest method serializeWithTruncateValueTest.

@Test
public void serializeWithTruncateValueTest() throws IOException {
    final ResponseContext ctx = ResponseContext.createEmpty();
    ctx.put(EXTN_COUNTER_KEY, 100L);
    ctx.put(EXTN_STRING_KEY, "long-string-that-is-supposed-to-be-removed-from-result");
    final DefaultObjectMapper objectMapper = new DefaultObjectMapper();
    final ResponseContext.SerializationResult res1 = ctx.serializeWith(objectMapper, Integer.MAX_VALUE);
    Assert.assertEquals(ctx.getDelegate(), deserializeContext(res1.getResult(), objectMapper));
    final ResponseContext ctxCopy = ResponseContext.createEmpty();
    ctxCopy.merge(ctx);
    final int target = EXTN_COUNTER_KEY.getName().length() + 3 + Keys.TRUNCATED.getName().length() + 5 + // Fudge factor for quotes, separators, etc.
    15;
    final ResponseContext.SerializationResult res2 = ctx.serializeWith(objectMapper, target);
    ctxCopy.remove(EXTN_STRING_KEY);
    ctxCopy.put(Keys.TRUNCATED, true);
    Assert.assertEquals(ctxCopy.getDelegate(), deserializeContext(res2.getResult(), objectMapper));
}
Also used : DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Example 44 with DefaultObjectMapper

use of org.apache.druid.jackson.DefaultObjectMapper in project druid by druid-io.

the class ResponseContextTest method serializeWithCorrectnessTest.

@Test
public void serializeWithCorrectnessTest() throws JsonProcessingException {
    final ResponseContext ctx1 = ResponseContext.createEmpty();
    ctx1.add(EXTN_STRING_KEY, "string-value");
    final DefaultObjectMapper mapper = new DefaultObjectMapper();
    Assert.assertEquals(mapper.writeValueAsString(ImmutableMap.of(EXTN_STRING_KEY.getName(), "string-value")), ctx1.serializeWith(mapper, Integer.MAX_VALUE).getResult());
    final ResponseContext ctx2 = ResponseContext.createEmpty();
    // Add two non-header fields, and one that will be in the header
    ctx2.putEntityTag("not in header");
    ctx2.addCpuNanos(100);
    ctx2.add(EXTN_COUNTER_KEY, 100);
    Assert.assertEquals(mapper.writeValueAsString(ImmutableMap.of(EXTN_COUNTER_KEY.getName(), 100)), ctx2.serializeWith(mapper, Integer.MAX_VALUE).getResult());
}
Also used : DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Example 45 with DefaultObjectMapper

use of org.apache.druid.jackson.DefaultObjectMapper in project druid by druid-io.

the class ResponseContextTest method deserializeTest.

@Test
public void deserializeTest() throws IOException {
    final DefaultObjectMapper mapper = new DefaultObjectMapper();
    final ResponseContext ctx = ResponseContext.deserialize(mapper.writeValueAsString(ImmutableMap.of(Keys.ETAG.getName(), "string-value", Keys.NUM_SCANNED_ROWS.getName(), 100L, Keys.CPU_CONSUMED_NANOS.getName(), 100000L)), mapper);
    Assert.assertEquals("string-value", ctx.getEntityTag());
    Assert.assertEquals((Long) 100L, ctx.getRowScanCount());
    Assert.assertEquals((Long) 100000L, ctx.getCpuNanos());
    ctx.addRowScanCount(10L);
    Assert.assertEquals((Long) 110L, ctx.getRowScanCount());
    ctx.addCpuNanos(100L);
    Assert.assertEquals((Long) 100100L, ctx.getCpuNanos());
}
Also used : DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Aggregations

DefaultObjectMapper (org.apache.druid.jackson.DefaultObjectMapper)287 Test (org.junit.Test)245 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)177 File (java.io.File)26 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)25 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)21 Before (org.junit.Before)21 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)20 InjectableValues (com.fasterxml.jackson.databind.InjectableValues)16 DataSegment (org.apache.druid.timeline.DataSegment)16 Period (org.joda.time.Period)16 ConstantPostAggregator (org.apache.druid.query.aggregation.post.ConstantPostAggregator)14 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)13 FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)12 DimensionsSpec (org.apache.druid.data.input.impl.DimensionsSpec)11 TaskStatus (org.apache.druid.indexer.TaskStatus)11 AvroStreamInputRowParserTest (org.apache.druid.data.input.AvroStreamInputRowParserTest)10 ImmutableList (com.google.common.collect.ImmutableList)9 Injector (com.google.inject.Injector)9 TimestampSpec (org.apache.druid.data.input.impl.TimestampSpec)9