Skip to content

Instantly share code, notes, and snippets.

@jacoyutorius
Last active September 12, 2017 16:01
Show Gist options
  • Save jacoyutorius/4b8f265cfb541e342cc7749fd8900610 to your computer and use it in GitHub Desktop.
Save jacoyutorius/4b8f265cfb541e342cc7749fd8900610 to your computer and use it in GitHub Desktop.
Ruby on DynamoDB local
=begin
mkdir dynamodb_local
wget https://s3-ap-northeast-1.amazonaws.com/dynamodb-local-tokyo/dynamodb_local_latest.tar.gz -P dynamodb_local
tar -xzvf dynamodb_local/dynamodb_local_latest.tar.gz -C dynamodb_local
echo '#!/bin/bash\njava -Djava.library.path=./dynamodb_local/DynamoDBLocal_lib -jar dynamodb_local/DynamoDBLocal.jar -sharedDb' >> dynamodb_local/start.sh
chmod 700 start.sh
bash dynamodb_local/start.sh
=end
require "aws-sdk"
require "aws-record"
require "pp"
Aws.config.update(endpoint: "http://localhost:8000")
client = Aws::DynamoDB::Client.new
class Artist
include Aws::Record
set_table_name "Artist"
integer_attr :id, hash_key: true
string_attr :name
end
# create table
unless Artist.table_exists?
migration = Aws::Record::TableMigration.new(Artist, client: client)
migration.create!(
provisioned_throughput: {
read_capacity_units: 5,
write_capacity_units: 2
}
)
migration.wait_until_available
end
artist = Artist.new(id: 1, name: "Primal Scream")
puts artist.save!
artists = Artist.scan
artists.each do |artist|
puts artist.name
end
# # delete table
# if Artist.table_exists?
# migration = Aws::Record::TableMigration.new(Artist, client: client)
# migration.delete!
# end
=begin
mkdir dynamodb_local
wget https://s3-ap-northeast-1.amazonaws.com/dynamodb-local-tokyo/dynamodb_local_latest.tar.gz -P dynamodb_local
tar -xzvf dynamodb_local/dynamodb_local_latest.tar.gz -C dynamodb_local
echo '#!/bin/bash\njava -Djava.library.path=./dynamodb_local/DynamoDBLocal_lib -jar dynamodb_local/DynamoDBLocal.jar -sharedDb' >> dynamodb_local/start.sh
chmod 700 start.sh
bash dynamodb_local/start.sh
=end
require "aws-sdk"
require "aws-record"
require "pp"
Aws.config.update(endpoint: "http://localhost:8000")
client = Aws::DynamoDB::Client.new
class Music
include Aws::Record
set_table_name :Music
string_attr :artist, hash_key: true
string_attr :song_title, range_key: true
string_attr :album_title
boolean_attr :favorite, database_attribute_name: "is_favorite_flag"
end
# create table
unless Music.table_exists?
migration = Aws::Record::TableMigration.new(Music, client: client)
migration.create!(
provisioned_throughput: {
read_capacity_units: 5,
write_capacity_units: 2
}
)
migration.wait_until_available
end
# puts Music.new(artist: "Primal Scream", song_title: "Where The Light Gets In", album_title: "Chaosmosis").save!
# puts Music.new(artist: "Bill Evans", song_title: "Autumn Leaves", album_title: "(1969)Autumn Leaves").save!
# puts Music.new(artist: "Foo Fighters", song_title: "In the Clear", album_title: "Sonic Highways").save!
# puts Music.new(artist: "Foo Fighters", song_title: "I Am a River", album_title: "Sonic Highways").save!
# puts Music.new(artist: "Jun Miyake", song_title: "Alviverde", album_title: "Stolen from trangers(+2)", favorite: true).save!
# musics = Music.scan
# musics.each do |music|
# pp music
# end
# find
pp Music.find(artist: "Bill Evans", song_title: "Autumn Leaves")
# query
params = {
table_name: "Music",
key_conditions: {
"artist" => {
attribute_value_list: ["Foo Fighters"],
comparison_operator: "EQ"
}
# "song_title" => {
# attribute_value_list: ["I Am a River"], # value <Hash,Array,String,Numeric,Boolean,IO,Set,nil>
# comparison_operator: "EQ", # required, accepts EQ, NE, IN, LE, LT, GE, GT, BETWEEN, NOT_NULL, NULL, CONTAINS, NOT_CONTAINS, BEGINS_WITH
# },
}
}
musics = Music.query(params)
musics.each do |row|
p row.song_title
end
# scan
Music.scan.each do |row|
p row.song_title
end
# # delete table
# if Music.table_exists?
# migration = Aws::Record::TableMigration.new(Music, client: client)
# migration.delete!
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment