如何使用带有laravel的dynamo db?

时间:2016-04-20 11:22:27

标签: php laravel amazon-web-services laravel-5 amazon-dynamodb

您好我是Dynamo db和laravel的新手。在互联网上搜索后,我找到了很多方法来为laravel设置发电机数据库,我尝试了每一种方法。但是在设置之后,为了在laravel中使用dynamo db我找不到任何教程或任何示例代码,以便我可以进一步使用它。 我用于发电机数据库设置的一些站点链接:

https://github.com/aws/aws-sdk-php-laravel

https://github.com/baopham/laravel-dynamodb

https://github.com/aws/aws-sdk-php

http://www.techigniter.in/tutorials/dynamodb-session-driver-for-laravel-5/

还有更多的网站,都使用自己不同的方法,但没有给我足够的帮助。

2 个答案:

答案 0 :(得分:1)

实际上非常简单。 您可以将此SDK用于Laravel:https://github.com/aws/aws-sdk-php-laravel

此链接可以帮助您了解如何使用它:https://stackoverflow.com/a/32594321/9845807

use AWS;
use Config;
public function dynamoDBConnect()
    {
        //Fetching credentials for DynamDB
        $config =  Config::get('aws');

        //Inserting value with tableName: "Logs" and columnName: "userId"
        $client = AWS::createClient('DynamoDb');
        $iterator = $client->getIterator('Query', array(
            'TableName'     => 'Logs',
            'KeyConditions' => array(
                'userId' => array(
                    'AttributeValueList' => array(
                        array('N' => '1202')
                    ),
                    'ComparisonOperator' => 'EQ'
                )
            )
        ));

        //Fetching the records.
        $result = $client->getItem(array(
            'ConsistentRead' => true,
            'TableName' => 'Logs',
            'Key'       => array(
                'userId'   => array('N' => '1201'),
                'Name'    => array('S' => 'Name string')
            )
        ));
    }

答案 1 :(得分:0)

完整文档:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.PHP.html

以下方法将连接到DynamoDB并检索数据。 将ACCESS_KEY,SECRET_KEY,REGION和TABLE_NAME更改为您的密码。

我的DynamoDB数据就像,     {'2019-11-05':'12:30-13:00','2019-11-05':'13:30-14:00'}

public function getDataFromDynamoDb() {
$credentials = new Aws\Credentials\Credentials(ACCESS_KEY, SECRET_KEY);

$client = new Aws\Sdk([
    'version' => 'latest',
    'region' => REGION,
    'credentials' => $credentials
]);

$dynamodb = $client->createDynamoDb();
$marshaler = new Marshaler();

$tableName = 'TABLE_NAME';

$eav = $marshaler->marshalJson('{":Date": "2019-11-05"}');

$params = [
    'TableName' => $tableName,
    'KeyConditionExpression' => '#Date = :Date',
    'ExpressionAttributeNames'=> [ '#Date' => 'Date' ],
    'ExpressionAttributeValues'=> $eav
];

try {

    $result = $dynamodb->query($params);

    foreach ($result['Items'] as $marketData) {
        echo $marshaler->unmarshalValue($marketData['Date']) . ': ' .
            $marshaler->unmarshalValue($marketData['Value']) . "\n";
    }

} catch (DynamoDbException $e) {
    echo "Unable to query:\n";
    echo $e->getMessage() . "\n";
}}