使用lambda函数(Python)创建DynamoDB表 - 错误

发布于 2025-02-12 03:37:15 字数 2307 浏览 0 评论 0原文

我在该表定义中定义了3个属性。 AgentID,AgentName,Agentrole。我想在AgentID(PartitionKey),Agentrole(范围键)上创建Keyschema。

在我的理解中,表可以具有10个属性。所有这10个属性不必成为按键的一部分。因为Keyschema用于识别唯一的记录。正确的?

它引发以下错误:

Response

 {

     "errorMessage": "An error occurred (ValidationException) when calling the 
     CreateTable operation: One or more parameter values were invalid: Number of attributes in 
     KeySchema does not exactly match number of attributes defined in AttributeDefinitions",

    "errorType": "ClientError",

    "requestId": "d8d07c59-f36c-4989-9ac2-6ada9d8f6521",

    "stackTrace": [

               "  File \"/var/task/lambda_function.py\", line 8, in lambda_handler\n    
               response = client.create_table(\n",

              "  File \"/var/runtime/botocore/client.py\", line 391, in _api_call\n    return 
              self._make_api_call(operation_name, kwargs)\n",

              "  File \"/var/runtime/botocore/client.py\", line 719, in _make_api_call\n    
      raise error_class(parsed_response, operation_name)\n"

   ]

 }

    import json
    import boto3
    client = boto3.client("dynamodb")
    def lambda_handler(event, context):  

             response = client.create_table(

                  AttributeDefinitions=[
                 {
                      'AttributeName': 'agentId',
                      'AttributeType': 'N'
                 },
                 {
                      'AttributeName': 'agentRole',
                      'AttributeType': 'S'
                 }, 
                 {
                      'AttributeName': 'agentName',
                      'AttributeType': 'S'    
                 }

                ],

                TableName='CallCenterCallsTable,
                KeySchema=[
                {
                      'AttributeName': 'agentId',
                      'KeyType': 'HASH'                 

                },                
               {

                      'AttributeName': 'agentRole',
                      'KeyType': 'RANGE'

               }

             ],

              BillingMode='PROVISIONED',
              ProvisionedThroughput={
                  'ReadCapacityUnits': 1,
                  'WriteCapacityUnits': 1

              }
          )  

         print(response)   

I have defined 3 attributes in that table definition. agentId, agentName, agentRole. I want to create KeySchema on agentId (partitionkey) , agentRole (range key).

In my understanding the table can have 10 attributes. All those 10 attributes don't have to be part of the KeySchema. Because Keyschema is used to identify unique records. Right?

It throws the following error:

Response

 {

     "errorMessage": "An error occurred (ValidationException) when calling the 
     CreateTable operation: One or more parameter values were invalid: Number of attributes in 
     KeySchema does not exactly match number of attributes defined in AttributeDefinitions",

    "errorType": "ClientError",

    "requestId": "d8d07c59-f36c-4989-9ac2-6ada9d8f6521",

    "stackTrace": [

               "  File \"/var/task/lambda_function.py\", line 8, in lambda_handler\n    
               response = client.create_table(\n",

              "  File \"/var/runtime/botocore/client.py\", line 391, in _api_call\n    return 
              self._make_api_call(operation_name, kwargs)\n",

              "  File \"/var/runtime/botocore/client.py\", line 719, in _make_api_call\n    
      raise error_class(parsed_response, operation_name)\n"

   ]

 }

    import json
    import boto3
    client = boto3.client("dynamodb")
    def lambda_handler(event, context):  

             response = client.create_table(

                  AttributeDefinitions=[
                 {
                      'AttributeName': 'agentId',
                      'AttributeType': 'N'
                 },
                 {
                      'AttributeName': 'agentRole',
                      'AttributeType': 'S'
                 }, 
                 {
                      'AttributeName': 'agentName',
                      'AttributeType': 'S'    
                 }

                ],

                TableName='CallCenterCallsTable,
                KeySchema=[
                {
                      'AttributeName': 'agentId',
                      'KeyType': 'HASH'                 

                },                
               {

                      'AttributeName': 'agentRole',
                      'KeyType': 'RANGE'

               }

             ],

              BillingMode='PROVISIONED',
              ProvisionedThroughput={
                  'ReadCapacityUnits': 1,
                  'WriteCapacityUnits': 1

              }
          )  

         print(response)   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女的英雄梦 2025-02-19 03:37:15

从属性定义中删除agentName

参见属性定义的文档

代表描述表和索引的密钥架构的属性。

您不在“键模式”或“索引”中使用agentName,因此不应包含在表定义中。 DynamoDB是示意性的。您只需要在创建时间定义哈希键和排序键即可。 DynamoDB不在乎您可能想插入表中的任何其他属性。

Remove agentName from the Attribute definitions.

See the documentation for Attribute Definitions:

Represents an attribute for describing the key schema for the table and indexes.

You aren't using agentName in the key schema or indexes, so it shouldn't be included in the table definition. DynamoDB is schemaless. You only need to define the hash key and sort key at creation time. DynamoDB doesn't care about any other attributes you may want to insert into your table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文