Skip to content

Instantly share code, notes, and snippets.

@arfon
Created January 21, 2012 15:41
Show Gist options
  • Save arfon/1653103 to your computer and use it in GitHub Desktop.
Save arfon/1653103 to your computer and use it in GitHub Desktop.
Sample queries
require 'aws-sdk'
AWS_ACCESS_KEY = 'your_aws_access_key'
AWS_SECRET_KEY = 'your_aws_secret_key'
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
# Count all records
puts table.items.count
=> 13356
# Find all tweets by a particular user in a date range - note that DynamoDB only supports two scalar data types: Number
# and String (as well as multi-valued types: String Set and Number Set. Date types are not supported so we're
# storing dates as UNIX time.
# Time.at(1326931200).utc -> 2012-01-19 00:00:00 UTC
# Time.at(1327190400).utc -> 2012-01-22 00:00:00 UTC
# User id: 66557411
results = table.items.count(:hash_value => 66557411, :range_value => 1326931200..1327190400)
=> #<Enumerator: <AWS::DynamoDB::ItemCollection>:each({:query=>true, :hash_key_value=>{:n=>"66557411"}, :range_key_condition=>{:attribute_value_list=>[{:n=>"1326931200"}, {:n=>"1327190400"}], :comparison_operator=>"BETWEEN"}})>
# Print out matching tweets
results.each {|r| puts r.attributes.to_h}
{"created_at"=>#<BigDecimal:7ff484277628,'0.1327114219E10',18(27)>, "id"=>#<BigDecimal:7ff484277380,'0.1605547898 5019392E18',18(27)>, "screen_name"=>"CrystalDione", "user_id"=>#<BigDecimal:7ff484277088,'0.66557411E8',9(18)>}
# Query non-indexed attributes using the Scan API and the aws-sdk gem filter builder.
# Finding tweets where the user's screen name contains 'jimmy'
table.items.where(:screen_name).contains("jimmy").count
=> 10
# And where it doesn't
table.items.where(:screen_name).does_not_contain("jimmy").count
=> 13346
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment