0

Twitter データの分析プログラムを開発しています。現在、mongoDB を使用しています。Twitter API からツイートを取得してデータベースに格納する Java プログラムを作成しようとしています。ツイートの取得はすでに非常にうまく機能していますが、データベースに入れたいときに問題があります。Twitter API はしばしばまったく同じツイートを返すため、データベースにある種のインデックスを配置する必要があります。

まず、データベースに接続して検索語に関連するコレクションを取得するか、コレクションが存在しない場合は作成します。

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_IDこのフィールドはインデックスに適したフィールドであり、コレクション内で一意である必要があると思います。

4

2 に答える 2

0

インデックスにオプションを設定しuniqueて、MongoDb が一意性を強制するようにします。

items.ensureIndex(index, new BasicDBObject("unique", true));

既存のインデックスを手動で削除し、すべての重複を削除する必要があることに注意してください。そうしないと、一意のインデックスを作成できなくなります。

于 2013-10-12T18:37:22.480 に答える