Commit 4fba4f5c by shenjinzhu

修改查询

parent fac593f1
......@@ -5,15 +5,10 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Resource;
import org.bson.BSON;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
......@@ -25,22 +20,19 @@ import org.springframework.stereotype.Component;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.zhiwei.manage.bean.EasyNews;
import com.zhiwei.manage.bean.News;
import com.zhiwei.manage.handle.DbDepot;
import com.zhiwei.manage.util.MongoConnect;
@Component
public class NewsDaoImpl implements NewsDao {
@Resource(name = "newmongoTemplate")
private MongoTemplate mongo;
@Resource(name = "newmongoTemplate")
private MongoTemplate spiderMongo;
@Override
public List<News> findByNew(int delayedCount, String type, String source) {
......@@ -52,13 +44,13 @@ public class NewsDaoImpl implements NewsDao {
query.addCriteria(Criteria.where("source").is(source));
query.with(new Sort(new Order(Direction.DESC, "time")));
query.limit(delayedCount);
return mongo.find(query, News.class, "net_media_" + year + (month>9?""+month:"0"+month));
return mongo.find(query, News.class, "net_media_" + year + (month > 9 ? "" + month : "0" + month));
} else {
Query query = new Query();
query.addCriteria(Criteria.where("type").is(type));
query.with(new Sort(new Order(Direction.DESC, "time")));
query.limit(delayedCount);
return mongo.find(query, News.class, "net_media_" + year + (month>9?""+month:"0"+month));
return mongo.find(query, News.class, "net_media_" + year + (month > 9 ? "" + month : "0" + month));
}
}
......@@ -72,13 +64,13 @@ public class NewsDaoImpl implements NewsDao {
query.addCriteria(Criteria.where("source").is(source));
query.with(new Sort(new Order(Direction.DESC, "time")));
query.limit(filedCount);
return mongo.find(query, News.class, "net_media_" + year + (month>9?""+month:"0"+month));
return mongo.find(query, News.class, "net_media_" + year + (month > 9 ? "" + month : "0" + month));
} else {
Query query = new Query();
query.addCriteria(Criteria.where("type").is(type));
query.with(new Sort(new Order(Direction.DESC, "time")));
query.limit(filedCount);
return mongo.find(query, News.class, "net_media_" + year + (month>9?""+month:"0"+month));
return mongo.find(query, News.class, "net_media_" + year + (month > 9 ? "" + month : "0" + month));
}
}
......@@ -90,7 +82,8 @@ public class NewsDaoImpl implements NewsDao {
Criteria criteria = new Criteria();
criteria.and("spyderInfoId").is(spyId);
criteria.and("time").gte(start).lt(end);
return mongo.count(Query.query(criteria), News.class, "net_media_" + year + (month>9?""+month:"0"+month));
return mongo.count(Query.query(criteria), News.class,
"net_media_" + year + (month > 9 ? "" + month : "0" + month));
}
@Override
......@@ -109,7 +102,8 @@ public class NewsDaoImpl implements NewsDao {
Criteria criteria = new Criteria();
criteria.and("source").is(source);
criteria.and("time").gte(start).lt(end);
return (int) mongo.count(Query.query(criteria), News.class, "net_media_" + year + (month>9?""+month:"0"+month));
return (int) mongo.count(Query.query(criteria), News.class,
"net_media_" + year + (month > 9 ? "" + month : "0" + month));
}
@Override
......@@ -118,62 +112,44 @@ public class NewsDaoImpl implements NewsDao {
int month = c.get(Calendar.MONTH) + 1;
int year = c.get(Calendar.YEAR);
return mongo.find(Query.query(Criteria.where("time").gte(start).lt(end)), EasyNews.class,
"net_media_" + year + (month>9?""+month:"0"+month));
"net_media_" + year + (month > 9 ? "" + month : "0" + month));
}
/**
* 获取新闻检测
*/
@SuppressWarnings({ "resource", "unchecked" })
@SuppressWarnings("unchecked")
@Override
public List<Map<String, Object>> findNews(String host, int port, String dbNamel, String collection, int count,
String id, String pt) {
Calendar cal=Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
int month=cal.get(Calendar.MONTH)+1;
String end=cal.get(Calendar.YEAR)+""+(month>9?month:"0"+month);
int month = cal.get(Calendar.MONTH) + 1;
String end = cal.get(Calendar.YEAR) + "" + (month > 9 ? month : "0" + month);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
MongoCollection<Document> col = null;
if (DbDepot.newsCol != null) {
if (DbDepot.newsCol.get(pt) != null) {
col = (MongoCollection<Document>) DbDepot.newsCol.get(pt);
} else {
MongoClient mongoClient = new MongoClient(host, port);
// 连接到数据库
MongoDatabase mongo = mongoClient.getDatabase(dbNamel);
col = mongo.getCollection(collection+end);
DbDepot.newsCol.put(pt, col);
}
} else {
DbDepot.newsCol = new ConcurrentHashMap<String, Object>();
MongoClient mongoClient = new MongoClient(host, port);
MongoDatabase mongo = mongoClient.getDatabase(dbNamel);
col = mongo.getCollection(collection+end);
DbDepot.newsCol.put(pt, col);
}
if (id != null & id.length() > 5) {
Bson sort = new BasicDBObject("savetime", -1);
FindIterable<Document> doc = col.find(new BasicDBObject("spyderInfoId", id)).sort(sort).limit(count);
MongoCursor<Document> mongoCursor = doc.iterator();
while (mongoCursor.hasNext()) {
Map<String, Object> map = mongoCursor.next();
DBCollection con = spiderMongo.getCollection(collection + end);
if (id != null && !id.equals("")) {
DBCursor cur = con.find(new BasicDBObject("spyderInfoId", id)).sort(new BasicDBObject("rsid", -1))
.limit(count);
while (cur.hasNext()) {
Map<String, Object> map = (Map<String, Object>) cur.next();
list.add(map);
}
}else{
Bson sort = new BasicDBObject("savetime", -1);
FindIterable<Document> doc = col.find().sort(sort).limit(count);
MongoCursor<Document> mongoCursor = doc.iterator();
while (mongoCursor.hasNext()) {
Map<String, Object> map = mongoCursor.next();
} else {
DBCursor cur = con.find().sort(new BasicDBObject("rsid", -1))
.limit(count);
while (cur.hasNext()) {
Map<String, Object> map = (Map<String, Object>) cur.next();
list.add(map);
}
}
return list;
}
public static void main(String[] args) {
MongoClient mongoClient = new MongoClient("115.236.59.91", 27017);
MongoDatabase mongo = mongoClient.getDatabase("mediaspider");
MongoCollection<Document> col = mongo.getCollection("");
MongoCollection<Document> col = mongo.getCollection("");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment