Php 检查DynamoDB中是否存在表的最佳方法是什么?

Php 检查DynamoDB中是否存在表的最佳方法是什么?,php,amazon-web-services,nosql,amazon-dynamodb,Php,Amazon Web Services,Nosql,Amazon Dynamodb,检查DynamoDb中是否存在表的最佳方法是什么 如果代码是PHP,我将不胜感激 活动或不活动 *稍后添加错误代码400的各种情况作为示例 检查表是否存在非常容易,它可以有以下之一 TableStatus=>创建、激活、删除或更新 但如果我得到错误400,它可能意味着不止一件事 1) 错误地将空字符串作为表名发送 [x-aws-body]=>{“表名”:“”} ) 2) 发送到DynamoDB的命令中存在语法错误,例如,写入tabel_name而不是table_name [x-aws-body]

检查DynamoDb中是否存在表的最佳方法是什么

如果代码是PHP,我将不胜感激

活动或不活动

*稍后添加错误代码400的各种情况作为示例

检查表是否存在非常容易,它可以有以下之一 TableStatus=>创建、激活、删除或更新

但如果我得到错误400,它可能意味着不止一件事

1) 错误地将空字符串作为表名发送

[x-aws-body]=>{“表名”:“”} )

2) 发送到DynamoDB的命令中存在语法错误,例如,写入tabel_name而不是table_name

[x-aws-body]=>{“TabelName”:“test7”} )

3) 我猜,但没有检查,如果我在同一时间超过了表中提供的容量。

您可以查看官方PHP SDK的“描述表”400表示“不存在””官方文件中有一个相当广泛的例子。看看它在底部的“delete”示例中是如何使用的

下面是文档中的(剥离)示例

<?php
require_once dirname(__FILE__) . '/sdk/sdk.class.php';

$dynamodb = new AmazonDynamoDB();
$table_name = 'ExampleTable';
$response = $dynamodb->describe_table(array('TableName' => $table_name));

if((integer) $response->status !== 400)
{
    $error_type = $response->body->__type;
    $error_code = explode('#', $error_type)[1];
    if($error_code == 'ResourceNotFoundException')
    {
        echo "Table ".$table_name." exists.";
    }
}
?>

使用DynamoDB,您需要解析错误消息的内容,以了解收到的错误类型,因为状态代码几乎总是400。下面是一个示例函数,可用于确定表是否存在。它还允许您指定一个状态,如果您想检查它是否存在以及它是否处于某种状态

<?php

function doesTableExist(AmazonDynamoDB $ddb, $tableName, $desiredStatus = null)
{
    $response = $ddb->describe_table(array('TableName' => $tableName));

    if ($response->isOK()) {
        if ($desiredStatus) {
            $status = $response->body->Table->TableStatus->to_string();
            return ($status === $desiredStatus);
        } else {
            return true;
        }
    } elseif ($response->status === 400) {
        $error = explode('#', $response->body->__type->to_string());
        $error = end($error);
        if ($error === 'ResourceNotFoundException') {
            return false;
        }
    }

    throw new DynamoDB_Exception('Error performing the DescribeTable operation.');
}

如果您只想知道该表是否存在,则上述答案是正确的。我想在这里提出另一个有用的观点,以防万一

在多线程或生产级代码中应该非常小心


假设有一个线程删除了该表,那么在回答第二个线程的查询时,您仍然会得到该表存在的答案,直到该表被完全删除为止。在这种情况下,一旦删除表,第二线程中的表句柄就变成僵尸,就像C++中的悬空指针错误。p> 我认为用
descripbetable
解决这个问题的答案是好的,但是如果在状态码响应上胡闹,代码的可读性就会降低,也会变得更加混乱

我选择使用
listTables
检查表是否存在。以下是

$tableName = 'my_table';

$client = DynamoDbClient::factory(array('region' => 'us-west-2'));

$response = $client->listTables();

if (!in_array($tableName, $response['TableNames'])) {
    // handle non-existence.
    // throw an error if you want or whatever
}

// handle existence
echo "Table " . $tableName . " exists";

其中一些答案使用了旧的SDK,因此我想我应该用我编写的代码更新这个有用的问题,并且效果很好。新的异常确实使这项任务更容易。此函数提供了一个很好的布尔值,可用于脚本中

use Aws\DynamoDb\Exception\ResourceNotFoundException; // <-- make sure this line is at the top

    public function TableExists($tableName) {

    $ddb = DynamoDbClient::factory(array('region' => 'us-east-1')); // EC2 role security

    try {
        $result = $ddb->describeTable(array(
            "TableName" => $tableName
        ));
    } catch (ResourceNotFoundException $e) {
        // if this exception is thrown, the table doesn't exist
        return false;
    }

    // no exception thrown? table exists!
    return true;
}
使用Aws\DynamoDb\Exception\ResourceNotFoundException;//”us-east-1');//EC2角色安全
试一试{
$result=$ddb->可描述(数组)(
“TableName”=>$TableName
));
}捕获(ResourceNotFoundException$e){
//如果引发此异常,则该表不存在
返回false;
}
//没有抛出异常?表存在!
返回true;
}

希望这段完整的工作代码能对您中的一些人有所帮助。

使用dynamodb cli,您可以按如下方式非常简单地完成:

aws dynamodb describe-table --table-name "my-table"
如果该表存在,它将返回

0 -- Command was successful. There were no errors thrown by either the CLI or by the service the request was made to.
如果该表不存在,它将返回

255 -- Command failed. There were errors thrown by either the CLI or by the service the request was made to.
另见:


我还找不到dynamodb的错误代码列表,但我知道,当您超过设置的限制时,会使用错误代码400,因此依赖它将是一个问题。引自亚马逊文档->“如果您的应用程序执行的读/秒或写/秒数超过表的配置吞吐量允许值,则超过配置容量的请求将被限制,您将收到400个错误代码。“我需要手动检查错误代码404是否适用于Descripte Table。使用Descriptable比ListTables更好。如果您的帐户中有数百个表,那么您会无缘无故地通过网络发送大量额外数据。查看我的答案和
doesTableExist
代码示例,了解如何使其与可描述的代码一起工作。在DynamoDb中,错误代码400几乎没有任何意义。你需要检查身体的具体原因导致400。在您的情况下,它是“ResourceNotFoundException”。
0 -- Command was successful. There were no errors thrown by either the CLI or by the service the request was made to.
255 -- Command failed. There were errors thrown by either the CLI or by the service the request was made to.