我正在为Twitter数据开发一个分析程序。我现在正在使用mongoDB。我试图编写一个Java程序,从Twitter中获取tweet并将它们放到数据库中。获取Tweets已经很好了,但是当我想将它们放到数据库中时,我遇到了一个问题。由于Twitter经常返回相同的Tweets,所以我必须在数据库中放置某种索引。
首先,我连接到数据库并获取与搜索项相关的集合,或者在不存在此集合的情况下创建该集合。
public void connectdb(String keyword)
{
try {
// on constructor load initialize MongoDB and load collection
initMongoDB();
items = db.getCollection(keyword);
BasicDBObject index = new BasicDBObject("tweet_ID", 1);
items.ensureIndex(index);
} catch (MongoException ex) {
System.out.println("MongoException :" + ex.getMessage());
}
}然后我得到推文并将它们放到数据库中:
public void getTweetByQuery(boolean loadRecords, String keyword) {
if (cb != null) {
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
Query query = new Query(keyword);
query.setCount(50);
QueryResult result;
result = twitter.search(query);
System.out.println("Getting Tweets...");
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
BasicDBObject basicObj = new BasicDBObject();
basicObj.put("user_name", tweet.getUser().getScreenName());
basicObj.put("retweet_count", tweet.getRetweetCount());
basicObj.put("tweet_followers_count", tweet.getUser().getFollowersCount());
UserMentionEntity[] mentioned = tweet.getUserMentionEntities();
basicObj.put("tweet_mentioned_count", mentioned.length);
basicObj.put("tweet_ID", tweet.getId());
basicObj.put("tweet_text", tweet.getText());
if (mentioned.length > 0) {
// System.out.println("Mentioned length " + mentioned.length + " Mentioned: " + mentioned[0].getName());
}
try {
items.insert(basicObj);
} catch (Exception e) {
System.out.println("MongoDB Connection Error : " + e.getMessage());
loadMenu();
}
}
// Printing fetched records from DB.
if (loadRecords) {
getTweetsRecords();
}
} catch (TwitterException te) {
System.out.println("te.getErrorCode() " + te.getErrorCode());
System.out.println("te.getExceptionCode() " + te.getExceptionCode());
System.out.println("te.getStatusCode() " + te.getStatusCode());
if (te.getStatusCode() == 401) {
System.out.println("Twitter Error : \nAuthentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect.\nEnsure that you have set valid consumer key/secret, access token/secret, and the system clock is in sync.");
} else {
System.out.println("Twitter Error : " + te.getMessage());
}
loadMenu();
}
} else {
System.out.println("MongoDB is not Connected! Please check mongoDB intance running..");
}
}但是,正如我前面提到的,经常有相同的tweet,而且它们在数据库中有副本。我认为tweet_ID字段是索引的好字段,在集合中应该是唯一的。
发布于 2014-01-22 09:49:32
这个问题已经回答了,但我想做一点贡献,因为MongoDB API 2.11提供了一个作为参数接收唯一选项的方法:
public void ensureIndex(DBObject keys, String name, boolean unique)对于想要在MongoDBNote上存储json文档的人来说,一个次要的提醒是,唯一性必须应用于BasicObject键,而不是超出值。例如:
BasicDBObject basicObj = new BasicDBObject();
basicObj.put("user_name", tweet.getUser().getScreenName());
basicObj.put("retweet_count", tweet.getRetweetCount());
basicObj.put("tweet_ID", tweet.getId());
basicObj.put("tweet_text", tweet.getText());
basicObj.put("a_json_text", "{"info_details":{"info_id":"1234"},"info_date":{"year":"2012"}, {"month":"12"}, {"day":"10"}}");在这种情况下,只能为基本对象键创建唯一索引:
BasicDBObject index = new BasicDBObject();
int directionOrder = 1;
index.put("tweet_ID", directionOrder);
boolean isUnique = true;
items.ensureIndex(index, "unique_tweet_ID", isUnique);任何与"info_id“这样的JSON值有关的索引都不能工作,因为它不是BasicObject键。
在MongDB上使用索引并不像听起来那么容易。您还可以查看MongoDB文档以获得更多详细信息,如Mongo索引教程和Mongo指数概念。当您需要一个组合索引时,方向顺序可能是非常重要的,这一点在这里得到了很好的解释,为什么方向顺序重要?。
https://stackoverflow.com/questions/19337449
复制相似问题