Search in sources :

Example 91 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project heron by twitter.

the class ScribeSink method makeJSON.

// Convert a record into json format required by Twitter Cuckoo
private String makeJSON(MetricsRecord record) {
    String serviceName = String.format("%s/%s", config.get(KEY_SERVICE_NAMESPACE), topologyName);
    // The source format is "host:port/componentName/instanceId"
    // However, we need just "/componentName/instanceId"
    String[] sources = record.getSource().split("/");
    String source = String.format("/%s/%s", sources[1], sources[2]);
    // The timestamp is in ms, however, we need to convert it in seconds to fit Twitter Infra
    long timestamp = record.getTimestamp() / Constants.SECONDS_TO_MILLISECONDS;
    Map<String, Object> json = new HashMap<String, Object>();
    // Add the service name
    json.put("service", serviceName);
    // Add the service name
    json.put("source", source);
    // Add the time stamp
    json.put("timestamp", timestamp);
    // Cuckoo_json allows multi-metrics in a single JSON, so we would like to
    // package all metrics received into one single JSON
    int metricsToWrite = 0;
    for (MetricsInfo info : record.getMetrics()) {
        // First check whether the metric  value is legal
        // since scribe would only accept a Double value as metric value
        // We would just skip it
        Double val;
        try {
            val = Double.valueOf(info.getValue());
        } catch (NumberFormatException ne) {
            LOG.log(Level.SEVERE, "Could not parse illegal metric: " + info.toString());
            sinkContext.exportCountMetric(ILLEGAL_METRICS_COUNT, 1);
            continue;
        }
        json.put(info.getName(), val);
        metricsToWrite++;
    }
    LOG.info(metricsToWrite + " metrics added");
    String result = "";
    try {
        result = MAPPER.writeValueAsString(json);
    } catch (JsonProcessingException e) {
        LOG.log(Level.SEVERE, "Could not convert map to JSONString: " + json.toString(), e);
    }
    return result;
}
Also used : MetricsInfo(com.twitter.heron.spi.metricsmgr.metrics.MetricsInfo) HashMap(java.util.HashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 92 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project graylog2-server by Graylog2.

the class BsonReader method readBsonFile.

protected List<DBObject> readBsonFile(String filename) {
    Path filePath = Paths.get(filename);
    List<DBObject> dataset = new ArrayList<>();
    try {
        ByteArrayInputStream fileBytes = new ByteArrayInputStream(Files.readAllBytes(filePath));
        BSONDecoder decoder = new BasicBSONDecoder();
        BSONObject obj;
        while ((obj = decoder.readObject(fileBytes)) != null) {
            final DBObject mongoDocument = new BasicDBObject(obj.toMap());
            dataset.add(mongoDocument);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw new RuntimeException("Can not open BSON input file.", e);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        throw new RuntimeException("Can not parse BSON data.", e);
    } catch (IOException e) {
    //EOF
    }
    return dataset;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) BSONObject(org.bson.BSONObject) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicBSONDecoder(org.bson.BasicBSONDecoder) BSONDecoder(org.bson.BSONDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BasicBSONDecoder(org.bson.BasicBSONDecoder)

Example 93 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project keywhiz by square.

the class AuthHelper method buildLoginPost.

/**
   * Builds a Keywhiz login request.
   *
   * @param username login username
   * @param password login password
   * @return valid login request, given a valid username and password
   */
public static Request buildLoginPost(@Nullable String username, @Nullable String password) {
    Map<String, String> login = Maps.newHashMap();
    if (username != null) {
        login.put("username", username);
    }
    if (password != null) {
        login.put("password", password);
    }
    RequestBody body;
    try {
        body = RequestBody.create(KeywhizClient.JSON, MAPPER.writeValueAsString(login));
    } catch (JsonProcessingException e) {
        throw new AssertionError(e);
    }
    return new Request.Builder().url(HttpClients.testUrl("/admin/login")).post(body).addHeader("Content-Type", MediaType.APPLICATION_JSON).build();
}
Also used : JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RequestBody(okhttp3.RequestBody)

Example 94 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project keywhiz by square.

the class GroupDAO method createGroup.

public long createGroup(String name, String creator, String description, ImmutableMap<String, String> metadata) {
    GroupsRecord r = dslContext.newRecord(GROUPS);
    String jsonMetadata;
    try {
        jsonMetadata = mapper.writeValueAsString(metadata);
    } catch (JsonProcessingException e) {
        // Serialization of a Map<String, String> can never fail.
        throw Throwables.propagate(e);
    }
    long now = OffsetDateTime.now().toEpochSecond();
    r.setName(name);
    r.setCreatedby(creator);
    r.setCreatedat(now);
    r.setUpdatedby(creator);
    r.setUpdatedat(now);
    r.setDescription(description);
    r.setMetadata(jsonMetadata);
    r.store();
    return r.getId();
}
Also used : GroupsRecord(keywhiz.jooq.tables.records.GroupsRecord) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 95 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project keywhiz by square.

the class SecretSeriesDAO method createSecretSeries.

long createSecretSeries(String name, String creator, String description, @Nullable String type, @Nullable Map<String, String> generationOptions) {
    SecretsRecord r = dslContext.newRecord(SECRETS);
    long now = OffsetDateTime.now().toEpochSecond();
    r.setName(name);
    r.setDescription(description);
    r.setCreatedby(creator);
    r.setCreatedat(now);
    r.setUpdatedby(creator);
    r.setUpdatedat(now);
    r.setType(type);
    if (generationOptions != null) {
        try {
            r.setOptions(mapper.writeValueAsString(generationOptions));
        } catch (JsonProcessingException e) {
            // Serialization of a Map<String, String> can never fail.
            throw Throwables.propagate(e);
        }
    } else {
        r.setOptions("{}");
    }
    r.store();
    return r.getId();
}
Also used : SecretsRecord(keywhiz.jooq.tables.records.SecretsRecord) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)153 IOException (java.io.IOException)43 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)42 HashMap (java.util.HashMap)18 ArrayList (java.util.ArrayList)15 Test (org.junit.Test)14 InputStream (java.io.InputStream)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 List (java.util.List)8 Map (java.util.Map)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7 ErrorResponseBody (com.nike.riposte.server.error.handler.ErrorResponseBody)6 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)6 OutputStreamWriter (java.io.OutputStreamWriter)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 TaskDTO (com.linkedin.thirdeye.datalayer.dto.TaskDTO)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 SimpleDateFormat (java.text.SimpleDateFormat)4 Test (org.testng.annotations.Test)4