Beispiel #1
0
  /*
   * 用于复杂对象转换 e.g {key1:value1,key2:{key21:value21,key22:value22,...},key3:[value31,value32,...],...}
   * @param obj 需要转化的对象
   */
  public BasicDBObject converToMGObject(Object obj) {

    String jsonStr = JsonUtil.toJson(obj);

    BasicDBObject dbObject = (BasicDBObject) JSON.parse(jsonStr);

    return dbObject;
  }
Beispiel #2
0
  /**
   * 查询记录
   *
   * <p>//and、or多条件联合条件样例 //age条件(and条件) BasicDBList condList = new BasicDBList(); BasicDBObject
   * cond = new BasicDBObject(); cond.put("$gt",0); cond.put("$lte",40); BasicDBObject composeCod =
   * new BasicDBObject(); composeCod.put("age", cond); condList.add(composeCod);
   *
   * <p>//name条件 BasicDBObject nameCond = new BasicDBObject(); nameCond.put("name", "name");
   * condList.add(nameCond);
   *
   * <p>//查询条件组合(or条件) BasicDBObject searchCond = new BasicDBObject(); searchCond.put("$or",
   * condList);
   *
   * @param nameClass
   * @param conditionDBObject
   * @param collectionName
   * @return
   */
  public <E> List<E> queryRecord(
      Class<E> nameClass, BasicDBObject conditionDBObject, String collectionName) {

    List<E> resultList = new ArrayList<E>();
    try {
      DBCollection collection = getCollection(collectionName);
      DBCursor cursor = collection.find(conditionDBObject);

      while (cursor.hasNext()) {
        E result = JsonUtil.fromJson(cursor.next().toString(), nameClass);
        resultList.add(result);
      }
    } catch (Exception e) {
      logger.error(
          "BaseMongoDAO.queryRecord查询数据时发生异常,入参collectionName="
              + collectionName
              + ";  conditionDBObject="
              + conditionDBObject
              + ";  nameClass="
              + nameClass.getName(),
          e);
    }
    return resultList;
  }
Beispiel #3
0
  /**
   * 查找数据sex1={name1};sex1={name2};sex1={name3};sex2={name1};sex2={name2};sex2={name3};sex3={name1};sex3={name2};sex3={name3};
   * String map = "function(){emit(this.sex,this.name);}";
   * 处理成sex1={name1,name2,name3};sex2={name1,name2,name3};sex3={name1,name2,name3}; String
   * reduce="function(key,values){ var ret={sex:key,names:values}; return ret;}";
   *
   * @param collectionName
   * @param mapFunction
   * @param reduceFunction
   * @param targetCollection
   * @param keyNameOfReduceValue
   */
  public void mapReduce(
      String collectionName,
      String mapFunction,
      String reduceFunction,
      String targetCollection,
      String keyNameOfReduceValue) {
    try {
      DBCollection collection = getCollection(collectionName);
      // 执行mapReduce命令
      MapReduceOutput result =
          collection.mapReduce(
              mapFunction, reduceFunction, targetCollection, OutputType.INLINE, null);
      logger.info(
          "BaseMongoDAO.mapReduce返回的原始数据值,  result=" + result.getCommandResult().toString());
      // 获取mapreduce的结果
      String reduceResult = result.getCommandResult().get("results").toString();
      logger.info("BaseMongoDAO.mapReduce返回的有效数据值,  reduceResult=" + reduceResult);

      // 返回结果key=要处理的属性;value=聚合后的结果
      Map<String, String> mapReduceResult = new HashMap<String, String>();
      // 将返回结果用json序列化格式为[{ "_id" : null , "value" : { "sex" : null , "names" : [ "test3" , "test3"
      // , "test3"]}} , { "_id" : "man" , "value" : { "sex" :
      // "man" , "names" : [ "name" , "test6" , "test6" , "test3" , "test3" , "test3" , "test3" ,
      // "test3" , "test3" , "test3" , "test3" , "test3" ,
      // "test3" , "test3" , "test3"]}}]
      List<Map<String, Object>> reduceMap = JsonUtil.fromJson(reduceResult, List.class);
      // 序列化后的串[{_id=null, value={sex=null, num=3.0}}, {_id=man, value={sex=man, num=15.0}},
      // {_id=woman, value={sex=woman, num=5.0}}, {_id=women,
      // value={name=test2, num=1.0}}]
      System.out.println(reduceMap);
      logger.info("BaseMongoDAO.mapReduce方法json反序列化后的值,  jsonToClassReduceMap=" + reduceMap);
      // 遍历mapReduce的结果
      for (Map<String, Object> mapInfo : reduceMap) {
        // 定义reduce出的key对应的collection中的值
        String keyValue = "null";
        for (Entry<String, Object> mapInfoEntry : mapInfo.entrySet()) {
          // System.out.println("111map key=====" +mapInfoEntry.getKey());
          // 获取key对应的collection中的值
          if (mapInfoEntry.getKey().equals("_id")) {
            keyValue = processNull(mapInfoEntry.getValue());
          }
          /**
           * 这个判断主要是为了集中处理value={sex=man, num=15.0}这类键值
           * 绕开对key=NULL(即value为NULL)这类键值对的处理;例如{_id=null, value={sex=null,
           * num=3.0}}中的_id和sex;如果为空则不做任何处理 因为下面使用的是MAP的强转,不适宜基础类型
           */
          // 如果reduce的值不为空,而且不是基础类型则继续解析map
          if (mapInfoEntry.getValue() != null
              && !isPrimitiveType(mapInfoEntry.getValue().getClass().getName())) {
            // System.out.println("in  in 111map value=====" +mapInfoEntry.getValue());
            LinkedHashMap<Object, Object> realInfoMapList =
                (LinkedHashMap<Object, Object>) mapInfoEntry.getValue();
            for (Entry e : realInfoMapList.entrySet()) {
              if (keyNameOfReduceValue.equals(e.getKey().toString())) {
                mapReduceResult.put(keyValue, e.getValue().toString());
              }
              // System.out.println(e.getKey());
              // System.out.println(e.getValue());
            }
            // 值不为NULL的基础类型的处理逻辑
          } else if (mapInfoEntry.getValue() != null) {
            if (keyNameOfReduceValue.equals(mapInfoEntry.getKey().toString())) {
              mapReduceResult.put(keyValue, mapInfoEntry.getValue().toString());
            }
            // 值为NULL的处理,只打印日志
          } else {
            logger.error("BaseMongoDAO.mapReduce方法,查到的结果为NULL,不做任何处理" + mapInfoEntry.getValue());
          }
        }
      }

      // 输出reduce处理完成后的结果集
      for (Entry<String, String> mapInfo : mapReduceResult.entrySet()) {
        logger.info(
            "BaseMongoDAO.mapReduce方法聚合后的结果key="
                + mapInfo.getKey()
                + "======"
                + mapInfo.getValue());
      }
    } catch (Exception e) {
      logger.error(
          "BaseMongoDAO.mapReduce聚合数据时发生异常,入参collectionName="
              + collectionName
              + ";  mapFunction="
              + mapFunction
              + ";  reduceFunction="
              + reduceFunction
              + ";  targetCollection="
              + targetCollection
              + ";  keyNameOfReduceValue="
              + keyNameOfReduceValue,
          e);
    }
  }