数据的转换,我的代码(垃圾)和我的师傅的代码!

我要做的事情是这样的!我要和一个C++对接!C++给传一个数据,我来进行处理,然后添加到Es中去,说白了也就是我写了一个接口!下面我们来看看我的代码!

首先C++给我的数据是这样的!

String jsonStr="

 {
    "bandWidth": {
        "actualRecvBwApplication": 0,
        "actualRecvBwAudio": 0,
        "actualRecvBwMax:": 1534,
        "actualRecvBwVideo:": 1534,
        "actualSendBwApplication:": 0,
        "actualSendBwAudio": 0,
        "actualSendBwMax": 1905,
        "actualSendBwVideo:": 1545,
        "availRecvBwMax": 100000,
        "availSendBwMax:": 1905,
        "truelyNetData": true
    },
    "localdatetime": "2017-07-10T15:19:41",
    "localtimestamp": 1499671181257,
    "orgkey": "c71badcc543e4c968275025f1896ec5f",
    "shaperInfo": {
        "delayAppPriorityNormal": 0,
        "delayAppPriorityRetransmit": 0,
        "delayVideoPriorutyRetransmit": 0,
        "delayVideoPriorytyNormal": 71,
        "numDroppedAppPriorityNormal": 0,
        "numDroppedVideoPriorytyNormal": 0,
        "numFramesAppPriorityNormal": 0,
        "numFramesVideoPriorytyNormal": 0,
        "numPacketsAppPriorityNormal": 0,
        "numPacketsAppPriorityRetransmit": 0,
        "numPacketsVideoPriorutyRetransmit": 0,
        "numPacketsVideoPriorytyNormal": 7,
        "truelyRateShaperData": true
    },
    "vedioInfo": {
        "decodedFrameRate": 29,
        "displayedFrameRate": 29,
        "height": 720,
        "receivedFrameRate": 29,
        "truelyParticipantData": true,
        "width": 1280
    }
}
";

下面是我的处理代码

@RequestMapping(value = "/postdata" , method = RequestMethod.POST)
public ResponseResult posts(
                            HttpServletRequest request,
                            String jsonStr) throws UnknownHostException {
    outputRequest(request);
    Map map1 = new LinkedHashMap<>();
    if("".equals(jsonStr)){
        return new ResponseResult(PaasExceptionDict.DataFormatError);
    }
    //System.out.println("++++++++++++++++++++++++++++++++++++++"+jsonStr);
    //解析json对象
    Map map = new HashMap();
    Gson gson = new Gson();
    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
    for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
        //System.out.println("#############"+entry.getKey()+"==== "+entry.getValue());
        if("bandWidth".equals(entry.getKey())){
            JSONObject jsonObject1 = JSONObject.parseObject(entry.getValue().toString());
            for (Map.Entry<String, Object> entry1 : jsonObject1.entrySet()) {
                map.put(entry1.getKey(),entry1.getValue());
                /*if("flase".equals(entry.getValue())){
                        return new ResponseResult(PaasExceptionDict.DataFormatError);
                }*/
            }
        }
        if("vedioInfo".equals(entry.getKey())){
            JSONObject jsonObject1 = JSONObject.parseObject(entry.getValue().toString());
            for (Map.Entry<String, Object> entry1 : jsonObject1.entrySet()) {
                map.put(entry1.getKey(),entry1.getValue());
            }
        }
        if("shaperInfo".equals(entry.getKey())){
            JSONObject jsonObject1 = JSONObject.parseObject(entry.getValue().toString());
            for (Map.Entry<String, Object> entry1 : jsonObject1.entrySet()) {
                map.put(entry1.getKey(),entry1.getValue());
            }
        }
        if("orgkey".equals(entry.getKey())){
            map.put(entry.getKey(),entry.getValue());
        }
        if("localtimestamp".equals(entry.getKey())){
            map.put(entry.getKey(),entry.getValue());
        }
        if("localDateTime".equals(entry.getKey())){
            map.put(entry.getKey(),entry.getValue());
        }
    }
    long timestamp=new Date().getTime();
    //System.out.println("时间戳是:"+timestamp);
    map.put("servertimestamp",timestamp);
    //时间戳转换成国际时间
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    String format1 = format.format(timestamp);
    map.put("serverDateTime",format1);
    String json = gson.toJson(map);
    //System.out.println("===================================== "+json);
   //添加数据
    InsertDataEs(json);
    map1.put("json", json);
    return  new ResponseResult(true,map1);
}
下面是我师傅的代码!

@RequestMapping(value = "/putdata" , method = RequestMethod.POST)
public ResponseResult posts(
        HttpServletRequest request,
        @RequestBody Map<String,Object> data
        ) throws UnknownHostException {
    outputRequest(request);
    if(data==null){
        System.out.println("data is null");
        return new ResponseResult(false,false);
    }
    Gson gson=new Gson();
    long timestamp=new Date().getTime();
    //System.out.println("时间戳是:"+timestamp);
    data.put("servertimestamp",timestamp);
    //时间戳转换成国际时间
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    String format1 = format.format(timestamp);
    data.put("serverdatetime",format1);
    String json = gson.toJson(data);
    InsertDataEs(json);
    return  new ResponseResult(true,true);
}

下面就是公共的代码!

//添加数据
public ResponseResult InsertDataEs(String json) throws UnknownHostException {
    //System.out.println(json);
    TransportClient client;
    //连接client
    System.out.println("create TransportClient...");
    client = new PreBuiltTransportClient(Settings.EMPTY)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300))
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300))
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300));

    IndexRequestBuilder indexRequestBuilder = client.prepareIndex("vdyoo", "desktop", "ID"+ IdentifierKeyHelper.getDateTimeLetterIdKey(5)).setSource(json);
    IndexResponse response = indexRequestBuilder.get();
    //关闭client
    if (client != null) {
        client.close();
        System.out.println("TransportClient close...");
    }
    return new ResponseResult(PaasExceptionDict.InsertError);
}
我写了三四天!而且他别慢!

我的师傅写了三分钟!而且特别快!

我的内心是奔溃的!

希望和我一样菜的哥们赶紧学习吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值