BigDataの集め方
              RubyでTwitter Streaming APIを使ってみた


                           @Mahito




12年7月30日月曜日
自己紹介
      {
              名前 : まひと
              ID   : @Mahito
              仕事 : [仮想化, NoSQL, BigData]
              コミュニティ : [Ruby, MongoDB]
              趣味 : [釣り, 熱帯魚, 温泉, 勉強会]
      }
12年7月30日月曜日
本日のお話


              簡単なBigDataの集め方と
              して、Twitterからのデー
              タ収集方法と利用サンプル
              のご紹介


12年7月30日月曜日
BigData時代到来!

          富、名声、力、この世のすべてを手に入れた
          男、海賊王ゴールド・D・ロジャー。彼の死に際
          に放った一言は、人々をデータの海へ駆り立て
          た。
         「オレの利益か?欲しけりゃくれてやる。探せ!
         この世のすべてをそこへ置いてきた!!」
         男たちは会社の利益を目指し、様々なデータを追
         い続ける。

         世はまさに、BigData時代!!

12年7月30日月曜日
            |i
                   \      |.|
                             長いので一言で
                    ト\   /| ト
                    | トヽ   / | | ト
                    | | トヽ\/| | | ト    /
                    | | | ト\≧三ミゞ=イ/
                   ム彡''´ ̄ ̄    ̄ ヽ{__..
                  /             V´
                  ノ  __          ',
               ,. == y ̄, __、\_        )
               |i  }-| ゝ二 |/ ̄ ̄  /ニ,l
               ヽ__ノ/ヾ _ ノ       > }}
                                              世界的ですもんね
                / >≦'__        し /
                 Vて二オカ       (_,/}
                 Yこ二ノ!!|          }
                                              乗るしかない
                  Y͡ 从        ∠)
                  从从从トミ   _.ィニ二 ̄丶
                   ミ三三彡 ' ´      \ \
                                              このビッグデータに
                      /           \ヽ
                    /            ミ;,. ', ',
                     |   _  _ __    \',.',
                    ノ!   | V7\ ´/
                   / l /_ゝ| ト >__/ /
                   |   ヽン ´  ヽー'
                  i|                l
                  |:! ヽ              |
12年7月30日月曜日
                  | ト、 `ミ,            l
そもそもBigDataって?

              BigData自体の定義は曖昧
               データの大きさ
               データの種類
               データ構造が半構造や非構造
              ソーシャルサービスのデータやそれらの複数
              の組み合わせが例にあげられる



12年7月30日月曜日
どうやって集めるの?



              そこでTwitterですよ!



12年7月30日月曜日
ご存知とは思いますが




                 Twitter

12年7月30日月曜日
Twitter

               5億ユーザ(アクティブユーザは1.4億)
               1日あたり4.9億 tweet(日本5970万 tweet)
               1秒辺り7000 tweet(日本1400 tweet)


              余談
              2011年12月9日に放送された「天空の城ラピュ
              タ」の「バルス」の瞬間に25088 tweet/sで当時
              の最高記録を樹立

12年7月30日月曜日
先週の状況

              サマーウォーズ




12年7月30日月曜日
REST APIでデータ収集
              Twitterにはデータ取得用のREST APIが存在
              Requestに応じてJSON形式でデータ取得可能
              それらをWrappingしたライブラリも存在
               Twitter4J(Java)
               Twitter(Ruby)
               Twitter4R(Ruby)
              TwitterクライアントのほとんどはREST APIで
              実装されている(模様)

12年7月30日月曜日
REST APIでデータ収集?


               REST APIでデータ収集できないことはない
               ただし制限として350 request / hour
               Public Time Lineの情報は20 tweet/request
               さらに60秒間キャッシュが効いているため、
               1200 tweet / hourが限界




12年7月30日月曜日
そこでStreaming APIですよ

              APIを利用するとTwitter側からデータが自動
              的に送られてくる(Push式)
              APIの使用制限がない
              Streaming APIの種類は3つ
               Public Stream(Public Time Line)
               User Stream(特定のユーザ対象)
               Site Stream(Webサイト向け)


12年7月30日月曜日
Public Stream

              Public Streamにも3つの種類が存在
               filter(フィルタリングしたPublic TL)
               sample(Public TLの一部)
               firehose(一部企業向け)




12年7月30日月曜日
ブラウザでお試し

              以下のURLにアクセス
               https://stream.twitter.com/1/statuses/sample.json

              自分のTwitterIDとパスワードを入力
              (Basic認証)
              DLが開始される
              (終わらないので満足したら止めること!)



12年7月30日月曜日
Rubyでお試し
              OAuthの設定
               Twitter Developersのサイトで実施
                 https://dev.twitter.com/
                 サインイン
                 My Application
                 Create a new Application
                 入力フォームを埋めて各種Keyを入手


12年7月30日月曜日
Rubyでお試し


              Rubyのライブラリ”TweetStream”を利用
               https://github.com/intridea/tweetstream/

              “gem install tweetstream”でインストール
              TweetStreamのサンプルにそってコーディング




12年7月30日月曜日
サンプルコード(sample)
          1   require 'tweetstream'
          2   require 'yaml'
          3
          4   CONF = YAML::load(open("conf.yaml"))
          5
          6   TweetStream.configure do |config|
          7     config.consumer_key       = CONF["consumer_key"]
          8     config.consumer_secret    = CONF["consumer_secret"]
          9     config.oauth_token        = CONF["token"]
         10     config.oauth_token_secret = CONF["token_secret"]
         11     config.auth_method        = :oauth
         12   end
         13
         14   TweetStream::Client.new.sample do |status|
         15     puts status.text
         16   end



12年7月30日月曜日
サンプルコード(filter)
          1   require 'tweetstream'
          2   require 'yaml'
          3
          4   CONF = YAML::load(open("conf.yaml"))
          5
          6   TweetStream.configure do |config|
          7     config.consumer_key       = CONF["consumer_key"]
          8     config.consumer_secret    = CONF["consumer_secret"]
          9     config.oauth_token        = CONF["token"]
         10     config.oauth_token_secret = CONF["token_secret"]
         11     config.auth_method        = :oauth
         12   end
         13
         14   TweetStream::Client.new.track('Ruby') do |status|
         15     puts status.text
         16   end



12年7月30日月曜日
取得データ(1件)
              {"in_reply_to_user_id_str"=>"455267413", "place"=>nil, "text"=>"@AsantiWheels keep a look out for "Ruby"
              @SEMASHOW this year http:/     /t.co/mDUGAJlR", "contributors"=>nil, "in_reply_to_user_id"=>455267413,
              "id_str"=>"227440608116097024", "coordinates"=>nil, "geo"=>nil, "favorited"=>false, "source"=>"<a href="http:/  /
              twitter.com/download/android" rel="nofollow">Twitter for Android</a>", "possibly_sensitive_editable"=>true,
              "created_at"=>"Mon Jul 23 16:30:42 +0000 2012", "in_reply_to_screen_name"=>"AsantiWheels",
              "in_reply_to_status_id"=>nil, "possibly_sensitive"=>false, "in_reply_to_status_id_str"=>nil, "user"=>{"is_translator"=>false,
              "notifications"=>nil, "profile_use_background_image"=>true, "profile_background_image_url_https"=>"https:/       /
              si0.twimg.com/images/themes/theme1/bg.png", "time_zone"=>nil, "profile_text_color"=>"333333",
              "profile_image_url_https"=>"https:/   /si0.twimg.com/profile_images/2386225418/549WvyXy_normal", "following"=>nil,
              "verified"=>false, "profile_background_image_url"=>"http:/    /a0.twimg.com/images/themes/theme1/bg.png",
              "default_profile_image"=>false, "profile_link_color"=>"0084B4", "description"=>"car crazy Arizona guy, dodge parts monkey,
              Severed Ties O.G. member, owner of Chain Reaction ", "id_str"=>"576656522", "contributors_enabled"=>false,
              "geo_enabled"=>false, "favourites_count"=>0, "followers_count"=>26, "profile_image_url"=>"http:/    /a0.twimg.com/
              profile_images/2386225418/549WvyXy_normal", "follow_request_sent"=>nil, "created_at"=>"Thu May 10 23:21:38 +0000
              2012", "profile_background_color"=>"C0DEED", "profile_background_tile"=>false, "friends_count"=>184, "url"=>nil,
              "show_all_inline_media"=>false, "statuses_count"=>99, "profile_sidebar_fill_color"=>"DDEEF6", "protected"=>false,
              "screen_name"=>"Severed_Joe", "listed_count"=>0, "name"=>"J.M.", "profile_sidebar_border_color"=>"C0DEED",
              "location"=>"Phx, AZ", "id"=>576656522, "default_profile"=>true, "lang"=>"en", "utc_offset"=>nil}, "retweet_count"=>0,
              "id"=>227440608116097024, "entities"=>{"user_mentions"=>[{"indices"=>[0, 13], "id_str"=>"455267413",
              "screen_name"=>"AsantiWheels", "name"=>"Asanti Wheels", "id"=>455267413}, {"indices"=>[41, 50], "id_str"=>"21902226",
              "screen_name"=>"SEMASHOW", "name"=>"SEMA Show", "id"=>21902226}], "media"=>[{"type"=>"photo", "indices"=>[61, 81],
              "display_url"=>"pic.twitter.com/mDUGAJlR", "media_url_https"=>"https:/   /p.twimg.com/AygIBy6CQAA2O3i.jpg",
              "id_str"=>"227440608120291328", "sizes"=>{"small"=>{"resize"=>"fit", "h"=>680, "w"=>284}, "medium"=>{"resize"=>"fit",
              "h"=>1172, "w"=>490}, "large"=>{"resize"=>"fit", "h"=>1172, "w"=>490}, "thumb"=>{"resize"=>"crop", "h"=>150, "w"=>150}},
              "url"=>"http://t.co/mDUGAJlR", "expanded_url"=>"http:/   /twitter.com/Severed_Joe/status/227440608116097024/photo/1",
              "media_url"=>"http:/  /p.twimg.com/AygIBy6CQAA2O3i.jpg", "id"=>227440608120291328}], "hashtags"=>[], "urls"=>[]},
              "retweeted"=>false, "truncated"=>false}




12年7月30日月曜日
まとめ

              Twitter Streaming APIは簡単に利用できる
              Twitter Streaming APIを利用することで、だ
              れでも簡単にBigDataを入手することができる
               BigDataで勝ち残るポイントは
                 データを蓄積すること
                 蓄積したデータを活かすかこと



12年7月30日月曜日
完
12年7月30日月曜日

BigDataの集め方

  • 1.
    BigDataの集め方 RubyでTwitter Streaming APIを使ってみた @Mahito 12年7月30日月曜日
  • 2.
    自己紹介 { 名前 : まひと ID : @Mahito 仕事 : [仮想化, NoSQL, BigData] コミュニティ : [Ruby, MongoDB] 趣味 : [釣り, 熱帯魚, 温泉, 勉強会] } 12年7月30日月曜日
  • 3.
    本日のお話 簡単なBigDataの集め方と して、Twitterからのデー タ収集方法と利用サンプル のご紹介 12年7月30日月曜日
  • 4.
    BigData時代到来! 富、名声、力、この世のすべてを手に入れた 男、海賊王ゴールド・D・ロジャー。彼の死に際 に放った一言は、人々をデータの海へ駆り立て た。 「オレの利益か?欲しけりゃくれてやる。探せ! この世のすべてをそこへ置いてきた!!」 男たちは会社の利益を目指し、様々なデータを追 い続ける。 世はまさに、BigData時代!! 12年7月30日月曜日
  • 5.
                |i      \      |.| 長いので一言で       ト\   /| ト       | トヽ   / | | ト       | | トヽ\/| | | ト    /       | | | ト\≧三ミゞ=イ/      ム彡''´ ̄ ̄    ̄ ヽ{__..     /             V´     ノ  __          ',  ,. == y ̄, __、\_        )  |i  }-| ゝ二 |/ ̄ ̄  /ニ,l  ヽ__ノ/ヾ _ ノ       > }} 世界的ですもんね   / >≦'__        し /    Vて二オカ       (_,/}    Yこ二ノ!!|          } 乗るしかない     Y͡ 从        ∠)     从从从トミ   _.ィニ二 ̄丶      ミ三三彡 ' ´      \ \ このビッグデータに         /           \ヽ       /            ミ;,. ', ',        |   _  _ __    \',.',       ノ!   | V7\ ´/      / l /_ゝ| ト >__/ /      |   ヽン ´  ヽー'     i|                l     |:! ヽ              | 12年7月30日月曜日     | ト、 `ミ,            l
  • 6.
    そもそもBigDataって? BigData自体の定義は曖昧 データの大きさ データの種類 データ構造が半構造や非構造 ソーシャルサービスのデータやそれらの複数 の組み合わせが例にあげられる 12年7月30日月曜日
  • 7.
    どうやって集めるの? そこでTwitterですよ! 12年7月30日月曜日
  • 8.
    ご存知とは思いますが Twitter 12年7月30日月曜日
  • 9.
    Twitter 5億ユーザ(アクティブユーザは1.4億) 1日あたり4.9億 tweet(日本5970万 tweet) 1秒辺り7000 tweet(日本1400 tweet) 余談 2011年12月9日に放送された「天空の城ラピュ タ」の「バルス」の瞬間に25088 tweet/sで当時 の最高記録を樹立 12年7月30日月曜日
  • 10.
    先週の状況 サマーウォーズ 12年7月30日月曜日
  • 11.
    REST APIでデータ収集 Twitterにはデータ取得用のREST APIが存在 Requestに応じてJSON形式でデータ取得可能 それらをWrappingしたライブラリも存在 Twitter4J(Java) Twitter(Ruby) Twitter4R(Ruby) TwitterクライアントのほとんどはREST APIで 実装されている(模様) 12年7月30日月曜日
  • 12.
    REST APIでデータ収集? REST APIでデータ収集できないことはない ただし制限として350 request / hour Public Time Lineの情報は20 tweet/request さらに60秒間キャッシュが効いているため、 1200 tweet / hourが限界 12年7月30日月曜日
  • 13.
    そこでStreaming APIですよ APIを利用するとTwitter側からデータが自動 的に送られてくる(Push式) APIの使用制限がない Streaming APIの種類は3つ Public Stream(Public Time Line) User Stream(特定のユーザ対象) Site Stream(Webサイト向け) 12年7月30日月曜日
  • 14.
    Public Stream Public Streamにも3つの種類が存在 filter(フィルタリングしたPublic TL) sample(Public TLの一部) firehose(一部企業向け) 12年7月30日月曜日
  • 15.
    ブラウザでお試し 以下のURLにアクセス https://stream.twitter.com/1/statuses/sample.json 自分のTwitterIDとパスワードを入力 (Basic認証) DLが開始される (終わらないので満足したら止めること!) 12年7月30日月曜日
  • 16.
    Rubyでお試し OAuthの設定 Twitter Developersのサイトで実施 https://dev.twitter.com/ サインイン My Application Create a new Application 入力フォームを埋めて各種Keyを入手 12年7月30日月曜日
  • 17.
    Rubyでお試し Rubyのライブラリ”TweetStream”を利用 https://github.com/intridea/tweetstream/ “gem install tweetstream”でインストール TweetStreamのサンプルにそってコーディング 12年7月30日月曜日
  • 18.
    サンプルコード(sample) 1 require 'tweetstream' 2 require 'yaml' 3 4 CONF = YAML::load(open("conf.yaml")) 5 6 TweetStream.configure do |config| 7 config.consumer_key = CONF["consumer_key"] 8 config.consumer_secret = CONF["consumer_secret"] 9 config.oauth_token = CONF["token"] 10 config.oauth_token_secret = CONF["token_secret"] 11 config.auth_method = :oauth 12 end 13 14 TweetStream::Client.new.sample do |status| 15 puts status.text 16 end 12年7月30日月曜日
  • 19.
    サンプルコード(filter) 1 require 'tweetstream' 2 require 'yaml' 3 4 CONF = YAML::load(open("conf.yaml")) 5 6 TweetStream.configure do |config| 7 config.consumer_key = CONF["consumer_key"] 8 config.consumer_secret = CONF["consumer_secret"] 9 config.oauth_token = CONF["token"] 10 config.oauth_token_secret = CONF["token_secret"] 11 config.auth_method = :oauth 12 end 13 14 TweetStream::Client.new.track('Ruby') do |status| 15 puts status.text 16 end 12年7月30日月曜日
  • 20.
    取得データ(1件) {"in_reply_to_user_id_str"=>"455267413", "place"=>nil, "text"=>"@AsantiWheels keep a look out for "Ruby" @SEMASHOW this year http:/ /t.co/mDUGAJlR", "contributors"=>nil, "in_reply_to_user_id"=>455267413, "id_str"=>"227440608116097024", "coordinates"=>nil, "geo"=>nil, "favorited"=>false, "source"=>"<a href="http:/ / twitter.com/download/android" rel="nofollow">Twitter for Android</a>", "possibly_sensitive_editable"=>true, "created_at"=>"Mon Jul 23 16:30:42 +0000 2012", "in_reply_to_screen_name"=>"AsantiWheels", "in_reply_to_status_id"=>nil, "possibly_sensitive"=>false, "in_reply_to_status_id_str"=>nil, "user"=>{"is_translator"=>false, "notifications"=>nil, "profile_use_background_image"=>true, "profile_background_image_url_https"=>"https:/ / si0.twimg.com/images/themes/theme1/bg.png", "time_zone"=>nil, "profile_text_color"=>"333333", "profile_image_url_https"=>"https:/ /si0.twimg.com/profile_images/2386225418/549WvyXy_normal", "following"=>nil, "verified"=>false, "profile_background_image_url"=>"http:/ /a0.twimg.com/images/themes/theme1/bg.png", "default_profile_image"=>false, "profile_link_color"=>"0084B4", "description"=>"car crazy Arizona guy, dodge parts monkey, Severed Ties O.G. member, owner of Chain Reaction ", "id_str"=>"576656522", "contributors_enabled"=>false, "geo_enabled"=>false, "favourites_count"=>0, "followers_count"=>26, "profile_image_url"=>"http:/ /a0.twimg.com/ profile_images/2386225418/549WvyXy_normal", "follow_request_sent"=>nil, "created_at"=>"Thu May 10 23:21:38 +0000 2012", "profile_background_color"=>"C0DEED", "profile_background_tile"=>false, "friends_count"=>184, "url"=>nil, "show_all_inline_media"=>false, "statuses_count"=>99, "profile_sidebar_fill_color"=>"DDEEF6", "protected"=>false, "screen_name"=>"Severed_Joe", "listed_count"=>0, "name"=>"J.M.", "profile_sidebar_border_color"=>"C0DEED", "location"=>"Phx, AZ", "id"=>576656522, "default_profile"=>true, "lang"=>"en", "utc_offset"=>nil}, "retweet_count"=>0, "id"=>227440608116097024, "entities"=>{"user_mentions"=>[{"indices"=>[0, 13], "id_str"=>"455267413", "screen_name"=>"AsantiWheels", "name"=>"Asanti Wheels", "id"=>455267413}, {"indices"=>[41, 50], "id_str"=>"21902226", "screen_name"=>"SEMASHOW", "name"=>"SEMA Show", "id"=>21902226}], "media"=>[{"type"=>"photo", "indices"=>[61, 81], "display_url"=>"pic.twitter.com/mDUGAJlR", "media_url_https"=>"https:/ /p.twimg.com/AygIBy6CQAA2O3i.jpg", "id_str"=>"227440608120291328", "sizes"=>{"small"=>{"resize"=>"fit", "h"=>680, "w"=>284}, "medium"=>{"resize"=>"fit", "h"=>1172, "w"=>490}, "large"=>{"resize"=>"fit", "h"=>1172, "w"=>490}, "thumb"=>{"resize"=>"crop", "h"=>150, "w"=>150}}, "url"=>"http://t.co/mDUGAJlR", "expanded_url"=>"http:/ /twitter.com/Severed_Joe/status/227440608116097024/photo/1", "media_url"=>"http:/ /p.twimg.com/AygIBy6CQAA2O3i.jpg", "id"=>227440608120291328}], "hashtags"=>[], "urls"=>[]}, "retweeted"=>false, "truncated"=>false} 12年7月30日月曜日
  • 21.
    まとめ Twitter Streaming APIは簡単に利用できる Twitter Streaming APIを利用することで、だ れでも簡単にBigDataを入手することができる BigDataで勝ち残るポイントは データを蓄積すること 蓄積したデータを活かすかこと 12年7月30日月曜日
  • 22.