OpenTSDB使用总结-(1)

举报
ChillRay 发表于 2020/12/29 23:04:05 2020/12/29
【摘要】 样例代码 写入数据查询数据删除数据 样例代码 写入数据 功能简介 使用OpenTSDB的接口写入数据。函数genWeatherData()模拟生成的气象数据,函数put()发送气象数据到OpenTSDB服务端。 样例代码 private static String PUT_URL = "http://" + OPENTSDB_IP...

样例代码

写入数据

功能简介

使用OpenTSDB的接口写入数据。函数genWeatherData()模拟生成的气象数据,函数put()发送气象数据到OpenTSDB服务端。

样例代码

private static String PUT_URL = "http://" + OPENTSDB_IP + ":" + OPENTSDB_PORT + "/api/put/?sync&sync_timeout=60000"; 

static class DataPoint { public String metric; public Long timestamp; public Double value; public Map<String, String> tags; public DataPoint(String metric, Long timestamp, Double value, Map<String, String> tags) { this.metric = metric; this.timestamp = timestamp; this.value = value; this.tags = tags; }

}

private String genWeatherData() { List<DataPoint> dataPoints = new ArrayList<DataPoint>(); Map<String, String> tags = ImmutableMap.of("city", "Shenzhen", "region", "Longgang"); // Data of air temperature dataPoints.add(new DataPoint("city.temp", 1498838400L, 28.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498842000L, 27.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498845600L, 27.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498849200L, 27.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498852800L, 27.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498856400L, 27.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498860000L, 27.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498863600L, 27.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498867200L, 29.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498870800L, 30.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498874400L, 32.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498878000L, 32.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498881600L, 33.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498885200L, 33.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498888800L, 32.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498892400L, 32.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498896000L, 31.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498899600L, 30.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498903200L, 30.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498906800L, 29.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498910400L, 29.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498914000L, 29.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498917600L, 28.0, tags)); dataPoints.add(new DataPoint("city.temp", 1498921200L, 28.0, tags)); // Data of humidity dataPoints.add(new DataPoint("city.hum", 1498838400L, 54.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498842000L, 53.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498845600L, 52.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498849200L, 51.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498852800L, 50.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498856400L, 49.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498860000L, 48.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498863600L, 46.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498867200L, 46.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498870800L, 48.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498874400L, 48.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498878000L, 49.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498881600L, 49.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498885200L, 50.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498888800L, 50.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498892400L, 50.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498896000L, 51.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498899600L, 51.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498903200L, 51.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498906800L, 51.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498910400L, 52.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498914000L, 53.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498917600L, 54.0, tags)); dataPoints.add(new DataPoint("city.hum", 1498921200L, 54.0, tags)); Gson gson = new Gson(); return gson.toJson(dataPoints);

}

public void put() throws ClientProtocolException, IOException { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(PUT_URL); String weatherData = genWeatherData(); StringEntity entity = new StringEntity(weatherData, "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_NO_CONTENT) { System.out.println("Request failed! " + response.getStatusLine()); } }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164

查询数据

功能简介

使用OpenTSDB的查询接口读取数据.函数genQueryReq()生成查询请求,函数query()把查询请求发送到OpenTSDB服务端。

样例代码

private static String QUERY_URL = "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);
}

public void query() throws ClientProtocolException, IOException {
  try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(QUERY_URL); String queryRequest = genQueryReq(); //System.out.println("Request=" + queryRequest); StringEntity entity = new StringEntity(queryRequest, "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());
}

String body = EntityUtils.toString(response.getEntity(), "ISO-8859-1");
System.out.println("Response content : " + body); }
}
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

删除数据

功能简介

使用OpenTSDB的查询接口读取数据,但是要加上delete参数,并且设置为true。函数genQueryReq()生成查询请求,函数query()把查询请求发送到OpenTSDB服务端

样例代码

private static String QUERY_URL = "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); 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());
} }
}
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

文章来源: zclhit.blog.csdn.net,作者:zclhit_,版权归原作者所有,如需转载,请联系作者。

原文链接:zclhit.blog.csdn.net/article/details/80682550

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。