edit-icon download-icon

Demo code

Last Updated: May 11, 2018

Dependent packages

Please download the dependent packages first: HTTP Client and FastJSON.

  1. <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
  2. <dependency>
  3. <groupId>org.apache.httpcomponents</groupId>
  4. <artifactId>httpclient</artifactId>
  5. <version>4.5.2</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  8. <dependency>
  9. <groupId>com.alibaba</groupId>
  10. <artifactId>fastjson</artifactId>
  11. <version>1.2.34</version>
  12. </dependency>

Demo code

  1. package org.hitsdb.client;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.ClientProtocolException;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.entity.StringEntity;
  11. import org.apache.http.impl.client.CloseableHttpClient;
  12. import org.apache.http.impl.client.HttpClients;
  13. import org.junit.Test;
  14. import com.alibaba.fastjson.JSON;
  15. public class HttpWrite {
  16. private final static String HITSDB_IP = "192.168.11.17"; //Instance address
  17. private final static int HITSDB_PORT = 8242; //Instance port
  18. private final static int SYNC_TIMEOUT_MS = 60 * 1000;
  19. private Long startTime = System.currentTimeMillis();
  20. static String putUrl = "http://" + HITSDB_IP + ":" + HITSDB_PORT +
  21. "/api/put?sync_timeout=" + SYNC_TIMEOUT_MS;
  22. static String queryUrl = "http://" + HITSDB_IP + ":" + HITSDB_PORT +
  23. "/api/query";
  24. static class DataPoint {
  25. public String metric;
  26. public Long timestamp;
  27. public Double value;
  28. public Map<String, String> tags;
  29. }
  30. String buildData() {
  31. // Construct the writing data entity
  32. List<DataPoint> dataPoints = new ArrayList<DataPoint>();
  33. for (int i = 0; i < 3600; ++i) {
  34. DataPoint dataPoint = new DataPoint();
  35. dataPoint.metric = "sys.cpu";
  36. dataPoint.timestamp = startTime + i * 1000;
  37. dataPoint.value = 32.4;
  38. dataPoint.tags = new HashMap<String, String>();
  39. dataPoint.tags.put("host", "host1");
  40. dataPoint.tags.put("appName", "test1");
  41. dataPoints.add(dataPoint);
  42. }
  43. return JSON.toJSONString(dataPoints);
  44. }
  45. @Test
  46. public void putDemo() throws ClientProtocolException, IOException {
  47. CloseableHttpClient httpClient = HttpClients.createDefault();
  48. HttpPost httpPost = new HttpPost(putUrl);
  49. StringEntity eStringEntity = new StringEntity(buildData(), "utf-8");
  50. eStringEntity.setContentType("application/json");
  51. httpPost.setEntity(eStringEntity);
  52. HttpResponse response = httpClient.execute(httpPost);
  53. int statusCode = response.getStatusLine().getStatusCode();
  54. if (statusCode == 200 || statusCode == 204) {
  55. System.out.println("write OK");
  56. }
  57. httpClient.close();
  58. }
  59. }
Thank you! We've received your feedback.