2015-06-16 5 views
39

मैं DynamoDB जावास्क्रिप्ट खोल का उपयोग करते हुए एक सरल तालिका बनाने के लिए कोशिश कर रहा हूँ और मैं इस अपवाद हो रही है:कुंजी स्कीमा विशेषता में परिभाषित विशेषताओं की संख्या से मेल खाना चाहिए में विशेषताओं की संख्या परिभाषाएँ


    { 
    "message": "The number of attributes in key schema must match the number of attributes defined in attribute definitions.", 
    "code": "ValidationException", 
    "time": "2015-06-16T10:24:23.319Z", 
    "statusCode": 400, 
    "retryable": false 
    } 
 

नीचे है तालिका मैं बनाने की कोशिश कर रहा हूं:


    var params = { 
     TableName: 'table_name', 
     KeySchema: [ 
      { 
       AttributeName: 'hash_key_attribute_name', 
       KeyType: 'HASH', 
      }, 

     ], 
     AttributeDefinitions: [ 
      { 
       AttributeName: 'hash_key_attribute_name', 
       AttributeType: 'S', 
      }, 
      { 
       AttributeName: 'attribute_name_1', 
       AttributeType: 'S', 
      } 
     ], 
     ProvisionedThroughput: { 
      ReadCapacityUnits: 1, 
      WriteCapacityUnits: 1, 
     }, 


    }; 
    dynamodb.createTable(params, function(err, data) { 
     if (err) print(err); 
     else print(data); 
    }); 
 

हालांकि अगर मैं कुंजीशेमा में दूसरी विशेषता जोड़ता हूं, तो यह ठीक काम करता है। कार्य तालिका के नीचे:


    var params = { 
     TableName: 'table_name', 
     KeySchema: [ 
      { 
       AttributeName: 'hash_key_attribute_name', 
       KeyType: 'HASH', 
      }, 
      { 
       AttributeName: 'attribute_name_1', 
       KeyType: 'RANGE', 
      } 

     ], 
     AttributeDefinitions: [ 
      { 
       AttributeName: 'hash_key_attribute_name', 
       AttributeType: 'S', 
      }, 
      { 
       AttributeName: 'attribute_name_1', 
       AttributeType: 'S', 
      } 
     ], 
     ProvisionedThroughput: { 
      ReadCapacityUnits: 1, 
      WriteCapacityUnits: 1, 
     }, 


    }; 
    dynamodb.createTable(params, function(err, data) { 
     if (err) print(err); 
     else print(data); 
    }); 
 

मैं श्रेणी को मुख्य स्कीमा में जोड़ना नहीं चाहता हूं। इसे कैसे ठीक किया जाये, कोई विचार?

+0

क्या यह केवल डायनेमो डीब्लोकल के खिलाफ होता है? क्या होता है जब आप वास्तविक सेवा के खिलाफ एक ही चीज़ करने की कोशिश करते हैं? – mkobit

+0

मेरे पास अभी तक एक एडब्ल्यूएस खाता नहीं है, इसलिए वास्तविक सेवा के खिलाफ इसका परीक्षण नहीं किया जा सका। मैं डायनेमो डीबी स्थानीय (dynamodb_local_2015-04-27_1.0) का नवीनतम संस्करण उपयोग कर रहा हूं। – NAbbas

+0

मैं dynamodb_local_2016-04-19 – Chris

उत्तर

104

DynamoDB स्कीमा-(कुंजी स्कीमा को छोड़कर)

कहने के लिए, यदि आप तालिका बनाने कुंजी स्कीमा (नाम और प्रकार विशेषता) निर्दिष्ट करने की आवश्यकता है है है। खैर, आपको किसी भी गैर-कुंजी विशेषताओं को निर्दिष्ट करने की आवश्यकता नहीं है। आप किसी आइटम को बाद में किसी भी विशेषता के साथ रख सकते हैं (पाठ्यक्रम की चाबियाँ शामिल करनी चाहिए)। विशेषताओं है कि टेबल और अनुक्रमित के लिए कुंजी स्कीमा का वर्णन की एक सरणी

:

documentation page से, AttributeDefinitions के रूप में परिभाषित किया गया है।

जब आप तालिका बनाते हैं, तो AttributeDefinitions फ़ील्ड केवल हैश और/या रेंज कुंजी के लिए उपयोग किया जाता है। आपके पहले मामले में, केवल 2 हैश कुंजी है (संख्या 1) जबकि आप 2 विशेषता प्रदान करते हैं। यह अपवाद का मूल कारण है।

टीएल; डीआरAttributeDefinitions में किसी भी गैर-महत्वपूर्ण विशेषता परिभाषाओं को शामिल न करें।

+5

एक अपवाद के साथ मेरा मानना ​​है कि गैर-महत्वपूर्ण विशेषता 'विशेषता परिभाषा' में होनी चाहिए यदि उस कुंजी का उपयोग सूचकांक में 'हैश' या 'श्रेणी' कुंजी के रूप में किया जाएगा – Srle

0

मुझे यह समस्या भी थी और मैं यहां पोस्ट करूंगा यदि यह किसी और की मदद करता है तो मेरे लिए क्या गलत हुआ।

मेरे CreateTableRequest में, मेरे पास GlobalSecondaryIndexes के लिए एक खाली सरणी थी।

CreateTableRequest createTableRequest = new CreateTableRequest 
{ 
    TableName = TableName, 
    ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 2, WriteCapacityUnits = 2 }, 
    KeySchema = new List<KeySchemaElement> 
    { 
    new KeySchemaElement 
    { 
     AttributeName = "Field1", 
     KeyType = KeyType.HASH 
    }, 
    new KeySchemaElement 
    { 
     AttributeName = "Field2", 
     KeyType = KeyType.RANGE 
    } 
    }, 
    AttributeDefinitions = new List<AttributeDefinition>() 
    { 
    new AttributeDefinition 
    { 
     AttributeName = "Field1", 
     AttributeType = ScalarAttributeType.S 
    }, 
    new AttributeDefinition 
    { 
     AttributeName = "Field2", 
     AttributeType = ScalarAttributeType.S 
    } 
    }, 
    //GlobalSecondaryIndexes = new List<GlobalSecondaryIndex> 
    //{        
    //} 
}; 

तालिका निर्माण में इन पंक्तियों को टिप्पणी करने से मेरी समस्या हल हो गई। तो मुझे लगता है कि सूची शून्य होनी चाहिए, खाली नहीं है।

5

जब आप "विशेषता वितरण" में गैर-महत्वपूर्ण विशेषता का उपयोग करते हैं, तो आपको इसे इंडेक्स के रूप में उपयोग करना होगा, अन्यथा यह काम करने के लिए डायनेमोड के माध्यम से है। देखें link

इसलिए यदि आप इसे इंडेक्स या प्राथमिक कुंजी के रूप में उपयोग नहीं कर रहे हैं तो "विशेषता परिभाषा" में गैर-महत्वपूर्ण विशेषता डालने की आवश्यकता नहीं है।

var params = { 
     TableName: 'table_name', 
     KeySchema: [ // The type of of schema. Must start with a HASH type, with an optional second RANGE. 
      { // Required HASH type attribute 
       AttributeName: 'UserId', 
       KeyType: 'HASH', 
      }, 
      { // Optional RANGE key type for HASH + RANGE tables 
       AttributeName: 'RemindTime', 
       KeyType: 'RANGE', 
      } 
     ], 
     AttributeDefinitions: [ // The names and types of all primary and index key attributes only 
      { 
       AttributeName: 'UserId', 
       AttributeType: 'S', // (S | N | B) for string, number, binary 
      }, 
      { 
       AttributeName: 'RemindTime', 
       AttributeType: 'S', // (S | N | B) for string, number, binary 
      }, 
      { 
       AttributeName: 'AlarmId', 
       AttributeType: 'S', // (S | N | B) for string, number, binary 
      }, 
      // ... more attributes ... 
     ], 
     ProvisionedThroughput: { // required provisioned throughput for the table 
      ReadCapacityUnits: 1, 
      WriteCapacityUnits: 1, 
     }, 
     LocalSecondaryIndexes: [ // optional (list of LocalSecondaryIndex) 
      { 
       IndexName: 'index_UserId_AlarmId', 
       KeySchema: [ 
        { // Required HASH type attribute - must match the table's HASH key attribute name 
         AttributeName: 'UserId', 
         KeyType: 'HASH', 
        }, 
        { // alternate RANGE key attribute for the secondary index 
         AttributeName: 'AlarmId', 
         KeyType: 'RANGE', 
        } 
       ], 
       Projection: { // required 
        ProjectionType: 'ALL', // (ALL | KEYS_ONLY | INCLUDE) 
       }, 
      }, 
      // ... more local secondary indexes ... 
     ], 
    }; 
    dynamodb.createTable(params, function(err, data) { 
     if (err) ppJson(err); // an error occurred 
     else ppJson(data); // successful response 

    }); 
संबंधित मुद्दे