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# 코드 예제에서는 이전 단계를 설명합니다. 이 코드는 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();
}