トピック
AWS SDK for .NET 低レベル API を使用して、1 つ以上のローカルセカンダリインデックスを含む Amazon DynamoDB テーブルを作成し、テーブルのインデックスを記述し、インデックスを使用してクエリを実行できます。これらのオペレーションは、対応する低レベル DynamoDB API アクションにマッピングされます。詳細については、「.NET コード例」を参照してください。
以下に、.NET 低レベル API を使用したテーブルオペレーションの一般的な手順を示します。
-
AmazonDynamoDBClient
クラスのインスタンスを作成します。 -
対応するリクエストオブジェクトを作成して、オペレーションについて必要なパラメータとオプションパラメータを入力します。
例えば、テーブルを作成するには
CreateTableRequest
オブジェクトを作成し、テーブルやインデックスにクエリを実行するにはQueryRequest
オブジェクトを作成します。 -
前述のステップで作成したクライアントから提供された適切なメソッドを実行します。
ローカルセカンダリインデックスを持つテーブルを作成する
ローカルセカンダリインデックスは、テーブルの作成と同時に作成する必要があります。これを行うには、CreateTable
を使用し、1 つ以上のローカルセカンダリインデックスの仕様を指定します。次の C# コード例では、ミュージックコレクション内の曲に関する情報を保持するためのテーブルを作成しています。パーティションキーは Artist
で、ソートキーは SongTitle
です。セカンダリインデックス AlbumTitleIndex
は、アルバムタイトルによるクエリを容易にします。
.NET 低レベル API を使用して、ローカルセカンダリインデックスを含むテーブルを作成する手順を次に示します。
-
AmazonDynamoDBClient
クラスのインスタンスを作成します。 -
リクエスト情報を指定する
CreateTableRequest
クラスのインスタンスを作成します。テーブル名、プライマリキー、およびプロビジョニングされたスループット値を指定する必要があります。ローカルセカンダリインデックスの場合は、インデックス名、インデックスソートキーの名前とデータ型、インデックスのキースキーマ、および属性射影を指定する必要があります。
-
リクエストオブジェクトをパラメータとして指定して、
CreateTable
メソッドを実行します。
以下の C# サンプルコードは、前述のステップの例です。このコードは、AlbumTitle
属性に関するセカンダリインデックスを持つテーブル (Music
) を作成します。テーブルパーティションキーとソートキー、およびインデックスソートキーのみが、インデックスに射影される属性です。
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
string tableName = "Music";
CreateTableRequest createTableRequest = new CreateTableRequest()
{
TableName = tableName
};
//ProvisionedThroughput
createTableRequest.ProvisionedThroughput = new ProvisionedThroughput()
{
ReadCapacityUnits = (long)5,
WriteCapacityUnits = (long)5
};
//AttributeDefinitions
List<AttributeDefinition> attributeDefinitions = new List<AttributeDefinition>();
attributeDefinitions.Add(new AttributeDefinition()
{
AttributeName = "Artist",
AttributeType = "S"
});
attributeDefinitions.Add(new AttributeDefinition()
{
AttributeName = "SongTitle",
AttributeType = "S"
});
attributeDefinitions.Add(new AttributeDefinition()
{
AttributeName = "AlbumTitle",
AttributeType = "S"
});
createTableRequest.AttributeDefinitions = attributeDefinitions;
//KeySchema
List<KeySchemaElement> tableKeySchema = new List<KeySchemaElement>();
tableKeySchema.Add(new KeySchemaElement() { AttributeName = "Artist", KeyType = "HASH" }); //Partition key
tableKeySchema.Add(new KeySchemaElement() { AttributeName = "SongTitle", KeyType = "RANGE" }); //Sort key
createTableRequest.KeySchema = tableKeySchema;
List<KeySchemaElement> indexKeySchema = new List<KeySchemaElement>();
indexKeySchema.Add(new KeySchemaElement() { AttributeName = "Artist", KeyType = "HASH" }); //Partition key
indexKeySchema.Add(new KeySchemaElement() { AttributeName = "AlbumTitle", KeyType = "RANGE" }); //Sort key
Projection projection = new Projection() { ProjectionType = "INCLUDE" };
List<string> nonKeyAttributes = new List<string>();
nonKeyAttributes.Add("Genre");
nonKeyAttributes.Add("Year");
projection.NonKeyAttributes = nonKeyAttributes;
LocalSecondaryIndex localSecondaryIndex = new LocalSecondaryIndex()
{
IndexName = "AlbumTitleIndex",
KeySchema = indexKeySchema,
Projection = projection
};
List<LocalSecondaryIndex> localSecondaryIndexes = new List<LocalSecondaryIndex>();
localSecondaryIndexes.Add(localSecondaryIndex);
createTableRequest.LocalSecondaryIndexes = localSecondaryIndexes;
CreateTableResponse result = client.CreateTable(createTableRequest);
Console.WriteLine(result.CreateTableResult.TableDescription.TableName);
Console.WriteLine(result.CreateTableResult.TableDescription.TableStatus);
DynamoDB がテーブルを作成し、テーブルのステータスを ACTIVE
に設定するまで待機する必要があります。その後、テーブルへのデータ項目の入力を開始できます。
ローカルセカンダリインデックスを持つテーブルの説明
テーブルのローカルセカンダリインデックスに関する情報を取得するには、DescribeTable
API を使用します。インデックスごとに、名前、キースキーマ、および射影された属性にアクセスできます。
.NET 低レベル API を使用してテーブルのローカルセカンダリインデックス情報にアクセスするには次の手順に従います。
-
AmazonDynamoDBClient
クラスのインスタンスを作成します。 -
リクエスト情報を指定する
DescribeTableRequest
クラスのインスタンスを作成します。テーブル名を入力する必要があります。 -
リクエストオブジェクトをパラメータとして指定して、
describeTable
メソッドを実行します。
以下の C# サンプルコードは、前述のステップの例です。
例
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
string tableName = "Music";
DescribeTableResponse response = client.DescribeTable(new DescribeTableRequest() { TableName = tableName });
List<LocalSecondaryIndexDescription> localSecondaryIndexes =
response.DescribeTableResult.Table.LocalSecondaryIndexes;
// This code snippet will work for multiple indexes, even though
// there is only one index in this example.
foreach (LocalSecondaryIndexDescription lsiDescription in localSecondaryIndexes)
{
Console.WriteLine("Info for index " + lsiDescription.IndexName + ":");
foreach (KeySchemaElement kse in lsiDescription.KeySchema)
{
Console.WriteLine("\t" + kse.AttributeName + ": key type is " + kse.KeyType);
}
Projection projection = lsiDescription.Projection;
Console.WriteLine("\tThe projection type is: " + projection.ProjectionType);
if (projection.ProjectionType.ToString().Equals("INCLUDE"))
{
Console.WriteLine("\t\tThe non-key projected attributes are:");
foreach (String s in projection.NonKeyAttributes)
{
Console.WriteLine("\t\t" + s);
}
}
}
ローカルセカンダリインデックスのクエリ
テーブルに Query
を実行するのとほぼ同じ方法で、ローカルセカンダリインデックスに対する Query
を使用することができます。インデックス名、インデックスソートキーのクエリ条件、および返す属性を指定する必要があります。この例では、インデックスは AlbumTitleIndex
、インデックスソートキーは AlbumTitle
です。
返される属性は、インデックスに射影された属性だけです。このクエリを変更して非キー属性を選択することもできますが、これには比較的コストのかかるテーブルフェッチアクティビティが必要です。テーブルのフェッチの詳細については、「属性の射影」を参照してください。
.NET 低レベル API を使用して、ローカルセカンダリインデックスを含むテーブルにクエリを実行する手順を次に示します。
-
AmazonDynamoDBClient
クラスのインスタンスを作成します。 -
リクエスト情報を指定する
QueryRequest
クラスのインスタンスを作成します。 -
リクエストオブジェクトをパラメータとして指定して、
query
メソッドを実行します。
以下の C# サンプルコードは、前述のステップの例です。
例
QueryRequest queryRequest = new QueryRequest
{
TableName = "Music",
IndexName = "AlbumTitleIndex",
Select = "ALL_ATTRIBUTES",
ScanIndexForward = true,
KeyConditionExpression = "Artist = :v_artist and AlbumTitle = :v_title",
ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
{
{":v_artist",new AttributeValue {S = "Acme Band"}},
{":v_title",new AttributeValue {S = "Songs About Life"}}
},
};
QueryResponse response = client.Query(queryRequest);
foreach (var attribs in response.Items)
{
foreach (var attrib in attribs)
{
Console.WriteLine(attrib.Key + " ---> " + attrib.Value.S);
}
Console.WriteLine();
}