DynamoDb - 整数无法转换为字符串

5

我是dynamoDB的新手,正在努力填充我的DynamoDB表格。我已经定义了这个表格如下:

    $response = $DynamoDb->createTable([
        'TableName' => 'products',
        'AttributeDefinitions' => [
            [
                'AttributeName' => 'product_code',
                'AttributeType' => 'N'
            ],
            [
                'AttributeName' => 'created_at',
                'AttributeType' => 'N'
            ],
        ],
        'KeySchema' => [
            [
                'AttributeName' => 'product_code',
                'KeyType' => 'HASH' 
            ],
            [
                'AttributeName' => 'created_at',
                'KeyType' => 'RANGE' 
            ]
        ],
        'ProvisionedThroughput' => [
            'ReadCapacityUnits'    => 5,
            'WriteCapacityUnits' => 6
        ]
    ]);

并使用以下内容进行填充

    $result = $DynamoDb->putItem([
      'TableName' => 'products',
        'Item' => [
          'product_code' => ['N'  =>  (int)$row[0]],
          'created_at' => ['N' => (int)strtotime("now")]
        ]
    ]);

I keep getting the error

Integer can not be converted to an String

请问有谁能给新手一些建议。非常感谢。

2个回答

14

您应该将字符串值插入到Dynamo,如果该行的类型为'N',则Dynamo将其转换为整数。

"N": "string", 

 $result = $DynamoDb->putItem([
          'TableName' => 'products',
            'Item' => [
              'product_code' => ['N'  =>  (str)$row[0]],
              'created_at' => ['N' => (str)strtotime("now")]
            ]
        ]);

来自文档

Item

Each element in the Item map is an AttributeValue object.

**Type: String to AttributeValue object map**

4
$result = $DynamoDb->putItem([
      'TableName' => 'products',
        'Item' => [
          'product_code' => ['N'  =>  (string)$row[0]],
          'created_at' => ['N' => (string)time()]
        ]
    ]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接