Search in sources :

Example 1 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project pinot by linkedin.

the class Utils method convertToMetricExpressions.

public static List<MetricExpression> convertToMetricExpressions(String metricsJson, MetricAggFunction aggFunction, String collection) throws ExecutionException {
    List<MetricExpression> metricExpressions = new ArrayList<>();
    if (metricsJson == null) {
        return metricExpressions;
    }
    ArrayList<String> metricExpressionNames;
    try {
        TypeReference<ArrayList<String>> valueTypeRef = new TypeReference<ArrayList<String>>() {
        };
        metricExpressionNames = OBJECT_MAPPER.readValue(metricsJson, valueTypeRef);
    } catch (Exception e) {
        LOG.warn("Expected json expression for metric [{}], adding as it is. Error in json parsing : [{}]", metricsJson, e.getMessage());
        metricExpressionNames = new ArrayList<>();
        String[] metrics = metricsJson.split(",");
        for (String metric : metrics) {
            metricExpressionNames.add(metric.trim());
        }
    }
    for (String metricExpressionName : metricExpressionNames) {
        String derivedMetricExpression = ThirdEyeUtils.getDerivedMetricExpression(metricExpressionName, collection);
        MetricExpression metricExpression = new MetricExpression(metricExpressionName, derivedMetricExpression, aggFunction);
        metricExpressions.add(metricExpression);
    }
    return metricExpressions;
}
Also used : ArrayList(java.util.ArrayList) TypeReference(com.fasterxml.jackson.core.type.TypeReference) MetricExpression(com.linkedin.thirdeye.client.MetricExpression) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project druid by druid-io.

the class SQLMetadataRuleManagerTest method testAuditEntryCreated.

@Test
public void testAuditEntryCreated() throws Exception {
    List<Rule> rules = Arrays.<Rule>asList(new IntervalLoadRule(new Interval("2015-01-01/2015-02-01"), ImmutableMap.<String, Integer>of(DruidServer.DEFAULT_TIER, DruidServer.DEFAULT_NUM_REPLICANTS)));
    AuditInfo auditInfo = new AuditInfo("test_author", "test_comment", "127.0.0.1");
    ruleManager.overrideRule("test_dataSource", rules, auditInfo);
    // fetch rules from metadata storage
    ruleManager.poll();
    Assert.assertEquals(rules, ruleManager.getRules("test_dataSource"));
    // verify audit entry is created
    List<AuditEntry> auditEntries = auditManager.fetchAuditHistory("test_dataSource", "rules", null);
    Assert.assertEquals(1, auditEntries.size());
    AuditEntry entry = auditEntries.get(0);
    Assert.assertEquals(rules, mapper.readValue(entry.getPayload(), new TypeReference<List<Rule>>() {
    }));
    Assert.assertEquals(auditInfo, entry.getAuditInfo());
    Assert.assertEquals("test_dataSource", entry.getKey());
}
Also used : AuditInfo(io.druid.audit.AuditInfo) IntervalLoadRule(io.druid.server.coordinator.rules.IntervalLoadRule) AuditEntry(io.druid.audit.AuditEntry) IntervalLoadRule(io.druid.server.coordinator.rules.IntervalLoadRule) Rule(io.druid.server.coordinator.rules.Rule) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 3 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project druid by druid-io.

the class SQLMetadataRuleManagerTest method testFetchAuditEntriesForAllDataSources.

@Test
public void testFetchAuditEntriesForAllDataSources() throws Exception {
    List<Rule> rules = Arrays.<Rule>asList(new IntervalLoadRule(new Interval("2015-01-01/2015-02-01"), ImmutableMap.<String, Integer>of(DruidServer.DEFAULT_TIER, DruidServer.DEFAULT_NUM_REPLICANTS)));
    AuditInfo auditInfo = new AuditInfo("test_author", "test_comment", "127.0.0.1");
    ruleManager.overrideRule("test_dataSource", rules, auditInfo);
    ruleManager.overrideRule("test_dataSource2", rules, auditInfo);
    // fetch rules from metadata storage
    ruleManager.poll();
    Assert.assertEquals(rules, ruleManager.getRules("test_dataSource"));
    Assert.assertEquals(rules, ruleManager.getRules("test_dataSource2"));
    // test fetch audit entries
    List<AuditEntry> auditEntries = auditManager.fetchAuditHistory("rules", null);
    Assert.assertEquals(2, auditEntries.size());
    for (AuditEntry entry : auditEntries) {
        Assert.assertEquals(rules, mapper.readValue(entry.getPayload(), new TypeReference<List<Rule>>() {
        }));
        Assert.assertEquals(auditInfo, entry.getAuditInfo());
    }
}
Also used : AuditInfo(io.druid.audit.AuditInfo) IntervalLoadRule(io.druid.server.coordinator.rules.IntervalLoadRule) AuditEntry(io.druid.audit.AuditEntry) IntervalLoadRule(io.druid.server.coordinator.rules.IntervalLoadRule) Rule(io.druid.server.coordinator.rules.Rule) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 4 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project druid by druid-io.

the class SqlResourceTest method doPost.

// Returns either an error or a result.
private Pair<QueryInterruptedException, List<Map<String, Object>>> doPost(final SqlQuery query) throws Exception {
    final Response response = resource.doPost(query);
    if (response.getStatus() == 200) {
        final StreamingOutput output = (StreamingOutput) response.getEntity();
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        output.write(baos);
        return Pair.of(null, JSON_MAPPER.<List<Map<String, Object>>>readValue(baos.toByteArray(), new TypeReference<List<Map<String, Object>>>() {
        }));
    } else {
        return Pair.of(JSON_MAPPER.readValue((byte[]) response.getEntity(), QueryInterruptedException.class), null);
    }
}
Also used : Response(javax.ws.rs.core.Response) StreamingOutput(javax.ws.rs.core.StreamingOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) QueryInterruptedException(io.druid.query.QueryInterruptedException)

Example 5 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project druid by druid-io.

the class WhiteListBasedConverter method readMap.

private ImmutableSortedMap<String, ImmutableSet<String>> readMap(final String mapPath) {
    String fileContent;
    String actualPath = mapPath;
    try {
        if (Strings.isNullOrEmpty(mapPath)) {
            URL resource = this.getClass().getClassLoader().getResource("defaultWhiteListMap.json");
            actualPath = resource.getFile();
            LOGGER.info("using default whiteList map located at [%s]", actualPath);
            fileContent = Resources.toString(resource, Charset.defaultCharset());
        } else {
            fileContent = Files.asCharSource(new File(mapPath), Charset.forName("UTF-8")).read();
        }
        return mapper.reader(new TypeReference<ImmutableSortedMap<String, ImmutableSet<String>>>() {
        }).readValue(fileContent);
    } catch (IOException e) {
        throw new ISE(e, "Got an exception while parsing file [%s]", actualPath);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ISE(io.druid.java.util.common.ISE) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL)

Aggregations

TypeReference (com.fasterxml.jackson.core.type.TypeReference)335 IOException (java.io.IOException)138 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)128 Test (org.junit.Test)101 ArrayList (java.util.ArrayList)79 Map (java.util.Map)76 List (java.util.List)70 HashMap (java.util.HashMap)60 File (java.io.File)36 Collectors (java.util.stream.Collectors)29 InputStream (java.io.InputStream)27 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)27 JsonNode (com.fasterxml.jackson.databind.JsonNode)21 ImmutableMap (com.google.common.collect.ImmutableMap)20 lombok.val (lombok.val)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 Set (java.util.Set)16 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)15 Collections (java.util.Collections)15 Logger (org.slf4j.Logger)15