use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken 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.JsonToken in project presto by prestodb.
the class JsonFunctions method jsonArrayContains.
@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.BIGINT) long value) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
if (parser.nextToken() != START_ARRAY) {
return null;
}
while (true) {
JsonToken token = parser.nextToken();
if (token == null) {
return null;
}
if (token == END_ARRAY) {
return false;
}
parser.skipChildren();
if ((token == VALUE_NUMBER_INT) && ((parser.getNumberType() == NumberType.INT) || (parser.getNumberType() == NumberType.LONG)) && (parser.getLongValue() == value)) {
return true;
}
}
} catch (IOException e) {
return null;
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project presto by prestodb.
the class JsonFunctions method jsonArrayContains.
@SqlNullable
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType("varchar(x)") Slice value) {
String valueString = value.toStringUtf8();
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
if (parser.nextToken() != START_ARRAY) {
return null;
}
while (true) {
JsonToken token = parser.nextToken();
if (token == null) {
return null;
}
if (token == END_ARRAY) {
return false;
}
parser.skipChildren();
if (token == VALUE_STRING && valueString.equals(parser.getValueAsString())) {
return true;
}
}
} catch (IOException e) {
return null;
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project presto by prestodb.
the class JsonFunctions method jsonArrayLength.
@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BIGINT)
public static Long jsonArrayLength(@SqlType(StandardTypes.JSON) Slice json) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
if (parser.nextToken() != START_ARRAY) {
return null;
}
long length = 0;
while (true) {
JsonToken token = parser.nextToken();
if (token == null) {
return null;
}
if (token == END_ARRAY) {
return length;
}
parser.skipChildren();
length++;
}
} catch (IOException e) {
return null;
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project presto by prestodb.
the class JsonFunctions method jsonArrayContains.
@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.BOOLEAN) boolean value) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
if (parser.nextToken() != START_ARRAY) {
return null;
}
while (true) {
JsonToken token = parser.nextToken();
if (token == null) {
return null;
}
if (token == END_ARRAY) {
return false;
}
parser.skipChildren();
if (((token == VALUE_TRUE) && value) || ((token == VALUE_FALSE) && (!value))) {
return true;
}
}
} catch (IOException e) {
return null;
}
}
Aggregations