更新时间:2021-03-18 GMT+08:00
删除数据
功能简介
在OpenTSDB的查询接口中增加delete参数,并且设置delete参数为true。函数genQueryReq()生成删除请求,函数delete()把删除请求发送到OpenTSDB服务端。
样例代码
private static String QUERY_URL = (securityMode ? "https://" : "http://") + OPENTSDB_IP + ":" + OPENTSDB_PORT + "/api/query"; static class Query { public Long start; public Long end; public boolean delete = false; public List<SubQuery> queries; } static class SubQuery { public String metric; public String aggregator; public SubQuery(String metric, String aggregator) { this.metric = metric; this.aggregator = aggregator; } } String genQueryReq() { Query query = new Query(); query.start = 1498838400L; query.end = 1498921200L; query.queries = ImmutableList.of(new SubQuery("city.temp", "sum"), new SubQuery("city.hum", "sum")); Gson gson = new Gson(); return gson.toJson(query); } String genDeleteReq() { Query query = new Query(); query.start = 1498838400L; query.end = 1498921200L; query.queries = ImmutableList.of(new SubQuery("city.temp", "sum"), new SubQuery("city.hum", "sum")); query.delete = true; Gson gson = new Gson(); return gson.toJson(query); } public void delete() throws ClientProtocolException, IOException { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(QUERY_URL); // 请求需要设置超时时间 addTimeout(httpPost); String deleteRequest = genDeleteReq(); StringEntity entity = new StringEntity(deleteRequest, "ISO-8859-1"); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); System.out.println("Status Code : " + statusCode); if (statusCode != HttpStatus.SC_OK) { System.out.println("Request failed! " + response.getStatusLine()); } } }
父主题: 样例代码说明
