2017-03-05 13 views
1

私は、自分のcassandraテーブルとクエリtwitter APIのいずれかでデータを取得して、1人のユーザーのフォロワーと友だちを取得するプログラムを作成しました。私はすべてのIDをセットで安全にして、すべての信者/友人を得るとき、私はそれをカサンドラに書きます。1つのクエリでcassandraに多くのデータを書き込む

この問題は、1M24フォロワーを持っているユーザーの1人です。このコードを実行すると、設定された種類のサイズがcassandraに書き込みエラーを生成します。

def get_data(tweepy_function, author_id, author_username, session): 
    if tweepy_function == "followers": 
     followers = set() 
     for follower_id in tweepy.Cursor(API.followers_ids, id=author_id, count=5000).items(): 
      if len(followers) % 5000 == 0 and len(followers) != 0: 
       print("Collected followers: ", len(followers)) 
      followers.add(follower_id) 
     query = "INSERT INTO {0} (node_id, screen_name, centrality, follower_ids) VALUES ({1}, {2}, {3}, {4})"\ 
      .format("network", author_id, author_username, 0.0, followers) 
     session.execute(query) 
    if tweepy_function == "friends": 
     friends = set() 
     for friend_id in tweepy.Cursor(API.friends_ids, id=author_id, count=5000).items(): 
      if len(friends) % 5000 == 0 and len(friends) != 0: 
       print("Collected followers: ", len(friends)) 
      friends.add(friend_id) 
     query = "INSERT INTO {0} (node_id, screen_name, centrality, friend_ids) VALUES ({1}, {2}, {3}, {4})"\ 
      .format("network", author_id, author_username, 0.0, friends) 
     session.execute(query) 

としては、私は私のスキーマを追加尋ねた:

table = """CREATE TABLE IF NOT EXISTS 
        {0} (
         node_id bigint , 
         screen_name text, 
         last_tweets set<text>, 
         follower_ids set<bigint>, 
         friend_ids set<bigint>, 
         centrality float, 
         PRIMARY KEY (node_id)) 
         """.format(table_name) 

は、なぜ私が書き込みエラーが出るのですか?それを防ぐ方法は?それはカッサンドラへの安全なデータへの良い方法ですか?

+0

あなたのスキーマは何ですか? –

+0

@AshrafulIslamそれを追加してください – mel

答えて

2

あなたは、Cassandraの中コレクションの

制限セット(コレクション)としてfollower_idsfriend_idsを使用している:

  • コレクション内のアイテムの最大サイズは に応じて、64K又は2Bでありますネイティブプロトコルバージョンで。

  • カッサンドラはコレクション全体を読み込むので、クエリ中の遅延を防ぐためにコレクションを小さくしてください。コレクションは内部で ページングされていません。コレクションは に少量のデータのみを格納するように設計されています。

  • コレクションに64Kを超えるアイテムを挿入しないでください。 64 KBを超えるアイテムをコレクションに挿入すると、64 KBのアイテムのみがクエリ可能になり、データが失われます。あなたは以下のスキーマを使用することができます

:ここ

CREATE TABLE IF NOT EXISTS my_table (
    node_id bigint , 
    screen_name text, 
    last_tweets set<text>, 
    centrality float, 
    friend_follower_id bigint, 
    is_friend boolean, 
    is_follower boolean, 
    PRIMARY KEY ((node_id), friend_follower_id) 
); 

friend_follower_idを友人が、その後フォロワーtrueとして及び場合is_friendをマークした場合、friendidまたはfolloweridで、その後is_follower

trueとして例マーク:

If for node_id = 1 
    friend_ids = [10, 20, 30] 
    follower_ids = [11, 21, 31] 

次に、あなたのINSERTクエリは次のようになります。

INSERT INTO user(node_id , friend_follower_id , is_friend) VALUES (1, 10, true); 
INSERT INTO user(node_id , friend_follower_id , is_friend) VALUES (1, 20, true); 
INSERT INTO user(node_id , friend_follower_id , is_friend) VALUES (1, 30, true); 
INSERT INTO user(node_id , friend_follower_id , is_follower) VALUES (1, 11, true); 
INSERT INTO user(node_id , friend_follower_id , is_follower) VALUES (1, 21, true); 
INSERT INTO user(node_id , friend_follower_id , is_follower) VALUES (1, 31, true); 

あなたはすべてのfriendidsとfolloweridsを取得したい場合は、クエリ:

SELECT * FROM user WHERE node_id = 1; 

あなたは、この取得します:

node_id | friend_follower_id | centrality | is_follower | is_friend | last_tweets | screen_name 
---------+--------------------+------------+-------------+-----------+-------------+------------- 
     1 |     10 |  null |  null |  True |  null |  null 
     1 |     11 |  null |  True |  null |  null |  null 
     1 |     20 |  null |  null |  True |  null |  null 
     1 |     21 |  null |  True |  null |  null |  null 
     1 |     30 |  null |  null |  True |  null |  null 
     1 |     31 |  null |  True |  null |  null |  null 

出典:
https://docs.datastax.com/en/cql/3.1/cql/cql_using/use_collections_c.html https://docs.datastax.com/en/cql/3.1/cql/cql_reference/refLimits.html

+0

私はあなたが提案している新しいスキーマを本当に理解していません。私のセットfollower_idsで私は私の作者に従っている人々のすべてのIDを格納していた、私は同じことをしたが、私の著者が従っていた人々を使用して私の設定friend_ids。最終的に私はIDの2つのコレクションを持っていました。 – mel

+0

@melの詳細が私の回答に追加されました –

+0

ありがとうございます。それはコレクションを保存する最善の方法ですか? – mel

関連する問題