本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
您可以使用 AWS SDK for .NET 低階 API 建立含有一或多個本機次要索引的 Amazon DynamoDB 資料表、描述資料表上的索引,以及使用索引執行查詢。這些操作會映射至對應的低階 DynamoDB API 動作。如需詳細資訊,請參閱 .NET 程式碼範例。
下列是使用 .NET 低階 API 執行資料表操作的一般步驟。
-
建立
AmazonDynamoDBClient
類別的執行個體。 -
透過建立對應的請求物件,為操作提供必要及選用的參數。
例如,建立
CreateTableRequest
物件來建立資料表,以及建立QueryRequest
物件來查詢資料表或索引。 -
執行您在前一步驟中建立之用戶端所提供的適當方法。
使用本機次要索引建立資料表
您必須同時建立資料表和本機次要索引。若要執行這項操作,請使用 CreateTable
,並提供一或多個本機次要索引的規格。以下 C# 程式碼範例會建立資料表來保存音樂收藏中歌曲的相關資訊。分割區索引鍵為 Artist
,而排序索引鍵為 SongTitle
。次要索引 AlbumTitleIndex
有助於依照專輯標題查詢。
以下是使用 .NET 低階 API 建立具有本機次要索引之資料表的步驟。
-
建立
AmazonDynamoDBClient
類別的執行個體。 -
建立
CreateTableRequest
類別的執行個體,以提供請求資訊。您必須提供資料表名稱、其主索引鍵,以及佈建的輸送量數值。對於本機次要索引,您必須提供索引名稱、索引排序索引鍵的名稱和資料類型、索引的索引鍵結構描述以及屬性投影。
-
以參數形式提供請求物件,以便執行
CreateTable
方法。
下列 C# 程式碼範例示範前述步驟。程式碼會建立一個資料表 (Music
) 與次要索引 AlbumTitle
屬性。資料表分割區索引鍵和排序索引鍵以及索引排序索引鍵,是唯一投影到索引的屬性。
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();
}