下面的实例是 java使用POST请求向opentsdb读条写入数据:
package com.streamsets.pipeline.stage.origin.yofc;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class httpSendPost {
public static String sendPost(String url,String string) throws IOException {
String result = null;
HttpPost httpPost = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(string,"utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse resp = httpClient.execute(httpPost);
System.out.println(resp);
HttpEntity he =resp.getEntity();
result = EntityUtils.toString(he,"UTF-8");
return result;
}
public static void main(String[] args) {
String string = "{\"metric\":\"test\",\"value\":\"10.0\",\"timestamp\":1523415635034,\"tags\":{\"fid\":\"1\",\"td\":\"test1\"}}";
try {
String str = sendPost("http://192.168.197.128:4242/api/put?details",string);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
下面的实例是 java使用POST请求向opentsdb中批量写入数据
package com.streamsets.pipeline.stage.origin.yofc;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class httpSendPost {
public static String sendPost(String url,List<Object> list) throws IOException {
String result = null;
HttpPost httpPost = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
String string = JSON.toJSONString(list);
StringEntity entity = new StringEntity(string,"utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse resp = httpClient.execute(httpPost);
System.out.println(resp);
HttpEntity he =resp.getEntity();
result = EntityUtils.toString(he,"UTF-8");
return result;
}
public static List<Object> setList(){
Map<String,Object> map =new HashMap<String,Object>();
Map<String,Object> map1 = new HashMap<>();
map.put("metric","machine.test");
map.put("timestamp",Long.valueOf("1522138004895"));
map.put("value",0);
map1.put("LineName","S16");
map1.put("BoardName","G-97RG3_15K-00007-AM2(1.8)_HDC000449-TOP");
map1.put("BarCode","081820403239");
map1.put("Division","2");
map1.put("Tower","0");
map1.put("Level","0");
map1.put("ComponentName","012-10121-24");
map.put("tags",map1);
Map<String,Object> map_1 =new HashMap<String,Object>();
Map<String,Object> map1_1 = new HashMap<>();
map_1.put("metric","machine.test");
map_1.put("timestamp",Long.valueOf("1522138004895"));
map_1.put("value",0);
map1_1.put("LineName","S16");
map1_1.put("BoardName","G-97RG3_15K-00007-AM2(1.8)_HDC000449-TOP");
map1_1.put("BarCode","081820403239");
map1_1.put("Division","2");
map1_1.put("Tower","0");
map1_1.put("Level","0");
map1_1.put("ComponentName","012-10121-24");
map_1.put("tags",map1_1);
Map<String,Object> map_2 =new HashMap<String,Object>();
Map<String,Object> map1_2 = new HashMap<>();
map_2.put("metric","machine.test");
map_2.put("timestamp",Long.valueOf("1522138004895"));
map_2.put("value",0);
map1_2.put("LineName","S16");
map1_2.put("BoardName","G-97RG3_15K-00007-AM2(1.8)_HDC000449-TOP");
map1_2.put("BarCode","081820403239");
map1_2.put("Division","2");
map1_2.put("Tower","0");
map1_2.put("Level","0");
map1_2.put("ComponentName","012-10121-24");
map_2.put("tags",map1_2);
List<Object> list = new ArrayList<>();
list.add(map);
list.add(map_1);
list.add(map_2);
return list;
}
public static void main(String[] args) {
List<Object> list = setList();
try {
String str = sendPost("http://192.168.197.128:4242/api/put?details",list);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}