Skip to content

Instantly share code, notes, and snippets.

@fukata
Created August 1, 2012 04:40
Show Gist options
  • Save fukata/3223702 to your computer and use it in GitHub Desktop.
Save fukata/3223702 to your computer and use it in GitHub Desktop.
import mongodb from treasure data.
#!/usr/bin/env ruby
require 'td'
require 'td-client'
require 'mongo'
WEEK = 604800
cln = TreasureData::Client.new(ENV['TREASURE_DATA_API_KEY'])
job = cln.query('agesage', "select v['query'] as keyword, count(*) as cnt from queries where v['query'] != '' and v['time'] > unix_timestamp() - #{WEEK} group by v['query']")
until job.finished?
sleep 2
job.update_status!
end
@conn = Mongo::Connection.new
@db = @conn['asg']
@coll = @db['query_rankings']
if job.success?
@coll.remove
job.result_each { |row|
@coll.insert({'keyword' => row[0], 'count' => row[1]})
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment