DynamoDB + CakePHP3ã§ãã¼ã¿ãä¿åãã¦ã¿ã
ã¯ããã«
RSSãªã¼ãã¼ã ã®è¨äºè§£æã ã®ãããããã£ã¦ã¿ããï¼AWSã®ãåå¼·ããã¦ã¿ããããã»ã¨ã¼ã³ã§ããããã°ããã
ãªãã¹ãççµåãªè¨è¨ãç®æãã¦ã
- RSSãã£ã¼ãã¸ã®ã¢ã¯ã»ã¹
- ãã¼ã¹ãã¦ã»ãã»ããã
ãã®è¾ºã¯åé¢ãããã¦ãRSSãã£ã¼ããä½ãèããã«ã¶ã¡è¾¼ãå ã¨ãã¦DynamoDBã¨ãã¦æ¡ç¨ãã¦ã¿ã¾ããã
ãã ãDynamoDB + CakePHP3ã§ããããã®ã ããã©ãAWS SDK for PHPã®æ å ±ãå¤ããã¼ã¸ã§ã³ã®ãã®ã ã£ãããã¦ãã¦ããã£ãã®ã§ã¡ã¢æ®ãã¾ãã
ç°å¢
ã³ã¼ã
è¨å®ãã¡ã¤ã«
/** * * AWS SDK Setting * */ 'Aws' => [ 'includes' => ['_aws'], 'services' => [ 'default_settings' => [ 'params' => [ 'version' => '2012-08-10',//dynamo DBã®å ´åã¯ãã®æ¥ä» 'region' => 'us-east-1', //å©ç¨ãã¦ãããªã¼ã¸ã§ã³ 'credentials' => [ 'secret' => '<AWSã³ã³ã½ã¼ã«ããåå¾ãããã®>', 'key' => '<AWSã³ã³ã½ã¼ã«ããåå¾ãããã®>', ] ] ] ] ]
ãããAPPROOT/config/app.phpã«è¿½å ãã¾ãã
ã©ããã¼ã¯ã©ã¹
<?php namespace App\Model\Table; //ããã¯åèªã®è¨è¨ææ³ã«å¿ãã¦æ±ºãã¦ãã ãã use Cake\Core\Configure; use Aws\Sdk; use Aws\DynamoDb\DynamoDbClient; use Aws\DynamoDb\Exception\DynamoDbException; use Aws\DynamoDb\Marshaler; //Exceptionã¯ä½ä½¿ããè¦ç´ããæ¹ããããã use \Exception; class AwsDynamoDb{ protected $_table_name = null; protected $_pk_name = null; protected $_pk_type = null; //"int" or "string" protected $_sdk = null; protected $_dynamodb = null; protected $_marshaler = null; const TYPE_NAME_INT = 'int'; const TYPE_NAME_STRING = 'string'; public function __construct(String $table_name, String $pk_name, String $pk_type){ $this->_table_name = $table_name; $this->_pk_name = $pk_name; //Dynamo DBã¸ã®ãªã¯ã¨ã¹ãJSONã¯ãPKã®åãééã£ã¦ãã¨ã¨ã©ã¼ã¨ãªãããã //ãããããã¯ããã¦ããããã if($pk_type != self::TYPE_NAME_INT && $pk_type != self::TYPE_NAME_STRING){ throw new Exception('Invalid Dynamo DB Primary Key Type Setting.'); }else{ $this->_pk_type = $pk_type; } try{ //SDKã®ã»ããã£ã³ã° $this->_sdk = new Sdk(Configure::read('Aws.services.default_settings.params')); //DynamoDBã®ã»ããã£ã³ã° $this->_dynamodb = $this->_sdk->createDynamoDb(); //Mashalerã®newããã®ã¯ã©ã¹ã使ã£ã¦ãjsonãDynamoDBã§ããæãã«æ ¼ç´ãããªãã¸ã§ã¯ãã«ããã $this->_marshaler = new Marshaler(); }catch(DynamoDbException $e1){ //ã¨ããããå¼ã³åºãå ã«ã¶ãæãããã¨ã§èããã throw $e1; }catch(Exception $e2){ //ã¨ããããå¼ã³åºãå ã«ã¶ãæãããã¨ã§èããã throw $e2; } } /** * putItemã®ã©ããã¼ */ public function put($pk, $body){ if(!$this->_check_pk_type($pk)) throw new Exception('Invalid Dynamo DB Primary Key Type.'); $item_array = array_merge([$this->_pk_name => $pk],$body); //æåã³ã¼ãã«ã¤ãã¦ã¯ãã¨ã§èãã $item_json = json_encode($item_array); //DynamoDBãã¼ãã«åã¨æ ¼ç´ãã¼ã¿ãã»ãã // $item = [ 'TableName' => $this->_table_name, 'Item' => $this->_marshaler->marshalJson($item_json) ]; //putItemå®è¡ $result = $this->_dynamodb->putItem($item); } //Dynamo DBã¸ã®ãªã¯ã¨ã¹ãJSONã¯ãPKã®åãééã£ã¦ãã¨ã¨ã©ã¼ã¨ãªããã private function _check_pk_type($pk){ if($this->_pk_type==self::TYPE_NAME_INT){ if(!is_int($pk)){ return false; } }elseif($this->_pk_type==self::TYPE_NAME_STRING){ if(!is_string($pk)){ return false; } }else{ //é常ããããªã throw new Exception('Invalid Dynamo DB Primary Key Type Setting.'); } return true; } }
å¼ã³åºãæ¹
$dynamo = new AwsDynamoDb('<AWSã³ã³ã½ã¼ã«ã§ä½ã£ããã¼ãã«å>','<AWSã³ã³ã½ã¼ã«ã§ä½ã£ãPrimary Key>',AwsDynamoDb::TYPE_NAME_INT); $dynamo->put(10,['info'=>'aaa']);
çµæ
ä¸æãå ¥ãã¾ããã
ããã£ãã¨ãã
è¨å®ãã¡ã¤ã«ã®æ§é
å¤ãSDKã ã¨
'params' => [ 'version' => 'latest', 'region' => 'us-east-1', //å©ç¨ãã¦ãããªã¼ã¸ã§ã³ 'secret' => '<AWSã³ã³ã½ã¼ã«ããåå¾ãããã®>', 'key' => '<AWSã³ã³ã½ã¼ã«ããåå¾ãããã®>', ]
ããããæ¸ãæ¹ãããã
- secetã¨keyãèªèãããªãã¦å°ã£ããcredentialsã®ä¸ã«å ¥ããªãã¨ãããªããããã
- versionãæ¥ä»ãããªãã¨ãã¡ã¨æããã
ã¡ãªã¿ã«ãversionã¯
ãã¡ãã§ç¢ºèªã§ãã¾ããDynamoDBã¯2012-08-10 ã¨ãã®ã¨ã
Amazon DynamoDB Aws\DynamoDb\DynamoDbClient 2012-08-10
ã¨æ¸ãã¦ããã¾ãã
Aws/Sdkã使ã
å¤ãSDKã¯Aws/Common/Awsã¯ã©ã¹ããã¯ã©ã¤ã¢ã³ããçæãã¦ããããã§ãåèããã°ã®ãããã¦use Aws/Common/Awsã¨ããã¦ãnot foundããã£ã¦æ²ããæãããã¦ããã
Aws/Common/Awsã¯å»æ¢ããã¦ãAws/Sdkã¯ã©ã¹ã使ãã£ã¦ãã¨ã«ãªã£ã¦ãã¿ããã§ããã