`
ld362093642
  • 浏览: 66655 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Java读取操作MONGODB数据

 
阅读更多
Java对MONGODB数据基础操作
http://blog.csdn.net/mcpang/article/details/8730849

http://www.cnblogs.com/hoojo/archive/2011/06/02/2068665.html


DocInfo.java

package models;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import play.data.validation.Min;
import play.data.validation.Required;

import annotation.BeanField;
import annotation.DateWithTime;
import annotation.NumList;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.util.JSON;

import net.sf.json.JSONObject;
import util.Constants;
import util.DBUtil;
import util.DateUtil;
import util.DocUtil;
import util.HttpHelper;
import util.SqlHelper;
import util.UtilValidate;
import util.XString;

/**
 * 文章信息数据操作类
 * @author rainyhao haojz@golaxy.cn
 * @since 2014-1-7 上午9:58:14
 */
public class DocInfo implements Serializable{
	public Long _id;//文章ID,CT生成
    public Double score;//得分
    public String u;//URL
    public Integer ch;//通道类型
    public String a;//摘要
    public String pt;//发布时间
    transient public Integer intPt;//int类型的发布时间,用于三通道融合检索
    public String gt;//采集时间
    public String au;//作者
    public Integer si;//网站id
    public String sa;//网站地域信息
    public String sn;//网站名称
    public Tag[] tag;//标签
    public String cf;//内容指纹
    public String src;//来源
    public Integer rpc;//转载数
    public Integer rc;//评论\回复数
    public Integer mc;//暂时不使用
    public Integer sc;//重要度/热度
    public String[] bct;//板块类别标签
    public String[]	pic;//图片列表
    public String[]	video;//视频列表
    public String t;//标题
    public Integer bi;//版块id
    public String bn;//板块名称
    public String k;//关键词
    public String kv;//关键词向量
    public String pl;//人名列表
    public String rl;//地名列表
    public String ol;//机构名列表
    public String md5;//网页MD5
    public String pf;//分页指纹(根据采集内容填写)
    public Integer rpf;//转载标识
    public Integer cc;//点击数
    public Integer ish;//是否网站首页文章
    public Integer nn;//负面指数
    public Integer irp;//是否为回复消息标志,1:是
    public String mid;//消息id
    public String ui;//微博作者ID
    public String un;//微博作者的用户名
    public String iu;//用户头像地址
    public String audio;//音频url
    public String[]	nc;//微博内出现的 @名字,格式:[“@lisi"]
    public String rpi;//被转发消息id
    public String ri;//被转发消息源头id
    public String ru;//转发用户ID(ri用户消息的作者)
    public String rpm;//被回复ID
    public String rpu;//被回复人ID
    public String rps;//被回复人昵称
    public String srh;//微博来源链接
    public Integer cl;//内容长度
    public Integer fn;//帖子楼层
    public String ct;//正文
    // 非mongo字段的辅助属性
    public Long eid;// 知识库中专家库的id
	
    
    /**
     * 获得一个任务的最新文章列表
     * 
     * @param ti
     *            task_id 任务ID(必选)
     * @param ps
     *            page size 单页返回记录数 可选(默认为50)
     * @param p
     *            page 返回结果的页码 可选(默认为1)
     * @param sb
     *            排序字段
     * @param so
     *            排序顺序, 1升序
     * @param tp 任务类型 t话题 c内容分类
     */
    public static List<DocInfo> getNewDocList(Long ti, Integer cis, Integer ps,
            Integer p,Integer rpf, String sb, Integer so) {
    	HashMap<String, Object> condition = new HashMap<String, Object>();
    	// 默认为0或空表示全部通道
        if (!UtilValidate.areEqual("0", cis) && UtilValidate.isNotEmpty(cis)) {
            condition.put("ch", cis+"");
        }
        condition.put("tag", getTaskType() + ti);
        condition.put("tt", 2); // 1,gt采集时间,2,pt发布时间
        condition.put("ps", ps);
        condition.put("p", p);
        if(sb==null||"".equals(sb)){
        	sb="pt";
        }
        condition.put("sb", sb);
        if(so==null||"".equals(so)){
        	so=-1;
        }
        condition.put("so", so);
        if(rpf!=null&&rpf==1){
        	condition.put("rpf", 1);
        }
        List<DocInfo> docInfoList = DocUtil.getNew(condition);
        return docInfoList;
    }
    
    /**
     * 获得一个任务的最新文章列表数量
     * 
     * @param ti task_id 任务ID(必选)
     * @param ps page size 单页返回记录数 可选(默认为50)
     * @param p  page 返回结果的页码 可选(默认为1)
     * @param sb 排序字段
     * @param so 排序顺序, 1升序
     */
    public static Long getNewDocCount(Long ti, Integer cis, Integer ps,
            Integer p,Integer rpf) {
    	HashMap<String, Object> condition = new HashMap<String, Object>();
    	// 默认为0或空表示全部通道
        if (!UtilValidate.areEqual("0", cis) && UtilValidate.isNotEmpty(cis)) {
            condition.put("ch", cis+"");
        }
        condition.put("tag", getTaskType() + ti);
        condition.put("tt", 2); // 1,gt采集时间,2,pt发布时间
        condition.put("ps", ps);
        condition.put("p", p);
        
        if(rpf!=null&&rpf==1){
        	condition.put("rpf", 1);
        }
        long count=0l;
        count = DocUtil.getCount(condition);
        return count;
    }
    
    /**
     * 根据taskId取得任务类型
     * 
     * @param taskId
     * @return
     */
    private static String getTaskType() {
    	return "r";
    }
    
    /**
	 * 根据一篇文章ID获得其正文
	 * @param did 文章id
	 */
	public static String getContent(Long did) {
		DB db = DBUtil.getDB(Constants.MONGO_DB_DOCTEXT);
		DBCollection doctextCollection = db
				.getCollection(Constants.MONGO_DOCTEXT);
		// 查询条件
		BasicDBObject queryObject = new BasicDBObject();
		queryObject.append("_id", did);
		// 返回字段
		BasicDBObject fieldObject = new BasicDBObject();
		fieldObject.put("content", 1);
		DBObject docObject = doctextCollection
				.findOne(queryObject, fieldObject);
		String returnField = "";
		if (docObject != null) {
			returnField = (String) docObject.get("content");
		}
		return returnField;
	}
    
    /**
	 * 获得一篇文章的元信息
	 * 
	 * @param did 文章id
	 * @param ti 任务id,关键词标红用
	 */
	public static DocInfo getDetail(Long did) {
		if(did==null){
			return new DocInfo();
		}
		
		DB db = DBUtil.getDB(Constants.MONGO_DB_DOCINFO);
		DBCollection collection = db.getCollection(Constants.MONGO_DOCINFO);
		BasicDBObject queryObject = new BasicDBObject();
		if(did!=null){
			queryObject.put("_id", did);
		}
		
		DBObject docObject = collection.findOne(queryObject);
		DocInfo docInfo = new DocInfo();
		if (docObject != null) {
			docInfo=DocUtil.assignDocinfo(docObject);
			DB doctextDb = DBUtil.getDB(Constants.MONGO_DB_DOCTEXT);
			DBCollection ctCollection = doctextDb
					.getCollection(Constants.MONGO_DOCTEXT);
			DBObject ctObject = ctCollection.findOne(new BasicDBObject()
					.append("_id", docInfo._id));
			if (ctObject != null) {
				
				docInfo.ct = ctObject.get("content") == null ? ""
							: (String) ctObject.get("content");// 标题
			} else {
				docInfo.ct = "";
			}
		}
		return docInfo;
	}
	
	//循环从MONGODB获取文章详细信息以及文章正文
	public static List<JSONObject> getContents(List<JSONObject> docs) {
		List<JSONObject> docList = new ArrayList<JSONObject>();
		int arraynum = 0;
		for (JSONObject doc : docs) {
			String did = doc.getString("did");
			DocInfo info = getDetail(Long.valueOf(did));
			arraynum++;
			doc.put("arraynum", arraynum);
			doc.put("md5", info.cf==null?"":info.cf);
			doc.put("fulltext", info.ct==null?"":info.ct);
			doc.put("createDate", info.pt==null?"":info.pt);
			doc.put("summary", info.t==null?"":info.t);
			doc.put("creator", info.au==null?"":info.au);
			doc.put("url", info.u==null?"":info.u);
			doc.put("reg", info.sa==null?"":info.sa);
			doc.put("src", info.sn==null?"":info.sn);
			docList.add(doc);
		}
		return docList;
	}
	
	/**
	 * <p>当天非针对话题ID的最新热门Top100信息报文(注:现有系统热门推荐热点文章功能)</p>
	 * <p>返回</p>
	 * @author Lingdong
	 * @since  2014-3-4 下午03:11:18
	 * @param  mininum 数据起始位
	 * @param  maxinum 数据终止位
	 * @param  galleryid 通道id(1新闻、2论坛、3博客)
	 * @return
	 */
	public static List<JSONObject> getHotDocList(Integer mininum, Integer maxinum, Integer galleryid){
		StringBuffer sql = new StringBuffer("select distinct a.doc_id as did, a.hot_degree as hd, a.keywords as kw ");
		sql.append(" from yqpt_hot_doc_today a  ");
		sql.append(" where a.channel_id =" + galleryid + " ");
		sql.append(" and a.publish_time >= '" + XString.formatDate(new Date(), "yyyy-MM-dd") + " 00:00:00'");
		sql.append(" order by a.publish_time desc ");
		sql.append(" limit " + mininum + "," + maxinum +" ");
		return SqlHelper.queryForJSONList(sql.toString());
	}
	
}


DocUtil.java
package util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import models.DocInfo;
import models.Tag;
import play.Logger;

import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * @author 
 */
public class DocUtil {
	
    /**
     * 根据筛选条件返回文章数量
     * 
     * @param condition
     * @return int
     */
    public static long getCount(HashMap<String, Object> condition) {
    	//单独处理按uis和sis查询的情况
    	if (condition.get("uis")!=null&&condition.get("sis")!=null) {
    		long count=getCountWith$or(condition);
    		return count;
        }
    	long start=System.currentTimeMillis();
        long count = 0l;
        DB db = DBUtil.getDB(Constants.MONGO_DB_DOCINFO);
        DBCollection collection = db.getCollection(Constants.MONGO_DOCINFO);
        long middle=System.currentTimeMillis();
        BasicDBObject queryObject = generateQuery(condition);
        count=collection.find(queryObject).count();
        long end=System.currentTimeMillis();
        Logger.info(queryObject+"-count cost:"+(middle-start)+"+"+(end-middle)+"="+(end-start)+"ms");
        return count;
    }
    
    /**
     * 单独处理按uis、sis查询的情况
     * @param condition
     * @return List<DocInfo>
     */
    private static long getCountWith$or(HashMap<String, Object> condition){
    	long start=System.currentTimeMillis();
        DB db = DBUtil.getDB(Constants.MONGO_DB_DOCINFO);
        DBCollection collection = db.getCollection(Constants.MONGO_DOCINFO);
        long middle=System.currentTimeMillis();
        BasicDBObject queryObject = generateQuery(condition);
        //第一次过滤
        //拼装si、ui条件
        Object sisObject = condition.get("sis");
        Object uisObject = condition.get("uis");
        if (sisObject!=null&&uisObject!=null) {
            String sis = (String) sisObject;
            String uis = (String) uisObject;
            if (!sis.equals("")&&!uis.equals("")) {
                String[] sisArray = sis.split(",");
                int sisLength = sisArray.length;
                Long[] siArray = new Long[sisLength];
                for (int i = 0; i < sisLength; i++) {
                    siArray[i] = Long.parseLong(sisArray[i]);
                }
                String[] uisArray = uis.split(",");
                int uis_length = uisArray.length;
                if(sisLength==uis_length){
                	BasicDBList basicDBList = new BasicDBList();
                	for(int i=0;i<sisLength;i++){
                		BasicDBObject query = new BasicDBObject().append("si",siArray[i]).append("ui",uisArray[i]);
                		basicDBList.add(query);
                	}
                	queryObject.append("$or",basicDBList);
                }
            }
        }
        // 拼装按时间(pt)过滤条件
        String st = String.valueOf(condition.get("st"));
        String et = String.valueOf(condition.get("et"));
        boolean flag = false;// st和et至少传一个,才会拼装时间过滤条件
        BasicDBObject basicDBObject = new BasicDBObject();
        try {
            if (!st.equals("") && !st.equals("null")) {
                Date stTime = DateUtil.string2UtilDate(st,
                        "yyyy-MM-dd HH:mm:ss");
                basicDBObject.append("$gte", stTime.getTime() / 1000);
                flag = true;
            }
            if (!et.equals("") && !et.equals("null")) {
                Date etTime = DateUtil.string2UtilDate(et,
                        "yyyy-MM-dd HH:mm:ss");
                basicDBObject.append("$lte", etTime.getTime() / 1000);
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (flag) {
            queryObject.append("pt", basicDBObject);
        }
        //拼装需要排除的文章id条件
        if(condition.containsKey("nids")){
        	Object nidsObject = condition.get("nids");
            if(nidsObject!=null){
                String nids=(String)nidsObject;
                if(!nids.equals("")){
                	String[] nidArray=nids.split(",");
                	Long[] idArray=new Long[nidArray.length];
                	for(int i=0;i<nidArray.length;i++){
                		idArray[i]=Long.parseLong(nidArray[i]);
                	}
                    queryObject.append("_id",new BasicDBObject().append("$nin",idArray));
                }
            }
        }
        BasicDBObject match=new BasicDBObject();
        match.append("$match",queryObject);
        BasicDBObject group=new BasicDBObject();
        group.append("$group", new BasicDBObject().append("_id","$ch").append("ct",new BasicDBObject().append("$sum",1)));
        AggregationOutput aggregate = collection.aggregate(match,group);
        CommandResult commandResult = aggregate.getCommandResult();
        long result=0L;
        double status=(Double)commandResult.get("ok");
        if(status==1){
        	BasicDBList dbList =(BasicDBList)commandResult.get("result");
        	if(dbList.size()>0){
        		result=((BasicDBObject)dbList.get(0)).getLong("ct");
        	}
        }
        long end=System.currentTimeMillis();
        Logger.info(queryObject+"-count cost:"+(middle-start)+"+"+(end-middle)+"="+(end-start)+"ms");
        return result;
    }
    
    /**
     * 根据筛选条件、排序条件、翻页条件、返回字段条件,返回文章列表
     * 
     * @param condition
     * @return List<DocInfo>
     */
    public static List<DocInfo> getNew(HashMap<String, Object> condition) {
    	//单独处理按uis和sis查询的情况
    	if (condition.get("uis")!=null&&condition.get("sis")!=null) {
    		List<DocInfo> result=getNewWith$or(condition);
    		return result;
        }
    	long start=System.currentTimeMillis();
        List<DocInfo> result = new ArrayList<DocInfo>();
        DB db = DBUtil.getDB(Constants.MONGO_DB_DOCINFO);
        DBCollection collection = db.getCollection(Constants.MONGO_DOCINFO);
        long middle=System.currentTimeMillis();
        BasicDBObject queryObject = generateQuery(condition);
        DBCursor dbCursor = null;
        //分页参数,默认为第1页,每页50条
        int p = 1, ps = 50;
        if (condition.containsKey("p")) {
            Object pObject = condition.get("p");
            if (pObject != null) {
                p = (Integer) pObject;
            }
        }
        if (condition.containsKey("ps")) {
            Object psObject = condition.get("ps");
            if (psObject != null) {
                ps = (Integer) psObject;
            }
        }
        int skipCount=0;
        if(condition.containsKey("start")){
        	 Object skipCountObject = condition.get("start");
             if (skipCountObject != null) {
            	 skipCount = (Integer)skipCountObject-1;
             }
        }else{
        	skipCount=(p-1)*ps;
        }
        if(p>0){
        	p = p-1;
        }
        //排序参数,默认为按pt降序(1升序,-1降序,默认降序)
        String sb = "pt";
        int so = -1;
        if (condition.containsKey("sb")) {
            Object theSb = condition.get("sb");
            if (theSb != null) {
                sb = String.valueOf(theSb).trim();
            }
        }
        if (condition.containsKey("so")) {
            Object theSo = condition.get("so");
            if (theSo != null) {
                so = (Integer) theSo;
                if(so!=1){
                	so=-1;
                }
            }
        }
        //排序条件
        BasicDBObject sortDBObject=new BasicDBObject();
        if(condition.containsKey("tag")&&"v".equals(condition.get("sb"))){
        	sortDBObject.append("tag.t",1).append("tag.v",-1);//按tag.v排序的特殊处理
        }else{
        	sortDBObject.append(sb, so);
        }
        Object flsObject=condition.get("fls");
        if (flsObject!=null&&!("".equals((String)flsObject))) {//返回指定字段
                String fls = (String) condition.get("fls");
                    BasicDBObject fieldObject = new BasicDBObject();
                    String[] fileds = fls.split(",");
                    for (int i = 0; i < fileds.length; i++) {
                        fieldObject.append(fileds[i], 1);
                    }
                    dbCursor = collection.find(queryObject, fieldObject)
                            .sort(sortDBObject).skip(p).limit(ps);//由REST方法更改,分页数据起始位置结束为止不做计算(对方要求)
        }else if(condition.containsKey("tag")&&"v".equals(condition.get("sb"))){//按tag.v排序的特殊处理
        	BasicDBObject fieldObject = new BasicDBObject();
        	fieldObject.append("tag",new BasicDBObject().append("$elemMatch",new BasicDBObject().append("t",condition.get("tag"))));
        	for(String field:Constants.wholeField){
        		if(!field.equals("tag")){
        			fieldObject.append(field,1);
        		}
        	}
        	dbCursor = collection.find(queryObject, fieldObject)
            	.sort(sortDBObject).skip(p).limit(ps);
        }else {//返回全部字段
            dbCursor = collection.find(queryObject)
                    .sort(sortDBObject).skip(p).limit(ps);
        }
        DocInfo docInfo;
        //是否截断摘要
        boolean cut=(condition.get("cut")==null)||(condition.get("cut")==Boolean.TRUE);
        while (dbCursor.hasNext()) {
            DBObject docObject = dbCursor.next();
            docInfo=assignDocinfo(docObject);
            String summary=docObject.get("a")==null?null:(String)docObject.get("a");//摘要
            //限制摘要的最大的长度
            if(docInfo.ch!=null&&docInfo.ch!=Constants.CH_IWB&&docInfo.ch!=Constants.CH_OWB
            		&&cut&&summary!=null
            		&&summary.length()>Constants.MAX_SUMMARY_LENGTH){
            	docInfo.a=summary.substring(0,Constants.MAX_SUMMARY_LENGTH-1)+"...";
            }else{
            	docInfo.a=summary;
            }
            result.add(docInfo);
        }
        dbCursor.close();
        long end=System.currentTimeMillis();
        Logger.info(queryObject+"-"+sortDBObject+"-query cost p-"+p+"-ps-"+ps+":"+(middle-start)+"+"+(end-middle)+"="+(end-start)+"ms");
        return result;
    }
    
    
    /**
     * 单独处理按uis、sis查询的情况
     * @param condition
     * @return List<DocInfo>
     */
    private static List<DocInfo> getNewWith$or(HashMap<String, Object> condition){
    	long start=System.currentTimeMillis();
        List<DocInfo> result = new ArrayList<DocInfo>();
        DB db = DBUtil.getDB(Constants.MONGO_DB_DOCINFO);
        DBCollection collection = db.getCollection(Constants.MONGO_DOCINFO);
        long middle=System.currentTimeMillis();
        BasicDBObject queryObject = generateQuery(condition);
        //分页参数,默认为第1页,每页50条
        int p = 1, ps = 50;
        if (condition.containsKey("p")) {
            Object pObject = condition.get("p");
            if (pObject != null) {
                p = (Integer) pObject;
            }
        }
        if (condition.containsKey("ps")) {
            Object psObject = condition.get("ps");
            if (psObject != null) {
                ps = (Integer) psObject;
            }
        }
        int skipCount=0;
        if(condition.containsKey("start")){
        	 Object skipCountObject = condition.get("start");
             if (skipCountObject != null) {
            	 skipCount = (Integer)skipCountObject-1;
             }
        }else{
        	skipCount=(p-1)*ps;
        }
        //第一次过滤
        //拼装si、ui条件
        Object sisObject = condition.get("sis");
        Object uisObject = condition.get("uis");
        if (sisObject!=null&&uisObject!=null) {
            String sis = (String) sisObject;
            String uis = (String) uisObject;
            if (!sis.equals("")&&!uis.equals("")) {
                String[] sisArray = sis.split(",");
                int sisLength = sisArray.length;
                Long[] siArray = new Long[sisLength];
                for (int i = 0; i < sisLength; i++) {
                    siArray[i] = Long.parseLong(sisArray[i]);
                }
                String[] uisArray = uis.split(",");
                int uis_length = uisArray.length;
                if(sisLength==uis_length){
                	BasicDBList basicDBList = new BasicDBList();
                	for(int i=0;i<sisLength;i++){
                		BasicDBObject query = new BasicDBObject().append("si",siArray[i]).append("ui",uisArray[i]);
                		basicDBList.add(query);
                	}
                	queryObject.append("$or",basicDBList);
                }
            }
        }
        // 拼装按时间(pt)过滤条件
        String st = String.valueOf(condition.get("st"));
        String et = String.valueOf(condition.get("et"));
        boolean flag = false;// st和et至少传一个,才会拼装时间过滤条件
        BasicDBObject basicDBObject = new BasicDBObject();
        try {
            if (!st.equals("") && !st.equals("null")) {
                Date stTime = DateUtil.string2UtilDate(st,
                        "yyyy-MM-dd HH:mm:ss");
                basicDBObject.append("$gte", stTime.getTime() / 1000);
                flag = true;
            }
            if (!et.equals("") && !et.equals("null")) {
                Date etTime = DateUtil.string2UtilDate(et,
                        "yyyy-MM-dd HH:mm:ss");
                basicDBObject.append("$lte", etTime.getTime() / 1000);
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (flag) {
            queryObject.append("pt", basicDBObject);
        }
        //拼装需要排除的文章id条件
        if(condition.containsKey("nids")){
        	Object nidsObject = condition.get("nids");
            if(nidsObject!=null){
                String nids=(String)nidsObject;
                if(!nids.equals("")){
                	String[] nidArray=nids.split(",");
                	Long[] idArray=new Long[nidArray.length];
                	for(int i=0;i<nidArray.length;i++){
                		idArray[i]=Long.parseLong(nidArray[i]);
                	}
                    queryObject.append("_id",new BasicDBObject().append("$nin",idArray));
                }
            }
        }
        BasicDBObject match=new BasicDBObject();
        match.append("$match",queryObject);
        //按pt降序排序
        BasicDBObject sort=new BasicDBObject();
        sort.append("$sort",new BasicDBObject().append("pt",-1));
        //翻页条件
        BasicDBObject skip=new BasicDBObject();
        skip.append("$skip",skipCount);
        BasicDBObject limit=new BasicDBObject();
        limit.append("$limit",ps);
        AggregationOutput aggregate = collection.aggregate(match,sort,skip,limit);
        CommandResult commandResult = aggregate.getCommandResult();
        BasicDBList dbList =(BasicDBList)commandResult.get("result");
        DocInfo docInfo;
        for (Object dbObject:dbList) {
        	docInfo=new DocInfo();
        	BasicDBObject docObject=(BasicDBObject)dbObject;
        	docInfo=assignDocinfo(docObject);
            result.add(docInfo);
		}
        long end=System.currentTimeMillis();
        Logger.info(queryObject+"-"+sort+"-query cost p-"+p+"-ps-"+ps+":"+(middle-start)+"+"+(end-middle)+"="+(end-start)+"ms");
        return result;
    }
    
    /**
     * 根据hashmap传进来的参数,拼装查询条件
     * 
     * @param condition
     * @return BasicDBObject
     */
    private static BasicDBObject generateQuery(HashMap<String, Object> condition) {
        BasicDBObject queryObject = new BasicDBObject();
        if (condition == null) {
            return queryObject;
        }
        // 拼装起始文章id条件
        if (condition.containsKey("sid")) {
            Object sidObject = condition.get("sid");
            if (sidObject != null) {
                queryObject.append("_id", new BasicDBObject("$gte",
                        (Long) sidObject));
            }
        }
        // 拼装文章id列表条件
        if (condition.containsKey("did")) {
            Object didObject = condition.get("did");
            if (didObject != null) {
                String dids = (String) didObject;
                if (!dids.equals("")) {
                    String[] didsArray = dids.split(",");
                    int length = didsArray.length;
                    Long[] didArray = new Long[length];
                    for (int i = 0; i < length; i++) {
                        didArray[i] = Long.parseLong(didsArray[i]);
                    }
                    if(didArray.length==1){
                    	queryObject.append("_id",didArray[0]);
                    }else{
                    	queryObject.append("_id",
                                new BasicDBObject("$in", didArray));
                    }
                }
            }
        }
        // 拼装频道查询条件
        //replies为false不返回评论回帖;为true时返回全部数据(必须处理null,不然会影响其他地方)
        Boolean replies=(Boolean) condition.get("replies");
        if (condition.containsKey("ch")) {
            Object chObject = condition.get("ch");
            if (chObject != null) {
                String chs = (String) chObject;
                if (!chs.equals("")) {
                    String[] chsArray = chs.split(",");
                    int length = chsArray.length;
                    int[] chArray = new int[length];
                    for (int i = 0; i < length; i++) {
                    	//指定通道不管replies为true,还是false都按通道取数据
                        chArray[i] = Integer.parseInt(chsArray[i]);
                    }
                    if(chArray.length==1){
                    	queryObject.append("ch", chArray[0]);
                    }else{
                    	queryObject.append("ch", new BasicDBObject("$in", chArray));
                    }
                }else{
                	//是否排除评论回帖
                	if(replies!=null&&!replies){
                		queryObject.append("ch", new BasicDBObject("$ne", 7));
                	}
                }
            }else{
            	//是否排除评论回帖
            	if(replies!=null&&!replies){
            		queryObject.append("ch", new BasicDBObject("$ne", 7));
            	}
            }
        }else{
        	//是否排除评论回帖
        	if(replies!=null&&!replies){
        		queryObject.append("ch", new BasicDBObject("$ne", 7));
        	}
        }
        // 拼装板块id查询条件
        if (condition.containsKey("bi")) {
            Object biObject = condition.get("bi");
            if (biObject != null) {
                String bis = (String) biObject;
                if (!bis.equals("")) {
                    String[] bisArray = bis.split(",");
                    int length = bisArray.length;
                    int[] biArray = new int[length];
                    for (int i = 0; i < length; i++) {
                        biArray[i] = Integer.parseInt(bisArray[i]);
                    }
                    if(biArray.length==1){
                    	queryObject.append("bi", biArray[0]);
                    }else{
                    	queryObject.append("bi", new BasicDBObject("$in", biArray));
                    }
                }
            }
        }
        // 拼装按时间过滤条件,过滤时间类型,1,gt采集时间,2,pt发布时间,默认为发布时间
        String st = String.valueOf(condition.get("st"));
        String et = String.valueOf(condition.get("et"));
        boolean flag = false;// st和et至少传一个,才会拼装时间过滤条件
        BasicDBObject basicDBObject = new BasicDBObject();
        try {
            if (!st.equals("") && !st.equals("null")) {
                Date stTime = DateUtil.string2UtilDate(st,
                        "yyyy-MM-dd HH:mm:ss");
                basicDBObject.append("$gte", stTime.getTime() / 1000);
                flag = true;
            }
            if (!et.equals("") && !et.equals("null")) {
                Date etTime = DateUtil.string2UtilDate(et,
                        "yyyy-MM-dd HH:mm:ss");
                basicDBObject.append("$lte", etTime.getTime() / 1000);
                flag = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        String timeType = "pt";
        if (condition.containsKey("tt")) {
            Object ttObject = condition.get("tt");
            if (ttObject != null) {
                if ((Integer) ttObject == 1) {
                    timeType = "gt";
                }
            }
            if (flag) {
                queryObject.append(timeType, basicDBObject);
            }
        }
        //拼装插入时间条件
        if(condition.containsKey("it")){
            String it = String.valueOf(condition.get("it"));
            if (!it.equals("") && !it.equals("null")) {
                try {
                    Date itTime = DateUtil.string2UtilDate(it,
                            "yyyy-MM-dd HH:mm:ss");
                    queryObject.append("it",new BasicDBObject("$gte", itTime.getTime() / 1000));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
      //拼装发布时间条件
        if(condition.containsKey("pt")){
            String pt = String.valueOf(condition.get("pt"));
            if (!pt.equals("") && !pt.equals("null")) {
                try {
                    Date itTime = DateUtil.string2UtilDate(pt,
                            "yyyy-MM-dd HH:mm:ss");
                    queryObject.append("pt",new BasicDBObject("$gte", itTime.getTime() / 1000));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        // 拼装md5条件
        if (condition.containsKey("md5")) {
            Object md5Object = condition.get("md5");
            if (md5Object != null) {
                String md5 = (String) md5Object;
                if (!md5.equals("")) {
                	String[] md5Array=md5.split(",");
                	if(md5Array.length==1){
                		queryObject.append("md5", md5Array[0]);
                	}else{
                		queryObject.append("md5",
                                new BasicDBObject("$in", md5Array));
                	}
                }
            }
        }
        // 拼装内容指纹条件
        if (condition.containsKey("cf")) {
            Object cfObject = condition.get("cf");
            if (cfObject != null) {
                String cf = (String) cfObject;
                if (!cf.equals("")) {
                	String[] cfArray=cf.split(",");
                	if(cfArray.length==1){
                		queryObject.append("cf", cfArray[0]);
                	}else{
                		queryObject.append("cf",
                                new BasicDBObject("$in", cfArray));
                	}
                }
            }
        }
        // 拼装倾向性条件
        if (condition.containsKey("sen")) {
            Object senObject = condition.get("sen");
            if (senObject != null) {
                queryObject.append("sen", (Integer) senObject);
            }
        }
        // 拼装作者条件
        if (condition.containsKey("au")) {
            Object auObject = condition.get("au");
            if (auObject != null) {
                String au = (String) auObject;
                if (!au.equals("")) {
                	String[] auArray=au.split(",");
                	if(auArray.length==1){
                		queryObject.append("au", auArray[0]);
                	}else{
                		queryObject.append("au",
                                new BasicDBObject("$in", auArray));
                	}
                }
            }
        }
        // 拼装新闻来源条件
        if (condition.containsKey("src")) {
            Object srcObject = condition.get("src");
            if (srcObject != null) {
                String src = (String) srcObject;
                if (!src.equals("")) {
                	String[] srcArray=src.split(",");
                	if(srcArray.length==1){
                		queryObject.append("src",srcArray[0]);
                	}else{
                		queryObject.append("src",new BasicDBObject("$in", srcArray));
                	}
                }
            }
        }
        // 拼装转载数条件
        if (condition.containsKey("rpc")) {
            Object rpcObject = condition.get("rpc");
            if (rpcObject != null) {
                queryObject.append("rpc", new BasicDBObject("$gt",
                        (Integer) rpcObject));
            }
        }
        // 拼装转载标识条件
        if (condition.containsKey("rpf")) {
            Object rpfObject = condition.get("rpf");
            if (rpfObject != null) {
                queryObject.append("rpf", (Integer) rpfObject);
            }
        }
        // 拼装点击数条件
        if (condition.containsKey("cc")) {
            Object ccObject = condition.get("cc");
            if (ccObject != null) {
                queryObject.append("cc", new BasicDBObject("$gt",
                        (Integer) ccObject));
            }
        }
        // 拼装转发数条件
        if (condition.containsKey("rc")) {
            Object rcObject = condition.get("rc");
            if (rcObject != null) {
                queryObject.append("rc", new BasicDBObject("$gt",
                        (Integer) rcObject));
            }
        }
        // 拼装是否网站首页条件
        if (condition.containsKey("ish")) {
            Object ishObject = condition.get("ish");
            if (ishObject != null) {
                queryObject.append("ish", (Integer) ishObject);
            }
        }
        // 拼装重要度/热度条件
        if (condition.containsKey("sc>")) {
            Object scObject = condition.get("sc>");
            if (scObject != null) {
                queryObject.append("sc", new BasicDBObject("$gt",
                        (Integer) scObject));
            }
        } else if (condition.containsKey("sc>=")) {
            Object scObject = condition.get("sc>=");
            if (scObject != null) {
                queryObject.append("sc", new BasicDBObject("$gte",
                        (Integer) scObject));
            }
        }
        if (condition.containsKey("sc<")) {
            Object scObject = condition.get("sc<");
            if (scObject != null) {
                queryObject.append("sc", new BasicDBObject("$lt",
                        (Integer) scObject));
            }
        } else if (condition.containsKey("sc<=")) {
            Object scObject = condition.get("sc<=");
            if (scObject != null) {
                queryObject.append("sc", new BasicDBObject("$lte",
                        (Integer) scObject));
            }
        }
        
        // 拼装网站id条件
        if (condition.containsKey("si")) {
            Object siObject = condition.get("si");
            if (siObject != null) {
                String sis = (String) siObject;
                if (!sis.equals("")) {
                    String[] sisArray = sis.split(",");
                    int length = sisArray.length;
                    Long[] siArray = new Long[length];
                    for (int i = 0; i < length; i++) {
                        siArray[i] = Long.parseLong(sisArray[i]);
                    }
                    if(siArray.length==1){
                    	queryObject.append("si", siArray[0]);
                    }else{
                    	queryObject.append("si", new BasicDBObject("$in",siArray));
                    }
                }
            }
        }
        // 拼装板块类别标签条件
        String[] bct_array=null;
        if (condition.containsKey("bct")) {
            Object bctObject = condition.get("bct");
            if (bctObject != null) {
                String bcts = (String) bctObject;
                if (!bcts.equals("")) {
                    bct_array = bcts.split(",");
                    if(bct_array.length==1){
                    	queryObject.append("bct", bct_array[0]);
                    }else{
                    	queryObject.append("bct", new BasicDBObject("$in",bct_array));
                    }
                }
            }
        }
        //拼装自定义分组的查询条件
        if(condition.containsKey("mybct")||condition.containsKey("mybi")){
			//如果已经拼装过bct条件,先移除
			if (condition.containsKey("bct")) {
				queryObject.remove("bct");
			}
			Object mybctObject = condition.get("mybct");
			Object mybiObject = condition.get("mybi");
			if (mybctObject != null && mybiObject != null) {
				String bcts = (String) mybctObject;
				String[] bctsArray = bcts.split(",");
				String bis = (String) mybiObject;
				String[] bisArray = bis.split(",");
				int biLength = bisArray.length;
				Long[] biArray = new Long[biLength];
				for (int i = 0; i < biLength; i++) {
					biArray[i] = Long.parseLong(bisArray[i]);
				}
				BasicDBObject[] myBasicDBObject = new BasicDBObject[2];
				if (bct_array != null) {
					BasicDBObject[] bctBasicDBObject = new BasicDBObject[2];
					bctBasicDBObject[0] = new BasicDBObject("bct",
							new BasicDBObject("$in", bct_array));
					bctBasicDBObject[1] = new BasicDBObject("bct",
							new BasicDBObject("$in", bctsArray));
					myBasicDBObject[0] = new BasicDBObject("$and",
							bctBasicDBObject);
				} else {
					myBasicDBObject[0] = new BasicDBObject("bct",
							new BasicDBObject("$in", bctsArray));
				}
				myBasicDBObject[1] = new BasicDBObject("bi", new BasicDBObject(
						"$in", biArray));
				queryObject.append("$or", myBasicDBObject);
			} else if (mybctObject != null) {
				String bcts = (String) mybctObject;
				String[] bctsArray = bcts.split(",");
				if (bct_array != null) {
					BasicDBObject[] bctBasicDBObject = new BasicDBObject[2];
					bctBasicDBObject[0] = new BasicDBObject("bct",
							new BasicDBObject("$in", bct_array));
					bctBasicDBObject[1] = new BasicDBObject("bct",
							new BasicDBObject("$in", bctsArray));
					queryObject.append("$and", bctBasicDBObject);
				} else {
					queryObject.append("bct",
							new BasicDBObject().append("$in", bctsArray));
				}
			} else {
				String bis = (String) mybiObject;
				String[] bisArray = bis.split(",");
				int biLength = bisArray.length;
				Long[] biArray = new Long[biLength];
				for (int i = 0; i < biLength; i++) {
					biArray[i] = Long.parseLong(bisArray[i]);
				}
				if(biArray.length==1){
					queryObject.append("bi", biArray[0]);
				}else{
					queryObject.append("bi", new BasicDBObject().append("$in", biArray));
				}
			}
        }
        // 拼装标签条件
        if (condition.containsKey("tag")) {
            Object tagObject = condition.get("tag");
            if (tagObject != null) {
                String tag = (String) condition.get("tag");
                if (!tag.equals("")) {
                	String[] tagArray=tag.split(",");
                	if(tagArray.length==1){
                		queryObject.append("tag.t", tagArray[0]);
                	}else{
                		queryObject.append("tag.t", new BasicDBObject().append("$in", tagArray));
                	}
                }
            }
        }
        //拼装按关键词模糊查找标题条件
        if(condition.containsKey("tk")){
            Object tkObject = condition.get("tk");
            if(tkObject!=null){
                String titleKeywords=(String)tkObject;
                if(!titleKeywords.equals("")){
                    String[] keywords=titleKeywords.trim().split("\\s+");
                    int length=keywords.length;
                    Pattern pattern[]=new Pattern[length];
                    for (int i = 0; i < length; i++) {
                        pattern[i]=Pattern.compile(keywords[i]);
                    }
                    queryObject.append("t", new BasicDBObject().append("$in", pattern));
                }
            }
        }
        //拼装按关键词模糊查找内容条件(适用于微博通道)
        if(condition.containsKey("ak")){
            Object akObject = condition.get("ak");
            if(akObject!=null){
                String contentKeywords=(String)akObject;
                if(!contentKeywords.equals("")){
                    String[] keywords=contentKeywords.trim().split("\\s+");
                    int length=keywords.length;
                    Pattern pattern[]=new Pattern[length];
                    for (int i = 0; i < length; i++) {
                        pattern[i]=Pattern.compile(keywords[i]);
                    }
                    queryObject.append("a", new BasicDBObject().append("$in", pattern));
                }
            }
        }
        //拼装按关键词模糊查找文章标题(新闻、论坛、博客、境外)或是内容(微博、评论)条件
        if(condition.containsKey("atk")){
            Object atkObject = condition.get("atk");
            if(atkObject!=null){
                String contentKeywords=(String)atkObject;
                if(!contentKeywords.equals("")){
                    String[] keywords=contentKeywords.trim().split("\\s+");
                    int length=keywords.length;
                    Pattern pattern[]=new Pattern[length];
                    for (int i = 0; i < length; i++) {
                        pattern[i]=Pattern.compile(keywords[i]);
                    }
                    //新闻、论坛、博客、境外通道按标题查询
                    BasicDBObject subQuery1=new BasicDBObject();
                    int[] ch1={Constants.CH_NEWS,Constants.CH_BBS,Constants.CH_BLOG,Constants.CH_JW};
                    subQuery1.append("ch",new BasicDBObject().append("$in",ch1));
                    subQuery1.append("t", new BasicDBObject().append("$in", pattern));
                    //国内微博、国外微博、评论通道按内容查询
                    BasicDBObject subQuery2=new BasicDBObject();
                    int[] ch2={Constants.CH_IWB,Constants.CH_OWB,Constants.CH_CC};
                    subQuery2.append("ch",new BasicDBObject().append("$in",ch2));
                    subQuery2.append("a", new BasicDBObject().append("$in", pattern));
                    BasicDBList subQuery=new BasicDBList();
                    subQuery.add(subQuery1);
                    subQuery.add(subQuery2);
                    queryObject.append("$or",subQuery);
                }
            }
        }
        //拼装按关键词模糊查找k字段条件
        if(condition.containsKey("kk")){
            Object kkObject = condition.get("kk");
            if(kkObject!=null){
                String keywords=(String)kkObject;
                if(!keywords.equals("")){
                    String[] keywordArray=keywords.trim().split("\\s+");
                    int length=keywordArray.length;
                    Pattern pattern[]=new Pattern[length];
                    for (int i = 0; i < length; i++) {
                        pattern[i]=Pattern.compile(keywordArray[i]);
                    }
                    queryObject.append("k", new BasicDBObject().append("$in", pattern));
                }
            }
        }
        //拼装按关键词模糊查找板块名称条件
        if(condition.containsKey("bn")){
            Object bnObject = condition.get("bn");
            if(bnObject!=null){
                String bnKeywords=(String)bnObject;
                if(!bnKeywords.equals("")){
                    String[] keywords=bnKeywords.trim().split("\\s+");
                    int length=keywords.length;
                    Pattern pattern[]=new Pattern[length];
                    for (int i = 0; i < length; i++) {
                        pattern[i]=Pattern.compile(keywords[i]);
                    }
                    queryObject.append("bn", new BasicDBObject().append("$in", pattern));
                }
            }
        }
        //拼装按关键词模糊查找网站名称条件
        if(condition.containsKey("sn")){
            Object snObject = condition.get("sn");
            if(snObject!=null){
                String snKeywords=(String)snObject;
                if(!snKeywords.equals("")){
                    String[] keywords=snKeywords.trim().split("\\s+");
                    int length=keywords.length;
                    Pattern pattern[]=new Pattern[length];
                    for (int i = 0; i < length; i++) {
                        pattern[i]=Pattern.compile(keywords[i]);
                    }
                    queryObject.append("sn", new BasicDBObject().append("$in", pattern));
                }
            }
        }
        //拼装按微博原始消息id查找微博条件
        if(condition.containsKey("mid")){
            Object midObject = condition.get("mid");
            if(midObject!=null){
                String mid=(String)midObject;
                if(!mid.equals("")){
                    queryObject.append("mid",midObject);
                }
            }
        }
        //拼装按微博作者id查找微博条件
        if(condition.containsKey("ui")){
            Object uiObject = condition.get("ui");
            if(uiObject!=null){
                String ui=(String)uiObject;
                if(!ui.equals("")){
                    queryObject.append("ui",uiObject);
                }
            }
        }
        //拼装需要排除的文章id条件
        if(condition.containsKey("nids")){
        	Object nidsObject = condition.get("nids");
            if(nidsObject!=null){
                String nids=(String)nidsObject;
                if(!nids.equals("")){
                	String[] nidArray=nids.split(",");
                	Long[] idArray=new Long[nidArray.length];
                	for(int i=0;i<nidArray.length;i++){
                		idArray[i]=Long.parseLong(nidArray[i]);
                	}
                    queryObject.append("_id",new BasicDBObject().append("$nin",idArray));
                }
            }
        }
        return queryObject;
    }

    /**
     * 返回某张表某篇文章的特定字段
     * 
     * @param collection
     *            表名
     * @param did
     *            文章id
     * @param field
     *            返回字段表内名称
     * @param fieldRename
     *            返回字段名称
     * @return Map<String, Object>
     */
    public static Map<String, Object> getSpecialFiled(String dbName,String collection,
            Long did, String field, String filedRename) {
        Map<String, Object> result = new HashMap<String, Object>();
        DB db = DBUtil.getDB(dbName);
        DBCollection dbCollection = db.getCollection(collection);
        // 查询条件
        BasicDBObject queryObject = new BasicDBObject();
        queryObject.put("_id", did);
        // 返回字段
        BasicDBObject fieldObject = new BasicDBObject();
        fieldObject.put(field, 1);
        DBObject docObject = dbCollection.findOne(queryObject, fieldObject);
        String returnField = "";
        if (docObject != null) {
            returnField = (String) docObject.get(field);
        }
        result.put(filedRename, returnField);
        return result;
    }
    
    /**
     * 私有标红方法
     * @param content
     * @param keyword
     * @param mark
     * @param prefix
     * @param suffix
     * @return String
     */
    public static String getMarkedString(String content,String keyword,Integer mark,String prefix,String suffix){
    	if(content==null||"".equals(content)){
    		return "";
    	}
    	if(keyword==null||"".equals(keyword)){
    		return content;
    	}else{
    		keyword=keyword.trim();
    	}
    	if(mark==null){
    		mark=0;
    	}
    	switch (mark.intValue()) {
		case 0:
			prefix="";
			suffix="";
			break;
		case 1:
			prefix=Constants.prefixHTML;
			suffix=Constants.suffixHTML;
			break;
		case 2:
            if(prefix==null||"".equals(prefix)){
            	prefix=Constants.prefixHTML;
            }
            if(suffix==null||"".equals(suffix)){
            	suffix=Constants.suffixHTML;
            }
			break;
		default:
			prefix="";
			suffix="";
			break;
		}
    	String result=content.replaceAll("<", "&lt").replaceAll(">", "&gt");
        String[] split = keyword.split("\\s+");
        for (int i=0;i<split.length;i++) {
			result=result.replaceAll(split[i], prefix+split[i]+suffix);
		}
    	return result;
    }
    
    /**
     * 将DBObject中的值赋值到Docinfo中
     * @param dbObject
     * @return DocInfo
     */
    public static DocInfo assignDocinfo(DBObject docObject){
    	DocInfo docInfo=new DocInfo();
		docInfo._id=docObject.get("_id")==null?0l:(Long)docObject.get("_id");//文章ID,CT生成
		docInfo.ch=docObject.get("ch")==null?0:(Integer)docObject.get("ch");//通道类型
		docInfo.u=docObject.get("u")==null?"":(String)docObject.get("u");//URL
		docInfo.a=docObject.get("a")==null?"":(String)docObject.get("a");//摘要
		docInfo.pt=DateUtil.getDateStrFromTime(docObject.get("pt")==null?null:(Integer)docObject.get("pt"));//发布时间
		docInfo.gt=DateUtil.getDateStrFromTime(docObject.get("gt")==null?null:(Integer)docObject.get("gt"));//采集时间
		docInfo.au=docObject.get("au")==null?"":(String)docObject.get("au");//作者
		docInfo.si=docObject.get("si")==null?0:(Integer)docObject.get("si");//网站id
		docInfo.sa=SiteAreaUtil.getAreaInfo(docInfo.si);//网站地域信息
		docInfo.sn=docObject.get("sn")==null?"":(String)docObject.get("sn");//网站名称
		Object tagObject=docObject.get("tag");//标签
		if(tagObject!=null){
			BasicDBList tagList=(BasicDBList)tagObject;
			Tag[] tags=new Tag[tagList.size()];
			for (int i=0;i<tagList.size();i++) {
				DBObject tag=(DBObject)tagList.get(i);
				tags[i]=new Tag();
				tags[i].t=tag.get("t")==null?"":(String)(tag.get("t"));
				tags[i].v=tag.get("v")==null?0:Double.parseDouble(tag.get("v").toString());
			}
			docInfo.tag=tags;
		}
		docInfo.cf=docObject.get("cf")==null?"":(String)docObject.get("cf");//内容指纹
		docInfo.src=docObject.get("src")==null?"":(String)docObject.get("src");//来源
		docInfo.rpc=docObject.get("rpc")==null?0:(Integer)docObject.get("rpc");//转载数
		docInfo.rc=docObject.get("rc")==null?0:(Integer)docObject.get("rc");//评论\回复数
		docInfo.mc=docObject.get("mc")==null?0:(Integer)docObject.get("mc");//暂时不使用
		docInfo.sc=docObject.get("sc")==null?0:(Integer)docObject.get("sc");//重要度/热度
		docInfo.bct=docObject.get("bct")==null?(new String[0]):((BasicDBList)docObject.get("bct")).toArray(new String[0]);//板块类别标签
		docInfo.pic=docObject.get("pic")==null?(new String[0]):((BasicDBList)docObject.get("pic")).toArray(new String[0]);//图片列表
		docInfo.video=docObject.get("video")==null?(new String[0]):((BasicDBList)docObject.get("video")).toArray(new String[0]);//视频列表
		docInfo.t=docObject.get("t")==null?"":(String)docObject.get("t");//标题
		docInfo.bi=docObject.get("bi")==null?0:(Integer)docObject.get("bi");//版块id
		docInfo.bn=docObject.get("bn")==null?"":(String)docObject.get("bn");//板块名称
		docInfo.k=docObject.get("k")==null?"":(String)docObject.get("k");//关键词
		docInfo.kv=docObject.get("kv")==null?"":(String)docObject.get("kv");//关键词向量
		docInfo.pl=docObject.get("pl")==null?"":(String)docObject.get("pl");//人名列表
		docInfo.rl=docObject.get("rl")==null?"":(String)docObject.get("rl");//地名列表
		docInfo.ol=docObject.get("ol")==null?"":(String)docObject.get("ol");//机构名列表
		docInfo.md5=docObject.get("md5")==null?"":(String)docObject.get("md5");//网页MD5
		docInfo.rpf=docObject.get("rpf")==null?0:(Integer)docObject.get("rpf");//转载标识
		docInfo.cc=docObject.get("cc")==null?0:(Integer)docObject.get("cc");//点击数
		docInfo.ish=docObject.get("ish")==null?0:(Integer)docObject.get("ish");//是否网站首页文章
		docInfo.nn=docObject.get("nn")==null?0:(Integer)docObject.get("nn");//负面指数
		docInfo.irp=docObject.get("irp")==null?0:(Integer)docObject.get("irp");//是否为回复消息标志,1:是
		docInfo.mid=docObject.get("mid")==null?"":(String)docObject.get("mid");//微博作者的用户id
		docInfo.ui=docObject.get("ui")==null?"":(String)docObject.get("ui");//消息原始ID
		docInfo.un=docObject.get("un")==null?"":(String)docObject.get("un");//微博作者的用户名
		docInfo.iu=docObject.get("iu")==null?"":(String)docObject.get("iu");//用户头像地址
		docInfo.audio=docObject.get("audio")==null?"":(String)docObject.get("audio");//音频url
		docInfo.nc=null;//暂时不用nc字段
		//docInfo.nc=docObject.get("nc")==null?(new String[0]):((BasicDBList)docObject.get("nc")).toArray(new String[0]);//微博内出现的 @名字,格式:[“@lisi"]
		docInfo.rpi=docObject.get("rpi")==null?"":(String)docObject.get("rpi");//被转发消息id
		docInfo.ri=docObject.get("ri")==null?"":(String)docObject.get("ri");//被转发消息源头id
		docInfo.ru=docObject.get("ru")==null?"":(String)docObject.get("ru");//转发用户ID(ri用户消息的作者)
		docInfo.rpm=docObject.get("rpm")==null?"":(String)docObject.get("rpm");//被回复ID
		docInfo.rpu=docObject.get("rpu")==null?"":(String)docObject.get("rpu");//被回复人ID
		docInfo.rps=docObject.get("rps")==null?"":(String)docObject.get("rps");//被回复人昵称
		docInfo.srh=docObject.get("srh")==null?"":(String)docObject.get("srh");//微博来源链接
		docInfo.cl=docObject.get("cl")==null?0:(Integer)docObject.get("cl");//内容长度
		docInfo.fn=docObject.get("fn")==null?0:(Integer)docObject.get("fn");//帖子楼层
		//微博通道转换url
		if(docInfo.ch!=null&&(docInfo.ch==5||docInfo.ch==6)&&docInfo.si!=null&&docInfo.ui!=null){
			docInfo.u=transUrl(docInfo.si,docInfo.ui)+docInfo.u;
		}
		return docInfo;
    }
    
    /**
     * 转换微博通道url
     * @param si
     * @param ui
     * @return
     */
    public static String transUrl(int si,String ui){
    	String url="";
    	switch (si) {
		case 100000001:
			url="http://www.weibo.com/"+ui+"/";
			break;
		case 100000002:
			break;
		case 100000003:
			url="http://t.qq.com";
			break;
		case 100000004:
			url="http://";
			break;
		case 100000005:
			url="http://";
			break;
		default:
			break;
		}
    	return url;
    }     
}


DBUtil.java MONGODB连接类
package util;

import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.MongoOptions;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;

/**
 * 数据库工具类
 * 
 * @author twy
 * 
 */
public class DBUtil {

    // mysql数据库对象
    public static play.db.DB db = new play.db.DB();
    
    // mongo db Map对象
    public static HashMap<String,com.mongodb.DB> dbMap;

    /**
     * 根据ip、端口、数据库名称返回DB对象
     * @param ip ip地址
     * @param port 端口
     * @param dbName 数据库名称
     * @return com.mongodb.DB
     */
    public static com.mongodb.DB findDB(String dbName,String ip,String port,int connections){
    	com.mongodb.DB db=null;
    	//mongo数据库地址列表
    	List<ServerAddress> addrs=new ArrayList<ServerAddress>();
    	try {
    		String[] dbIpArray=ip.trim().split(",");
        	String[] dbPortArray=port.trim().split(",");
        	ServerAddress serverAddress;
        	for (int i=0;i<dbIpArray.length; i++) {
        		serverAddress=new ServerAddress(dbIpArray[i],Integer.parseInt(dbPortArray[i]));
        		addrs.add(serverAddress);
			}
        	MongoOptions mongoOptions=new MongoOptions();
        	//每个主机允许的连接数(每个主机的连接池大小),当连接池被用光时,会被阻塞住 ,默认为10
        	mongoOptions.setConnectionsPerHost(connections);
        	//查询超过1000ms会抛出异常
//        	mongoOptions.setSocketTimeout(1000);
        	//被阻塞线程从连接池获取连接的最长等待时间(ms)
//        	mongoOptions.setMaxWaitTime(5000);
        	Mongo mongo = new Mongo(addrs, mongoOptions);
        	mongo.setReadPreference(ReadPreference.secondaryPreferred());
        	db=mongo.getDB(dbName);
        	return db;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e) {
            e.printStackTrace();
        }
    	return db;
    }
    
    /**
     * 在项目启动时调用该方法,初始化dbMap信息
     */
    public static void initDBMap(){
    	dbMap=new HashMap<String,com.mongodb.DB>();
    	//初始化docinfo库
    	String docinfoIp=Constants.rb.getString("mongo."+Constants.MONGO_DB_DOCINFO+".ip");
    	String docinfoPort=Constants.rb.getString("mongo."+Constants.MONGO_DB_DOCINFO+".port");
    	String docinfoConnections=Constants.rb.getString("mongo."+Constants.MONGO_DB_DOCINFO+".connections");
		com.mongodb.DB docinfoDb=findDB(Constants.MONGO_DB_DOCINFO,docinfoIp,docinfoPort,Integer.parseInt(docinfoConnections));
		dbMap.put(Constants.MONGO_DB_DOCINFO,docinfoDb);
		//初始化doctext库
    	String doctextIp=Constants.rb.getString("mongo."+Constants.MONGO_DB_DOCTEXT+".ip");
    	String doctextPort=Constants.rb.getString("mongo."+Constants.MONGO_DB_DOCTEXT+".port");
    	String doctextConnections=Constants.rb.getString("mongo."+Constants.MONGO_DB_DOCTEXT+".connections");
		com.mongodb.DB doctextDb=findDB(Constants.MONGO_DB_DOCTEXT,doctextIp,doctextPort,Integer.parseInt(doctextConnections));
		dbMap.put(Constants.MONGO_DB_DOCTEXT,doctextDb);
		//初始化snapshot库
    	String snapshotIp=Constants.rb.getString("mongo."+Constants.MONGO_DB_SNAPSHOT+".ip");
    	String snapshotPort=Constants.rb.getString("mongo."+Constants.MONGO_DB_SNAPSHOT+".port");
    	String snapshotConnections=Constants.rb.getString("mongo."+Constants.MONGO_DB_SNAPSHOT+".connections");
		com.mongodb.DB snapshotDb=findDB(Constants.MONGO_DB_SNAPSHOT,snapshotIp,snapshotPort,Integer.parseInt(snapshotConnections));
		dbMap.put(Constants.MONGO_DB_SNAPSHOT,snapshotDb);
		//初始化docreprint库
    	String docreprintIp=Constants.rb.getString("mongo."+Constants.MONGO_DB_DOCREPRINT+".ip");
    	String docreprintPort=Constants.rb.getString("mongo."+Constants.MONGO_DB_DOCREPRINT+".port");
    	String docreprintConnections=Constants.rb.getString("mongo."+Constants.MONGO_DB_DOCREPRINT+".connections");
		com.mongodb.DB docreprintDb=findDB(Constants.MONGO_DB_DOCREPRINT,docreprintIp,docreprintPort,Integer.parseInt(docreprintConnections));
		dbMap.put(Constants.MONGO_DB_DOCREPRINT,docreprintDb);
		//初始化weiboperson库
    	String weibopersonIp=Constants.rb.getString("mongo."+Constants.MONGO_DB_WEIBOPERSON+".ip");
    	String weibopersonPort=Constants.rb.getString("mongo."+Constants.MONGO_DB_WEIBOPERSON+".port");
    	String weibopersonConnections=Constants.rb.getString("mongo."+Constants.MONGO_DB_WEIBOPERSON+".connections");
		com.mongodb.DB weibopersonDb=findDB(Constants.MONGO_DB_WEIBOPERSON,weibopersonIp,weibopersonPort,Integer.parseInt(weibopersonConnections));
		dbMap.put(Constants.MONGO_DB_WEIBOPERSON,weibopersonDb);
    }
    
    /**
     * 
     * @param dbName数据库名称
     * @return com.mongodb.DB
     */
    public static com.mongodb.DB getDB(String dbName){
    	com.mongodb.DB db=dbMap.get(dbName);
    	return db;
    }

    /**
     * 关闭mysql数据库连接
     * 
     * @param conn
     *            连接对象
     */
    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void closeStatement(Statement ps) {
    	if (null != ps) {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    }
    
    public static void closeResultSet(ResultSet rs) {
    	if (null != rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    }
    
    public static void dispose(Connection con, Statement ps, ResultSet rs) {
    	closeResultSet(rs);
    	closeStatement(ps);
    	closeConnection(con);
    }
}

分享到:
评论

相关推荐

    jdbc java mongodb mysql 相互同步

    本文将深入探讨如何使用Java的JDBC(Java Database Connectivity)接口与MongoDB、MySQL数据库进行数据交互,并实现相互之间的数据同步。 首先,我们需要理解JDBC,它是Java语言访问数据库的标准API,能够连接并...

    java操作mongoDB(CRUD)

    Java操作MongoDB主要涉及到的是Java驱动程序与MongoDB数据库之间的交互,这涵盖了创建连接、执行CRUD(创建、读取、更新、删除)操作等一系列基本的数据库管理任务。MongoDB是一个流行的NoSQL数据库,以其灵活性、高...

    java操作mongoDB实现文件上传预览打包下载

    在Java编程环境中,MongoDB是一个广泛使用的文档型数据库,它以JSON格式存储数据,提供了灵活的数据模型和高性能的处理能力。对于"java操作mongoDB实现文件上传预览打包下载"这个主题,我们将深入探讨如何利用Java与...

    Java连接mongoDB需要的jar包(3.9.1)

    开发者可以使用这个库来创建MongoClient实例,建立到MongoDB服务器的连接,然后执行各种数据库操作,如读取、插入、更新和删除数据。 3. `mongodb-driver-core-3.9.1.jar`: 这个核心库包含了基本的网络通信和线程...

    Java操作MongoDB之CRUD(增删改查)

    这篇博文“Java操作MongoDB之CRUD(增删改查)”主要探讨了如何使用Java驱动程序来执行基本的数据库操作,包括创建(Create)、读取(Read)、更新(Update)和删除(Delete)。 首先,我们来了解MongoDB的Java驱动...

    mongodb java Driver

    它提供了一组丰富的API,使得在Java应用程序中执行CRUD(创建、读取、更新、删除)操作变得简单而高效。MongoDB是一个分布式文档存储数据库,而Java驱动程序则是连接MongoDB服务器与Java应用程序之间的桥梁。 首先...

    Spark-Mongodb是一个库允许用户利用SparkSQL读写数据至MongoDB集合

    Spark-MongoDB库将这两者完美结合,实现了在Spark上对MongoDB数据集的高效读写。 首先,让我们详细了解一下如何在Spark中使用Spark-MongoDB库进行数据读取。要连接到MongoDB,你需要配置MongoDB的URI(包括数据库名...

    Java操作MongoDB数据库示例分享

    Java是常用的编程语言,用于与MongoDB交互,提供了丰富的驱动程序库,如Java MongoDB驱动程序,使得开发人员能够方便地进行数据的存取。 在提供的代码片段中,`MongoDBConfig.java`是Java中连接MongoDB的一个配置类...

    geoserver发布mongodb矢量数据地图服务.docx

    Geoserver提供了强大的MongoDB插件,可以实现MongoDB矢量数据的读取和写入。 发布步骤: 1. 创建工作区 首先,需要创建一个工作区,用于存储矢量数据。 2. 创建数据存储 其次,需要创建一个数据存储,用于存储...

    mongodb_java_2.6_API

    MongoDB的Java驱动程序是Java开发者与MongoDB服务器通信的桥梁,它提供了丰富的API,使得在Java应用中执行CRUD(创建、读取、更新、删除)操作变得简单。2.6版本是历史版本,但仍然包含了许多核心功能,如连接管理...

    java连接mongodb.zip

    总之,Java连接MongoDB涉及的主要知识点包括MongoDB的Java驱动程序使用、数据库和集合操作、数据的CRUD(创建、读取、更新、删除)以及连接管理。通过理解这些概念和实践,开发者能够有效地在Java应用中集成MongoDB...

    java操作mongodb.zip

    MongoDB是一个分布式文档型数据库,它属于NoSQL数据库的一种,与传统的关系型数据库(如SQL)有着显著的区别。...通过Java驱动程序和相关的框架支持,可以方便地在Java Web应用中集成和操作MongoDB。

    Java连接mongoDB需要的jar包

    通过这个驱动,Java应用程序可以执行诸如创建、读取、更新和删除(CRUD)等操作。此JAR文件包含了连接管理、命令执行、数据转换等功能。 3. **mongodb-driver-core-3.9.1.jar**:这是驱动程序的核心库,包含了与...

    JAVA操作MongoDB

    通过Java操作MongoDB,可以使得Java应用程序能够轻松地存储和处理数据。 #### 2. 准备工作 在使用Java操作MongoDB之前,首先需要进行一些准备工作。这包括下载MongoDB的Java驱动包,因为它是Java应用程序与MongoDB...

    java连接mongodb3.4.2所需jar

    2. `mongodb-driver-3.4.2.jar`:这个JAR文件扩展了核心驱动,提供了更高级的功能,如MongoDB的CRUD(创建、读取、更新、删除)操作,以及数据模型和集合的抽象。它封装了与数据库的交互细节,使得开发人员可以更...

    java操作mongodb增删改查

    本文将详细介绍如何使用Java对MongoDB执行基本的CRUD(创建、读取、更新、删除)操作,并探讨一些高级查询技巧。 #### 二、环境搭建与基础配置 在开始之前,确保已经安装了MongoDB服务并正确启动。接下来,我们需要...

    使用java上传MP4文件至mongodb数据库,并下载 源代码

    本篇文章将深入讲解如何使用Java来上传MP4文件到MongoDB,并进行下载操作。 首先,我们需要引入MongoDB的Java驱动程序。MongoDB提供了Java驱动库,通过Maven或Gradle可以轻松添加依赖。在Maven的`pom.xml`中,你...

    mongoDB java driver api

    5. **CRUD操作**:MongoDB Java驱动提供了丰富的API来进行创建(Create)、读取(Retrieve)、更新(Update)和删除/Delete)数据的操作。例如,你可以使用`insertOne()`或`insertMany()`方法来插入数据,`find()`来查询,`...

    SpringBoot+MongoDB+Echarts图表数据可视化

    MongoDB提供了丰富的查询API和数据聚合框架,能够高效地读取和存储数据。在本项目中,MongoDB用于存储和检索用于数据可视化的数据。 Echarts是百度开源的一款JavaScript图表库,支持多种图表类型,如折线图、柱状图...

Global site tag (gtag.js) - Google Analytics