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;
}
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;
}
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();
}
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();
}
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();
}
Aggregations