/** * {@inheritDoc} */ public function exists($entityClass, $primaryKey, array $commandOptions = array()) { $entity = $this->initEntity($entityClass, $primaryKey); $commandOptions = array('ConsistentRead' => $this->consistentRead, 'TableName' => $this->getEntityTable($entity), 'Key' => $this->formatKeyCondition($entity), 'ReturnConsumedCapacity' => $this->returnConsumedCapacity); $model = $this->dynamoDb->getItem($commandOptions); return isset($model['Item']); }
/** * Retrieve single result using hash_key and range_key * * @param string $hash_key_value * @param string $range_key_value * @param array $options * * @return \Kettle\ORM instance of the ORM sub class */ public function findOne($hash_key_value, $range_key_value = null, array $options = array()) { $conditions = array($this->_hash_key => $hash_key_value); if ($range_key_value) { if (!$this->_range_key) { throw new \Exception("Range key is not defined."); } $conditions[$this->_range_key] = $range_key_value; } $key = $this->_formatAttributes($conditions); $args = array('TableName' => $this->_table_name, 'Key' => $key, 'ConsistentRead' => $this->_consistent_read, 'ReturnConsumedCapacity' => 'TOTAL'); // Merge $options to $args $option_names = array('AttributesToGet', 'ReturnConsumedCapacity'); foreach ($option_names as $option_name) { if (isset($options[$option_name])) { $args[$option_name] = $options[$option_name]; } } $item = self::$_client->getItem($args); self::_logQuery('getItem', $args, $item); if (!is_array($item['Item'])) { return null; } $result = $this->_formatResult($item['Item']); $class_name = get_called_class(); $instance = self::factory($class_name); $instance->hydrate($result); return $instance; }
/** * Get an item via the get_item call * @param string $table The item table * @param mixed $hash The primary hash key * @param mixed|null $range The primary range key * @param Context\Get|null $context The call context * @return Item|null */ public function get($table, $hash, $range = null, Context\Get $context = null) { if (null !== $this->logger) { $this->log('Get on table ' . $table); } // Primary key $hash = new Attribute($hash); $parameters = array('TableName' => $table, 'Key' => array('HashKeyElement' => $hash->getForDynamoDB())); // Range key if (null !== $range) { $range = new Attribute($range); $parameters['Key']['RangeKeyElement'] = $range->getForDynamoDB(); } if (null !== $context) { $parameters += $context->getForDynamoDB(); } if (null !== $this->logger) { $this->log('Get request paramaters : ' . print_r($parameters, true), Logger::DEBUG); } $response = $this->connector->getItem($parameters); if (null !== $this->logger) { $this->log('Get request response : ' . print_r($response, true), Logger::DEBUG); } $this->addConsumedReadUnits($table, floatval($response['ConsumedCapacityUnits'])); if (isset($response['Item'])) { $item = new Item($table); $item->populateFromDynamoDB($response['Item']); return $item; } else { if (null !== $this->logger) { $this->log('Didn\'t find item'); } return null; } }
/** * Get Item data * * @param $id * @return array|bool|stdClass */ public function getData($id) { if (empty($this->table)) { return false; } /** * Get item */ $result = $this->client->getItem(['TableName' => $this->table, 'Key' => ['id' => ['S' => $id]]]); /** * Un Marshal Item */ $marshaler = new Marshaler(); $data = $marshaler->unmarshalItem($result['Item']); return $data; }
/** * Returns the value associated with field in the hash stored at key * * @param string $key * The desired key to get a hash value from * @param string $field * The name of the hash field to get * @return mixed The value stored at the specified hash field */ public function hGet($key, $field) { $result = $this->client->getItem(array('TableName' => $this->tableName, 'Key' => array(self::SIMPLE_KEY_NAME => array('S' => $key)), 'ConsistentRead' => true, 'AttributesToGet' => array(self::HPREFIX . $field))); if (isset($result['Item'][self::HPREFIX . $field])) { return base64_decode($result['Item'][self::HPREFIX . $field]['B']); } else { return false; } }
public function testDeleteRecord() { $seed = $this->seed(); $seedId = array_get($seed, 'id.S'); $this->testModel->find($seedId)->delete(); $query = ['TableName' => $this->testModel->getTable(), 'Key' => ['id' => ['S' => $seedId]]]; $record = $this->dynamoDbClient->getItem($query)->toArray(); $this->assertArrayNotHasKey('Item', $record); }
/** * @param $key ** get and getValue methods return false on non existent key* get and getValue methods return false on non existent key * @return int|string|bool */ public function getValue($key) { $response = $this->dynamoDbClient->getItem(['TableName' => $this->tableName, 'Key' => [$this->attribute => [$this->getVariableType($key) => (string) $key]]]); if ($response->hasKey('Item')) { $array = $response['Item']['status']; $value = reset($array); $key = key($array); return $this->getCastedResponse($key, $value); } else { return false; } }
public function testGetItem() { try { $result = self::$client->getItem(['TableName' => self::$tableName, 'ConsistentRead' => true, 'Key' => ['id' => ['S' => $this->message->get('id')->toString()]]]); } catch (\Exception $e) { $this->fail($e->getMessage()); return; } $this->assertSame($result['Item']['id']['S'], $this->message->get('id')->toString()); $message = $this->marshaler->unmarshal($result['Item']); foreach ($this->message->schema()->getFields() as $field) { $expected = $this->message->get($field->getName()); $actual = $message->get($field->getName()); if ($field->isASet()) { sort($expected); sort($actual); } $this->assertSame(json_encode($expected), json_encode($actual)); } //echo json_encode($message, JSON_PRETTY_PRINT); }
public function get(array $keys, $is_consistent_read = false) { $keyItem = DynamoDbItem::createFromArray($keys, $this->attributeTypes); $requestArgs = ["TableName" => $this->tableName, "Key" => $keyItem->getData()]; if ($is_consistent_read) { $requestArgs["ConsistentRead"] = true; } $result = $this->dbClient->getItem($requestArgs); if ($result['Item']) { $item = DynamoDbItem::createFromTypedArray((array) $result['Item']); return $item->toArray(); } else { return null; } }
/** * @depends testCreatesTable */ public function testAddsGetsAndDeletesItems() { $attributes = $this->client->formatAttributes(array('foo' => 'Test', 'bar' => 10, 'baz' => 'abc')); self::log('Adding an item to the table: ' . var_export($attributes, true)); $result = $this->client->putItem(array('TableName' => $this->table, 'Item' => $attributes)); $this->assertTrue(isset($result['ConsumedCapacityUnits'])); self::log('Getting the item'); // Get the item using the formatAttributes helper $result = $this->client->getItem(array('TableName' => $this->table, 'Key' => $this->client->formatAttributes(array('HashKeyElement' => 'Test', 'RangeKeyElement' => 10)), 'ConsistentRead' => true)); $this->assertEquals('Test', $result['Item']['foo']['S']); $this->assertEquals(10, $result['Item']['bar']['N']); $this->assertEquals('abc', $result['Item']['baz']['S']); self::log('Deleting the item'); $result = $this->client->deleteItem(array('TableName' => $this->table, 'Key' => $this->client->formatAttributes(array('HashKeyElement' => 'Test', 'RangeKeyElement' => 10)))); $this->assertTrue(isset($result['ConsumedCapacityUnits'])); }
/** * The GetItem operation returns a set of attributes for the item with the given primary key. * If there is no matching item, GetItem does not return any data. * * @param string $tableName The name of the table containing the requested item. * @param array $key Associative array of <AttributeName> keys mapping to (associative-array) values. * @param array $attributesToGet The names of one or more attributes to retrieve. If no attribute names are specified, then all attributes will be returned. * @param boolean $consistentRead If set to true, then the operation uses strongly consistent reads; otherwise, eventually consistent reads are used. * @param string $returnConsumedCapacity Sets consumed capacity return mode. * * @return Guzzle\Service\Resource\Model * * @see http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.DynamoDb.DynamoDbClient.html#_getItem */ public function getItem($tableName, array $key, array $attributesToGet = null, $consistentRead = false, $returnConsumedCapacity = self::CAPACITY_NONE) { $args = ['TableName' => $tableName, 'Key' => $key, 'ConsistentRead' => $consistentRead, 'ReturnConsumedCapacity' => $returnConsumedCapacity]; if ($attributesToGet !== null) { $args['AttributesToGet'] = $attributesToGet; } // Try to load from cache is a cacheService is available if ($this->cacheService !== null) { $cacheKey = $this->generateCacheKey($args); if ($this->cacheService->has($cacheKey)) { return unserialize($this->cacheService->get($cacheKey)); } } // Sends getItem command to AWS $result = $this->client->getItem($args); // Saves to cache if ($this->cacheService !== null) { $this->cacheService->set($cacheKey, serialize($result), $this->requestTtl); } return $result; }
public function getData($id) { $r = $this->dynamoDb->getItem(array('ConsistentRead' => true, 'TableName' => $this->tableName, 'Key' => array('id' => array('S' => (string) $id)))); return $r['Item']; }