-
Notifications
You must be signed in to change notification settings - Fork 3
/
dynamodb_global_index.rb
149 lines (132 loc) · 3.85 KB
/
dynamodb_global_index.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'aws-sdk-core'
dynamo_db = Aws::DynamoDB::Client.new(
access_key_id: "test",
secret_access_key: "testtest",
endpoint: 'http://localhost:8000', region: 'ap-northeast-1')
list_tables = dynamo_db.list_tables.data.table_names
dynamo_db.delete_table(table_name: "issues") if list_tables.include? "issues"
result = dynamo_db.create_table(
table_name: "issues",
attribute_definitions: [
{attribute_name: "issue_id", attribute_type: "S"},
{attribute_name: "title", attribute_type: "S"},
{attribute_name: "created_date", attribute_type: "S"},
{attribute_name: "due_date", attribute_type: "S"}
],
key_schema: [
{attribute_name: "issue_id", key_type: "HASH"},
{attribute_name: "title", key_type: "RANGE"}
],
global_secondary_indexes: [
{
index_name: "created_date_index",
key_schema: [
{attribute_name: "created_date", key_type: "HASH"},
{attribute_name: "issue_id", key_type: "RANGE"}
],
projection: {
projection_type: "INCLUDE",
non_key_attributes: ["description", "status"]
},
provisioned_throughput: {
read_capacity_units: 10,
write_capacity_units: 5
}
},
{
index_name: "title_index",
key_schema: [
{attribute_name: "title", key_type: "HASH"},
{attribute_name: "issue_id", key_type: "RANGE"}
],
projection: {
projection_type: "KEYS_ONLY"
},
provisioned_throughput: {
read_capacity_units: 10,
write_capacity_units: 5
}
},
{
index_name: "due_date_index",
key_schema: [
{attribute_name: "due_date", key_type: "HASH"}
],
projection: {
projection_type: "ALL"
},
provisioned_throughput: {
read_capacity_units: 10,
write_capacity_units: 5
}
}
],
provisioned_throughput: {
read_capacity_units: 10,
write_capacity_units: 5
}
)
items = Array.new
items << {"issue_id" => "A-101",
"title" => "Compilation error",
"description" => "Can't compile Project X - bad version number. What does this mean?",
"created_date" => "2013-11-01",
"last_updated_date" => "2013-11-02",
"due_date" => "2013-11-10",
"priority" => 1,
"status" => "Assigned"}
items << {"issue_id" => "A-102",
"title" => "Can't read data file",
"description" => "The main data file is missing, or the permissions are incorrect",
"created_date" => "2013-11-01",
"last_updated_date" => "2013-11-04",
"due_date" => "2013-11-30",
"priority" => 2,
"status" => "In progress"}
items << {"issue_id" => "A-103",
"title" => "Test failure",
"description" => "Functional test of Project X produces errors",
"created_date" => "2013-11-01",
"last_updated_date" => "2013-11-02",
"due_date" => "2013-11-10",
"priority" => 1,
"status" => "In progress"}
items << {"issue_id" => "A-104",
"title" => "Compilation error",
"description" => "Variable 'messageCount' was not initialized.",
"created_date" =>"2013-11-15",
"last_updated_date" => "2013-11-16",
"due_date" => "2013-11-30",
"priority" => 3,
"status" =>"Assigned"}
items << {"issue_id" => "A-105",
"title" => "Network issue",
"description" => "Can't ping IP address 127.0.0.1. Please fix this.",
"created_date" =>"2013-11-15",
"last_updated_date" => "2013-11-16",
"due_date" => "2013-11-19",
"priority" => 5,
"status" =>"Assigned"}
items.each do |i|
dynamo_db.put_item(
table_name: "issues",
item: i
)
end
forum_value = dynamo_db.query(
table_name: "issues",
index_name: "created_date_index",
key_conditions: {
"created_date" => {
attribute_value_list: ["2013-11-01"],
comparison_operator: "EQ"
},
"issue_id" => {
attribute_value_list: ["A-"],
comparison_operator: "BEGINS_WITH"
}
}
# select: "ALL_PROJECTED_ATTRIBUTES",
# return_consumed_capacity: "ALL"
)
puts forum_value.data