java代码使用Post请求向opentsdb写入数据

下面的实例是 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();
        }

    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值