zoukankan      html  css  js  c++  java
  • 测试环境和线上环境数据的传输

    不bb,直接上代码:

    /**
    * 传输数据
    * @param libraryId
    */
    @RequestMapping(value = "/**transfer****", method = RequestMethod.GET)
    @ResponseBody
    public Object transfer(@RequestParam(value = "libraryId", defaultValue = "") Integer libraryId,
    @RequestParam(value = "collection", defaultValue = "") String collection,
    @RequestParam(value = "month", defaultValue = "") Integer month,
    @RequestParam(value = "endPage", defaultValue = "") Integer endPage) {
    Map<String,Object> map = Maps.newHashMap();
    Map<String, String> message = Maps.newHashMap();
    // 取出数据
    int page = 1;
    int pageSize = 10;
    while (true) {
    // 返回的数据
    String result = WebUtil.get("http://***************/ex?libraryId=" + libraryId
    + "&collection=" + collection + "&month=" + month + "&page=" + page + "&pageSize=" + pageSize);
    logger.info("开始传输第"+page+"页数据");
    if (StringUtils.isEmpty(result) || result.equals("[]")) {
    logger.info("传输数据为空,传输完毕");
    break;
    }
    // 导入数据
    message.put("data", result);
    WebUtil.post("http://************/import*****", message);
    if (endPage != null && page == endPage) {
    break;
    }
    page++;
    }
    map.put("总页数", page);
    map.put("总条数", page*pageSize);
    map.put("message", "传输完毕");
    return map;
    }

    /**
    * 提取数据接口
    * @param request
    * @param response
    * @param model
    * @param libraryId
    * @param collection
    * @param page
    * @param pageSize
    * @param month
    * @param year
    * @return
    */
    @RequestMapping(value = "/extraction", method = RequestMethod.GET)
    @ResponseBody
    public List<Document> getDocumentPageBylibraryId(HttpServletRequest request, HttpServletResponse response,
    Model model, @RequestParam(value = "libraryId", defaultValue = "") Integer libraryId,
    @RequestParam(value = "collection", defaultValue = "") String collection,
    @RequestParam(value = "page", defaultValue = "1") Integer page,
    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
    @RequestParam(value = "month", defaultValue = "") Integer month,
    @RequestParam(value = "year", defaultValue = "2017") Integer year) {
    if (month == null || month < 0 || month >12) {
    return null;
    }
    if (year < 2000 || year > 2020) {
    return null;
    }
    logger.info("传输参数:libraryId:{},collection:{},month:{}",libraryId,collection,month);
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    List<Document> docs;
    try {
    if (month == 0) {
    docs = StringUtils.isEmpty(collection) ? materialService.getMaterialByLibraryId(libraryId, page, pageSize)
    : materialService.getMaterialByCollectionAndLibraryIdAndTime(collection, libraryId, page, pageSize, null, null);
    return docs;
    }
    Date startTime;
    Date endTime;
    if (month == 12) {
    startTime = dateFormat.parse(year+"-12-01");
    endTime = dateFormat.parse(year+1+"-01-01");
    } else {
    startTime = dateFormat.parse(year+"-"+month+"-01");
    endTime = dateFormat.parse(year+"-"+(month+1)+"-01");
    }

    //从mongodb中搜索时间,此处有一个小坑
    docs = materialService.getMaterialByCollectionAndLibraryIdAndTime(collection,libraryId, page, pageSize,startTime,endTime); 
    return docs;
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return null;
    }

    /**
    * 导入接口 addMaterialData
    * @param data
    */
    @RequestMapping(value = "/**import****", method = RequestMethod.POST)
    public void addMaterialData(@RequestParam(value = "data", defaultValue = "") String data) {
    if (StringUtils.isEmpty(data)) {
    return;
    }
    // 反序列化
    List<Document> docs = JsonUtil.strToDocumentList(data);

    for (Document doc : docs) {

    try {
    doc.remove("_id");
    doc.put("library_id", (int) Double.parseDouble(String.valueOf(doc.get("library_id"))));  //获取到的数据 数字需要转换一下
    doc.put("fetch_time", new Date(doc.getDouble("fetch_time").longValue()));  //时间格式转换mongodb支持的
    doc.put("pub_date", new Date(doc.getDouble("pub_date").longValue()));
    materialService.importData(doc);
    } catch (NumberFormatException e) {
    e.printStackTrace();
    }
    }

    }

    注:mongodb命令根据时间搜索数据的时候,时间采用的是UTC标准时间,所以搜索的时候要减去8个小时!

      之前测试没注意一直返回的数据和实际的不一样,细节决定成败,以后多看看文档!

  • 相关阅读:
    Servlet编程实例1
    Servlet
    JDBC之代码优化
    JDBC数据库编程
    数据库常识
    数据库基本操作
    STM32CUBEMX入门学习笔记3:HAL库以及STM32CUBE相关资料
    QT入门学习笔记2:QT例程
    爬虫制作入门学习笔记2:[转]python爬虫实例项目大全
    中移物联网onenet入门学习笔记2:中移物联的通信格式
  • 原文地址:https://www.cnblogs.com/yzf666/p/7150969.html
Copyright © 2011-2022 走看看