文档首页 > > 开发指南> OpenTSDB应用开发> 开发程序> 删除数据

删除数据

分享
更新时间: 2020/01/11 GMT+08:00

功能简介

在OpenTSDB的查询接口中增加delete参数,并且设置delete参数为true。函数genQueryReq()生成删除请求,函数deleteData()把删除请求发送到OpenTSDB服务端。

样例代码

以下代码片段在com.huawei.bigdata.opentsdb.examples包的"OpentsdbExample"类的deleteData方法中。

public void deleteData(String dataPoint) {
  QUERY_URL = BASE_URL + dataPoint;
  try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
    HttpPost httpPost = new HttpPost(QUERY_URL);addTimeout(httpPost);
    String deleteRequest = genDeleteReq();
    StringEntity entity = new StringEntity(deleteRequest, "utf-8");
    entity.setContentType("application/json");
    httpPost.setEntity(entity);
    HttpResponse response = httpClient.execute(httpPost);
    int statusCode = response.getStatusLine().getStatusCode();
    LOG.info("Status Code : " + statusCode);
    if (statusCode != HttpStatus.SC_OK) {
      LOG.info("Request failed! " + response.getStatusLine());
    }
  } catch (IOException e) {
    LOG.error("Failed to delete data.", e);
  }
}

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);
}
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问