use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project drill by apache.
the class JSONFormatPlugin method writeStatistics.
@Override
public void writeStatistics(TableStatistics statistics, FileSystem fs, Path statsTablePath) throws IOException {
FSDataOutputStream stream = null;
JsonGenerator generator = null;
try {
JsonFactory factory = new JsonFactory();
stream = fs.create(statsTablePath);
ObjectMapper mapper = DrillStatsTable.getMapper();
generator = factory.createGenerator((OutputStream) stream).useDefaultPrettyPrinter().setCodec(mapper);
mapper.writeValue(generator, statistics);
} catch (com.fasterxml.jackson.core.JsonGenerationException ex) {
logger.error("Unable to create file (JSON generation error): " + statsTablePath.getName(), ex);
throw ex;
} catch (com.fasterxml.jackson.databind.JsonMappingException ex) {
logger.error("Unable to create file (JSON mapping error): " + statsTablePath.getName(), ex);
throw ex;
} catch (IOException ex) {
logger.error("Unable to create file " + statsTablePath.getName(), ex);
} finally {
if (generator != null) {
generator.flush();
}
if (stream != null) {
stream.close();
}
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project seldon-core by SeldonIO.
the class DeploymentWatcher method removeCreationTimestampField.
private String removeCreationTimestampField(String json) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getFactory();
JsonParser parser = factory.createParser(json);
JsonNode obj = mapper.readTree(parser);
if (obj.has("metadata") && obj.get("metadata").has("creationTimestamp")) {
((ObjectNode) obj.get("metadata")).remove("creationTimestamp");
return mapper.writeValueAsString(obj);
} else
return json;
} catch (JsonParseException e) {
logger.error("Failed to remove creationTimestamp");
return json;
} catch (IOException e) {
logger.error("Failed to remove creationTimestamp");
return json;
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project seldon-core by SeldonIO.
the class TestJsonParse method multiDimTest.
@Test
public void multiDimTest() throws JsonProcessingException, IOException {
String json = "{\"request\":{\"values\":[[1.0]]}}";
System.out.println(json);
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getFactory();
JsonParser parser = factory.createParser(json);
JsonNode j = mapper.readTree(parser);
JsonNode values = j.get("request").get("values");
double[][] v = mapper.readValue(values.toString(), double[][].class);
double[] vs = Doubles.concat(v);
int[] shape = { v.length, v[0].length };
((ObjectNode) j.get("request")).replace("values", mapper.valueToTree(vs));
((ObjectNode) j.get("request")).set("shape", mapper.valueToTree(shape));
System.out.println(j.toString());
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project presto by prestodb.
the class PrometheusQueryResponse method parsePrometheusQueryResponse.
private void parsePrometheusQueryResponse(InputStream response) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
JsonParser parser = new JsonFactory().createParser(response);
while (!parser.isClosed()) {
JsonToken jsonToken = parser.nextToken();
if (FIELD_NAME.equals(jsonToken)) {
if (parser.getCurrentName().equals(parseResultStatus)) {
parser.nextToken();
if (parser.getValueAsString().equals(parseResultSuccess)) {
this.status = true;
while (!parser.isClosed()) {
parser.nextToken();
if (FIELD_NAME.equals(jsonToken)) {
if (parser.getCurrentName().equals(parseResultType)) {
parser.nextToken();
resultType = ResultType.valueOf(parser.getValueAsString());
}
if (parser.getCurrentName().equals(parseResult)) {
parser.nextToken();
ArrayNode node = mapper.readTree(parser);
result = node.toString();
break;
}
}
}
} else {
// error path
String parsedStatus = parser.getValueAsString();
// parsing json is key-value based, so first nextToken is advanced to the key, nextToken advances to the value.
// for "errorType" key
parser.nextToken();
// for "errorType" key's value
parser.nextToken();
errorType = parser.getValueAsString();
// advance to "error" key
parser.nextToken();
// advance to "error" key's value
parser.nextToken();
error = parser.getValueAsString();
throw new PrestoException(PROMETHEUS_PARSE_ERROR, "Unable to parse Prometheus response: " + parsedStatus + " " + errorType + " " + error);
}
}
}
if (result != null) {
break;
}
}
if (result != null && resultType != null) {
switch(resultType) {
case matrix:
case vector:
results = mapper.readValue(result, new TypeReference<List<PrometheusMetricResult>>() {
});
break;
case scalar:
case string:
PrometheusTimeSeriesValue stringOrScalarResult = mapper.readValue(result, new TypeReference<PrometheusTimeSeriesValue>() {
});
Map<String, String> madeUpMetricHeader = new HashMap<>();
madeUpMetricHeader.put("__name__", resultType.toString());
PrometheusTimeSeriesValueArray timeSeriesValues = new PrometheusTimeSeriesValueArray(singletonList(stringOrScalarResult));
results = singletonList(new PrometheusMetricResult(madeUpMetricHeader, timeSeriesValues));
}
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project presto by prestodb.
the class TestJsonExtract method doExtract.
private static String doExtract(JsonExtractor<Slice> jsonExtractor, String json) throws IOException {
JsonFactory jsonFactory = new JsonFactory();
JsonParser jsonParser = jsonFactory.createParser(json);
// Advance to the first token
jsonParser.nextToken();
Slice extract = jsonExtractor.extract(jsonParser);
return (extract == null) ? null : extract.toStringUtf8();
}
Aggregations