Updated on 2024-08-16 GMT+08:00

Deleting OpenTSDB Data

Function Description

Add the delete parameter to the query API of OpenTSDB and set it to true. Function genQueryReq () generates a deletion request, and function deleteData() sends the deletion request to the OpenTSDB server.

Sample Code

The following code snippets are in the deleteData method of the OpentsdbExample class in the com.huawei.bigdata.opentsdb.examples packet.

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);
}