Improve JSON serialization for the http requests to Druid
@@ -23,6 +23,8 @@ import com.facebook.presto.druid.metadata.DruidSegmentIdWrapper;
import com.facebook.presto.druid.metadata.DruidSegmentInfo;
import com.facebook.presto.druid.metadata.DruidTableInfo;
import com.facebook.presto.spi.PrestoException;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import javax.inject.Inject;
@@ -137,7 +139,7 @@ public class DruidClient
private static byte[] createRequestBody(String query, DruidResultFormat resultFormat, boolean queryHeader)
{
- return format("{\"query\":\"%s\",\"resultFormat\":\"%s\",\"header\": %b }\n", query, resultFormat.getResultFormat(), queryHeader).getBytes();
+ return new DruidRequestBody(query, resultFormat.getResultFormat(), queryHeader).toJson().getBytes();
}
private Request prepareMetadataQuery(String query)
@@ -183,4 +185,44 @@ public class DruidClient
}
}
}
+ public static class DruidRequestBody
+ {
+ private String query;
+ private String resultFormat;
+ private boolean queryHeader;
+ @JsonCreator
+ public DruidRequestBody(
+ @JsonProperty("query") String query,
+ @JsonProperty("resultFormat") String resultFormat,
+ @JsonProperty("queryHeader") boolean queryHeader)
+ {
+ requireNonNull(query);
+ this.query = query;
+ this.resultFormat = resultFormat;
+ this.queryHeader = queryHeader;
+ }
+
+ @JsonProperty("query")
+ public String getQuery()
+ {
+ return query;
+ }
+
+ @JsonProperty("resultFormat")
+ public String getResultFormat()
+ {
+ return resultFormat;
+ }
+
+ @JsonProperty("queryHeader")
+ public boolean isQueryHeader()
+ {
+ return queryHeader;
+ }
+
+ public String toJson()
+ {
+ return JsonCodec.jsonCodec(DruidRequestBody.class).toJson(this);
+ }
+ }
}
