2017-04-11 19 views
3

主キーとは異なるキーでDynamoDBテーブルを照会する必要があります。私はそれのためのグローバルセカンダリインデックスを作成しようとしました。しかし、私はこのエラーが発生します: "クエリキーの条件は、dynamodbをサポートしていません"。いくつかの例を見ることで、プライマリインデックス/キーも含めない限り、セカンダリインデックスでクエリできないように見えますが、これは正しいですか?特定の都市で働くすべての従業員がクエリを実行する必要があるとします。従業員IDなしで実行できますか?主キーなしでDynamoDBを照会

更新された情報 インデックスが作成されていない可能性がありますか?

表情報:

  • ID - >プライマリパーティションキー
  • 主ソートキー - >名前

GSI:

  • パーティション・キー/主キー - →都市
  • 突然変異 - >すべて

私は、パラメータとして、市内を送信したノード、およびインデックス名からクエリを実行する場合:

const filter = { city: city}; 
    return this.getRecordsFromDb(filter, { IndexName: "myIndexName" }) 
     .then(records => __.head(records)); 
+0

メインtaのパーティションキーなしでGSIを作成してクエリできますble。 GSIに照会しようとしている場所でコードを表示できますか? GSIの主要な属性についても言及してください。 – notionquest

+0

ありがとう!私のコードで質問を更新します – eagleEye

答えて

7

注: -あなたは完全なコードを提供していないとして、シミュレートすることは困難であると問題を特定する。しかし、私は同様のテーブルとインデックスを作成しました。それは私のためにうまく動作します。詳細については、以下のコードを参照してください。

ここにテーブル作成スクリプトがあり、インデックスを照会します。

必要に応じてテーブル名とインデックス名を変更することができます。あなたが投稿したのと同じ主要な属性の構造に従ってきました。

これはテスト済みで正常です。

1)インデックス 'city_index' でテーブル '街' を作成します -

var params = { 
     TableName: 'city', 
     KeySchema: [ // The type of of schema. Must start with a HASH type, with an optional second RANGE. 
      { // Required HASH type attribute 
       AttributeName: 'id', 
       KeyType: 'HASH', 
      }, 
      { // Required HASH type attribute 
       AttributeName: 'name', 
       KeyType: 'RANGE', 
      }    

     ], 
     AttributeDefinitions: [ // The names and types of all primary and index key attributes only 
      { 
       AttributeName: 'id', 
       AttributeType: 'S', // (S | N | B) for string, number, binary 
      }, 
      { 
       AttributeName: 'name', 
       AttributeType: 'S', // (S | N | B) for string, number, binary 
      }, 
      { 
       AttributeName: 'city', 
       AttributeType: 'S', // (S | N | B) for string, number, binary 
      }, 

     ], 
     ProvisionedThroughput: { // required provisioned throughput for the table 
      ReadCapacityUnits: 400, 
      WriteCapacityUnits: 400, 
     }, 
     GlobalSecondaryIndexes: [ // optional (list of GlobalSecondaryIndex) 
      { 
       IndexName: 'city_index', 
       KeySchema: [ 
        { // Required HASH type attribute 
         AttributeName: 'city', 
         KeyType: 'HASH', 
        } 
       ], 
       Projection: { // attributes to project into the index 
        ProjectionType: 'ALL' // (ALL | KEYS_ONLY | INCLUDE) 
       }, 
       ProvisionedThroughput: { // throughput to provision to the index 
        ReadCapacityUnits: 400, 
        WriteCapacityUnits: 400, 
       }, 
      }, 
      // ... more global secondary indexes ... 
     ], 

    }; 
    dynamodb.createTable(params, function(err, data) { 
     if (err){ console.log("error :" +JSON.stringify(err));} // an error occurred 
     else console.log("success :" +JSON.stringify(data)); // successful response 

    }); 

2)は、市のテーブルにいくつかのデータを挿入し

3)を使ったクエリのインデックス: -

var docClient = new AWS.DynamoDB.DocumentClient(); 
var table = "city"; 
var params = { 
    TableName : table, 
    IndexName : 'city_index', 
    KeyConditionExpression : 'city = :cityVal', 
    ExpressionAttributeValues : { 
     ':cityVal' : 'london'   
    } 
}; 

docClient.query(params, function(err, data) { 
    if (err) { 
     console.error("Unable to read item. Error JSON:", JSON.stringify(err, 
       null, 2)); 
    } else { 
     console.log("GetItem succeeded:", JSON.stringify(data, null, 2)); 
    } 
}); 
+1

'IndexName:'は、ソートキーを持つプライマリインデックスの命を救う人でした。 –

関連する問題