├── .gitignore ├── README.md ├── createtables.php └── uploaddata.php /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### CakePHP template 3 | # CakePHP 3 4 | 5 | /vendor/* 6 | /config/app.php 7 | 8 | /tmp/cache/models/* 9 | !/tmp/cache/models/empty 10 | /tmp/cache/persistent/* 11 | !/tmp/cache/persistent/empty 12 | /tmp/cache/views/* 13 | !/tmp/cache/views/empty 14 | /tmp/sessions/* 15 | !/tmp/sessions/empty 16 | /tmp/tests/* 17 | !/tmp/tests/empty 18 | 19 | /logs/* 20 | !/logs/empty 21 | 22 | # CakePHP 2 23 | 24 | /app/tmp/* 25 | /app/Config/core.php 26 | /app/Config/database.php 27 | /vendors/* 28 | ### FuelPHP template 29 | # the composer package lock file and install directory 30 | # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file 31 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file 32 | # /composer.lock 33 | /fuel/vendor 34 | 35 | # the fuelphp document 36 | /docs/ 37 | 38 | # you may install these packages with `oil package`. 39 | # http://fuelphp.com/docs/packages/oil/package.html 40 | # /fuel/packages/auth/ 41 | # /fuel/packages/email/ 42 | # /fuel/packages/oil/ 43 | # /fuel/packages/orm/ 44 | # /fuel/packages/parser/ 45 | 46 | # dynamically generated files 47 | /fuel/app/logs/*/*/* 48 | /fuel/app/cache/*/* 49 | /fuel/app/config/crypt.php 50 | ### JetBrains template 51 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 52 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 53 | 54 | # User-specific stuff 55 | .idea/**/workspace.xml 56 | .idea/**/tasks.xml 57 | .idea/**/usage.statistics.xml 58 | .idea/**/dictionaries 59 | .idea/**/shelf 60 | 61 | # Sensitive or high-churn files 62 | .idea/**/dataSources/ 63 | .idea/**/dataSources.ids 64 | .idea/**/dataSources.local.xml 65 | .idea/**/sqlDataSources.xml 66 | .idea/**/dynamic.xml 67 | .idea/**/uiDesigner.xml 68 | .idea/**/dbnavigator.xml 69 | 70 | # Gradle 71 | .idea/**/gradle.xml 72 | .idea/**/libraries 73 | 74 | # Gradle and Maven with auto-import 75 | # When using Gradle or Maven with auto-import, you should exclude module files, 76 | # since they will be recreated, and may cause churn. Uncomment if using 77 | # auto-import. 78 | # .idea/modules.xml 79 | # .idea/*.iml 80 | # .idea/modules 81 | 82 | # CMake 83 | cmake-build-*/ 84 | 85 | # Mongo Explorer plugin 86 | .idea/**/mongoSettings.xml 87 | 88 | # File-based project format 89 | *.iws 90 | 91 | # IntelliJ 92 | out/ 93 | 94 | # mpeltonen/sbt-idea plugin 95 | .idea_modules/ 96 | 97 | # JIRA plugin 98 | atlassian-ide-plugin.xml 99 | 100 | # Cursive Clojure plugin 101 | .idea/replstate.xml 102 | 103 | # Crashlytics plugin (for Android Studio and IntelliJ) 104 | com_crashlytics_export_strings.xml 105 | crashlytics.properties 106 | crashlytics-build.properties 107 | fabric.properties 108 | 109 | # Editor-based Rest Client 110 | .idea/httpRequests 111 | 112 | .idea/dynamodb.iml 113 | .idea/inspectionProfiles/ 114 | .idea/misc.xml 115 | .idea/modules.xml 116 | .idea/vcs.xml 117 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dynamodb & PHP 2 | 3 | This is a very simple project showing how to use an SDK to create DynamoDB tables and populate them. 4 | The scripts are intended to be run from an EC2 instance whith a appropriat eRole configured 5 | 6 | This is a student Lab not intened to real world use. 7 | No guaranteer is given or implied. 8 | 9 | Have fun 10 | A Cloud Guru 11 | https://acloud.guru/ 12 | -------------------------------------------------------------------------------- /createtables.php: -------------------------------------------------------------------------------- 1 | 'eu-west-1', // replace with your desired region visit http://docs.aws.amazon.com/general/latest/gr/rande.html to get your regions. 17 | 'version' => '2012-08-10' // Now needs a version 18 | )); 19 | 20 | $tableNames = array(); 21 | 22 | $tableName = 'ProductCatalog'; 23 | echo "Creating table $tableName..." . PHP_EOL; 24 | 25 | $response = $client->createTable(array( 26 | 'TableName' => $tableName, 27 | 'AttributeDefinitions' => array( 28 | array( 29 | 'AttributeName' => 'Id', 30 | 'AttributeType' => 'N' 31 | ) 32 | ), 33 | 'KeySchema' => array( 34 | array( 35 | 'AttributeName' => 'Id', 36 | 'KeyType' => 'HASH' 37 | ) 38 | ), 39 | 'ProvisionedThroughput' => array( 40 | 'ReadCapacityUnits' => 6, 41 | 'WriteCapacityUnits' => 5 42 | ) 43 | )); 44 | $tableNames[] = $tableName; 45 | 46 | $tableName = 'Forum'; 47 | echo "Creating table $tableName..." . PHP_EOL; 48 | 49 | $response = $client->createTable(array( 50 | 'TableName' => $tableName, 51 | 'AttributeDefinitions' => array( 52 | array( 53 | 'AttributeName' => 'Name', 54 | 'AttributeType' => 'S' 55 | ) 56 | ), 57 | 'KeySchema' => array( 58 | array( 59 | 'AttributeName' => 'Name', 60 | 'KeyType' => 'HASH' 61 | ) 62 | ), 63 | 'ProvisionedThroughput' => array( 64 | 'ReadCapacityUnits' => 6, 65 | 'WriteCapacityUnits' => 5 66 | ) 67 | )); 68 | $tableNames[] = $tableName; 69 | 70 | $tableName = 'Thread'; 71 | echo "Creating table $tableName..." . PHP_EOL; 72 | 73 | $response = $client->createTable(array( 74 | 'TableName' => $tableName, 75 | 'AttributeDefinitions' => array( 76 | array( 77 | 'AttributeName' => 'ForumName', 78 | 'AttributeType' => 'S' 79 | ), 80 | array( 81 | 'AttributeName' => 'Subject', 82 | 'AttributeType' => 'S' 83 | ) 84 | ), 85 | 'KeySchema' => array( 86 | array( 87 | 'AttributeName' => 'ForumName', 88 | 'KeyType' => 'HASH' 89 | ), 90 | array( 91 | 'AttributeName' => 'Subject', 92 | 'KeyType' => 'RANGE' 93 | ) 94 | ), 95 | 'ProvisionedThroughput' => array( 96 | 'ReadCapacityUnits' => 6, 97 | 'WriteCapacityUnits' => 5 98 | ) 99 | )); 100 | $tableNames[] = $tableName; 101 | 102 | $tableName = 'Reply'; 103 | echo "Creating table $tableName..." . PHP_EOL; 104 | 105 | $response = $client->createTable(array( 106 | 'TableName' => $tableName, 107 | 'AttributeDefinitions' => array( 108 | array( 109 | 'AttributeName' => 'Id', 110 | 'AttributeType' => 'S' 111 | ), 112 | array( 113 | 'AttributeName' => 'ReplyDateTime', 114 | 'AttributeType' => 'S' 115 | ), 116 | array( 117 | 'AttributeName' => 'PostedBy', 118 | 'AttributeType' => 'S' 119 | ) 120 | ), 121 | 'LocalSecondaryIndexes' => array( 122 | array( 123 | 'IndexName' => 'PostedBy-index', 124 | 'KeySchema' => array( 125 | array( 126 | 'AttributeName' => 'Id', 127 | 'KeyType' => 'HASH' 128 | ), 129 | array( 130 | 'AttributeName' => 'PostedBy', 131 | 'KeyType' => 'RANGE' 132 | ), 133 | ), 134 | 'Projection' => array( 135 | 'ProjectionType' => 'KEYS_ONLY', 136 | ), 137 | ), 138 | ), 139 | 'KeySchema' => array( 140 | array( 141 | 'AttributeName' => 'Id', 142 | 'KeyType' => 'HASH' 143 | ), 144 | array( 145 | 'AttributeName' => 'ReplyDateTime', 146 | 'KeyType' => 'RANGE' 147 | ) 148 | ), 149 | 'ProvisionedThroughput' => array( 150 | 'ReadCapacityUnits' => 6, 151 | 'WriteCapacityUnits' => 5 152 | ) 153 | )); 154 | $tableNames[] = $tableName; 155 | 156 | foreach($tableNames as $tableName) { 157 | echo "Waiting for table $tableName to be created." . PHP_EOL; 158 | $client->waitUntil('TableExists', array('TableName' => $tableName)); // Changed from v2 159 | echo "Table $tableName has been created." . PHP_EOL; 160 | } 161 | 162 | -------------------------------------------------------------------------------- /uploaddata.php: -------------------------------------------------------------------------------- 1 | 'eu-west-1', // replace with your desired region 16 | 'version' => '2012-08-10' // Now needs a version 17 | )); 18 | 19 | # Setup some local variables for dates 20 | 21 | date_default_timezone_set('UTC'); 22 | 23 | $oneDayAgo = date('Y-m-d H:i:s', strtotime('-1 days')); 24 | $sevenDaysAgo = date('Y-m-d H:i:s', strtotime('-7 days')); 25 | $fourteenDaysAgo = date('Y-m-d H:i:s', strtotime('-14 days')); 26 | $twentyOneDaysAgo = date('Y-m-d H:i:s', strtotime('-21 days')); 27 | 28 | $tableName = 'ProductCatalog'; 29 | echo "Adding data to the $tableName table..." . PHP_EOL; 30 | 31 | $response = $client->batchWriteItem(array( 32 | 'RequestItems' => array( 33 | $tableName => array( 34 | array( 35 | 'PutRequest' => array( 36 | 'Item' => array( 37 | 'Id' => array('N' => '1101'), 38 | 'Title' => array('S' => 'Book 101 Title'), 39 | 'ISBN' => array('S' => '111-1111111111'), 40 | 'Authors' => array('SS' => array('Author1')), 41 | 'Price' => array('N' => '2'), 42 | 'Dimensions' => array('S' => '8.5 x 11.0 x 0.5'), 43 | 'PageCount' => array('N' => '500'), 44 | 'InPublication' => array('N' => '1'), 45 | 'ProductCategory' => array('S' => 'Book') 46 | ) 47 | ), 48 | ), 49 | array( 50 | 'PutRequest' => array( 51 | 'Item' => array( 52 | 'Id' => array('N' => '102'), 53 | 'Title' => array('S' => 'Book 102 Title'), 54 | 'ISBN' => array('S' => '222-2222222222'), 55 | 'Authors' => array('SS' => array('Author1', 'Author2')), 56 | 'Price' => array('N' => '20'), 57 | 'Dimensions' => array('S' => '8.5 x 11.0 x 0.8'), 58 | 'PageCount' => array('N' => '600'), 59 | 'InPublication' => array('N' => '1'), 60 | 'ProductCategory' => array('S' => 'Book') 61 | ) 62 | ), 63 | ), 64 | array( 65 | 'PutRequest' => array( 66 | 'Item' => array( 67 | 'Id' => array('N' => '103'), 68 | 'Title' => array('S' => 'Book 103 Title'), 69 | 'ISBN' => array('S' => '333-3333333333'), 70 | 'Authors' => array('SS' => array('Author1', 'Author2')), 71 | 'Price' => array('N' => '2000'), 72 | 'Dimensions' => array('S' => '8.5 x 11.0 x 1.5'), 73 | 'PageCount' => array('N' => '600'), 74 | 'InPublication' => array('N' => '0'), 75 | 'ProductCategory' => array('S' => 'Book') 76 | ) 77 | ), 78 | ), 79 | array( 80 | 'PutRequest' => array( 81 | 'Item' => array( 82 | 'Id' => array('N' => '201'), 83 | 'Title' => array('S' => '18-Bike-201'), 84 | 'Description' => array('S' => '201 Description'), 85 | 'BicycleType' => array('S' => 'Road'), 86 | 'Brand' => array('S' => 'Mountain A'), 87 | 'Price' => array('N' => '100'), 88 | 'Gender' => array('S' => 'M'), 89 | 'Color' => array('SS' => array('Red', 'Black')), 90 | 'ProductCategory' => array('S' => 'Bicycle') 91 | ) 92 | ), 93 | ), 94 | array( 95 | 'PutRequest' => array( 96 | 'Item' => array( 97 | 'Id' => array('N' => '202'), 98 | 'Title' => array('S' => '21-Bike-202'), 99 | 'Description' => array('S' => '202 Description'), 100 | 'BicycleType' => array('S' => 'Road'), 101 | 'Brand' => array('S' => 'Brand-Company A'), 102 | 'Price' => array('N' => '200'), 103 | 'Gender' => array('S' => 'M'), 104 | 'Color' => array('SS' => array('Green', 'Black')), 105 | 'ProductCategory' => array('S' => 'Bicycle') 106 | ) 107 | ), 108 | ), 109 | array( 110 | 'PutRequest' => array( 111 | 'Item' => array( 112 | 'Id' => array('N' => '203'), 113 | 'Title' => array('S' => '19-Bike-203'), 114 | 'Description' => array('S' => '203 Description'), 115 | 'BicycleType' => array('S' => 'Road'), 116 | 'Brand' => array('S' => 'Brand-Company B'), 117 | 'Price' => array('N' => '300'), 118 | 'Gender' => array('S' => 'W'), 119 | 'Color' => array('SS' => array('Red', 'Green', 'Black')), 120 | 'ProductCategory' => array('S' => 'Bicycle') 121 | ) 122 | ), 123 | ), 124 | array( 125 | 'PutRequest' => array( 126 | 'Item' => array( 127 | 'Id' => array('N' => '204'), 128 | 'Title' => array('S' => '18-Bike-204'), 129 | 'Description' => array('S' => '204 Description'), 130 | 'BicycleType' => array('S' => 'Mountain'), 131 | 'Brand' => array('S' => 'Brand-Company B'), 132 | 'Price' => array('N' => '400'), 133 | 'Gender' => array('S' => 'W'), 134 | 'Color' => array('SS' => array('Red')), 135 | 'ProductCategory' => array('S' => 'Bicycle') 136 | ) 137 | ), 138 | ), 139 | array( 140 | 'PutRequest' => array( 141 | 'Item' => array( 142 | 'Id' => array('N' => '205'), 143 | 'Title' => array('S' => '20-Bike-205'), 144 | 'Description' => array('S' => '205 Description'), 145 | 'BicycleType' => array('S' => 'Hybrid'), 146 | 'Brand' => array('S' => 'Brand-Company C'), 147 | 'Price' => array('N' => '500'), 148 | 'Gender' => array('S' => 'B'), 149 | 'Color' => array('SS' => array('Red', 'Black')), 150 | 'ProductCategory' => array('S' => 'Bicycle') 151 | ) 152 | ) 153 | ) 154 | ), 155 | ), 156 | )); 157 | 158 | echo "done." . PHP_EOL; 159 | 160 | 161 | 162 | $tableName = 'Forum'; 163 | echo "Adding data to the $tableName table..." . PHP_EOL; 164 | 165 | $response = $client->batchWriteItem(array( 166 | 'RequestItems' => array( 167 | $tableName => array( 168 | array( 169 | 'PutRequest' => array( 170 | 'Item' => array( 171 | 'Name' => array('S' => 'Amazon DynamoDB'), 172 | 'Category' => array('S' => 'Amazon Web Services'), 173 | 'Threads' => array('N' => '0'), 174 | 'Messages' => array('N' => '0'), 175 | 'Views' => array('N' => '1000') 176 | ) 177 | ) 178 | ), 179 | array( 180 | 'PutRequest' => array( 181 | 'Item' => array( 182 | 'Name' => array('S' => 'Amazon S3'), 183 | 'Category' => array('S' => 'Amazon Web Services'), 184 | 'Threads' => array('N' => '0') 185 | ) 186 | ) 187 | ), 188 | ) 189 | ) 190 | )); 191 | 192 | echo "done." . PHP_EOL; 193 | 194 | 195 | $tableName = 'Reply'; 196 | echo "Adding data to the $tableName table..." . PHP_EOL; 197 | 198 | $response = $client->batchWriteItem(array( 199 | 'RequestItems' => array( 200 | $tableName => array( 201 | array( 202 | 'PutRequest' => array( 203 | 'Item' => array( 204 | 'Id' => array('S' => 'Amazon DynamoDB#DynamoDB Thread 1'), 205 | 'ReplyDateTime' => array('S' => $fourteenDaysAgo), 206 | 'Message' => array('S' => 'DynamoDB Thread 1 Reply 2 text'), 207 | 'PostedBy' => array('S' => 'User B') 208 | ) 209 | ) 210 | ), 211 | array( 212 | 'PutRequest' => array( 213 | 'Item' => array( 214 | 'Id' => array('S' => 'Amazon DynamoDB#DynamoDB Thread 2'), 215 | 'ReplyDateTime' => array('S' => $twentyOneDaysAgo), 216 | 'Message' => array('S' => 'DynamoDB Thread 2 Reply 3 text'), 217 | 'PostedBy' => array('S' => 'User B') 218 | ) 219 | ) 220 | ), 221 | array( 222 | 'PutRequest' => array( 223 | 'Item' => array( 224 | 'Id' => array('S' => 'Amazon DynamoDB#DynamoDB Thread 2'), 225 | 'ReplyDateTime' => array('S' => $sevenDaysAgo), 226 | 'Message' => array('S' => 'DynamoDB Thread 2 Reply 2 text'), 227 | 'PostedBy' => array('S' => 'User A') 228 | ) 229 | ) 230 | ), 231 | array( 232 | 'PutRequest' => array( 233 | 'Item' => array( 234 | 'Id' => array('S' => 'Amazon DynamoDB#DynamoDB Thread 2'), 235 | 'ReplyDateTime' => array('S' => $oneDayAgo), 236 | 'Message' => array('S' => 'DynamoDB Thread 2 Reply 1 text'), 237 | 'PostedBy' => array('S' => 'User A') 238 | ) 239 | ) 240 | ) 241 | ), 242 | ) 243 | )); 244 | 245 | echo "done." . PHP_EOL; 246 | --------------------------------------------------------------------------------