2012-03-24 58 views
0

按照本教程(http://arfon.org/twitter-streaming-with-eventmachine-and-dynamodb)尝试启动Amazon监听环境。接收特定的推文并将它们添加到一个Dynamo数据库(通过一个新线程)。在spawn_threadpool中使用Eventmachine gem - > block

require 'aws-sdk' 
require 'eventmachine' 
require 'tweetstream' 

AWS_ACCESS_KEY = 'HERE IT IS' 
AWS_SECRET_KEY = 'YES HERE' 


dynamo_db = AWS::DynamoDB.new(:access_key_id => AWS_ACCESS_KEY, :secret_access_key => AWS_SECRET_KEY) 

table = dynamo_db.tables['tweets'] 
table.load_schema 

TweetStream.configure do |config| 
    config.username = 'geezlouis' 
    config.password = 'password' 
    config.auth_method = :basic 
end 

EM.run{ 
    client = TweetStream::Client.new 

    def write_to_dynamo(status) 
    EM.defer do 
     tweet_hash = {:user_id => status.user.id, 
        :created_at => status.created_at, 
        :id => status.id, 
        :screen_name => status.user.screen_name} 

     begin 
     table.items.create(tweet_hash) 
     rescue Exception => e 
     puts e.message 
     puts e.backtrace.inspect 
     end 
    end 
    end 

    client.track("Romney", "Gingrich") do |status| 
    write_to_dynamo(status) 
    end 
} 

未定义的局部变量或方法'表“主:对象

["tweets.rb:31:in `block in write_to_dynamo'", "/.rvm/gems/ruby-1.9.3-p125/gems/eventmachine-0.12.10/lib/eventmachine.rb:1060:in 

call'", "/.rvm/gems/ruby-1.9.3-p125/gems/eventmachine-0.12.10/lib/eventmachine.rb:1060:in 块spawn_threadpool””使用EventMachine的时候产卵线程我碰到下面的一切工作但罚款]

回答

0

表是一个局部变量,所以你不能从方法范围中得到它。

你想以下几点:

require 'aws-sdk' 
require 'eventmachine' 
require 'tweetstream' 

AWS_ACCESS_KEY = 'HERE IT IS' 
AWS_SECRET_KEY = 'YES HERE' 


dynamo_db = AWS::DynamoDB.new(:access_key_id => AWS_ACCESS_KEY, :secret_access_key => AWS_SECRET_KEY) 

table = dynamo_db.tables['tweets'] 
table.load_schema 

TweetStream.configure do |config| 
    config.username = 'geezlouis' 
    config.password = 'password' 
    config.auth_method = :basic 
end 

EM.run{ 
    client = TweetStream::Client.new 

    def write_to_dynamo(status, table) 
    EM.defer do 
     tweet_hash = {:user_id => status.user.id, 
        :created_at => status.created_at, 
        :id => status.id, 
        :screen_name => status.user.screen_name} 

     begin 
     table.items.create(tweet_hash) 
     rescue Exception => e 
     puts e.message 
     puts e.backtrace.inspect 
     end 
    end 
    end 

    client.track("Romney", "Gingrich") do |status| 
    write_to_dynamo(status, table) 
    end 
} 

您也可能会希望停止捕获“异常”,而是赶上“StandardError的”,否则你将无法例如CTRL + C程序。

+0

我想我错过了什么 - >看起来像我提交的确切粘贴。 – 2012-03-30 22:51:41