Writing Data
Function Description
You can use OpenTSDB APIs to write data.
Function genWeatherData () simulates the generated weather data. The function put () sends weather data to the OpenTSDB server.
Sample Code
private static String PUT_URL = (securityMode ? "https://" : "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);
// A timeout interval needs to be set for the request.
addTimeout(httpPost);
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());
}
}
}
The sync parameter is added to PUT_URL, indicating that data can be returned only after data is written to HBase. This parameter is strongly recommended. If sync is not used, data is asynchronously written to HBase, which may cause data loss. For details, see OpenTSDB API Introduction.
Last Article: Performing IAM Authentication for Clusters
Next Article: Querying Data
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.