

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# DynamoDB Mapper에서 보조 인덱스 사용
<a name="ddb-mapper-secondary-indices"></a>

****  
**DynamoDB Mapper는 개발자 미리 보기 릴리스입니다. 기능이 완전하지 않으며 변경될 수 있습니다.**

## 보조 인덱스에 대한 스키마 정의
<a name="ddb-mapper-secondary-indices-schema"></a>

DynamoDB 테이블은 기본 테이블 자체에 정의된 것과 다른 키를 사용하여 데이터에 액세스할 수 있는 보조 인덱스를 지원합니다. 기본 테이블과 마찬가지로 DynamoDB Mapper는 `[ItemSchema](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.items/-item-schema/index.html)` 유형을 사용하여 인덱스와 상호 작용합니다.

DynamoDB 보조 인덱스는 기본 테이블의 모든 속성을 포함할 필요가 없습니다. 따라서 인덱스에 매핑되는 Kotlin 클래스는 해당 인덱스의 기본 테이블에 매핑되는 Kotlin 클래스와 다를 수 있습니다. 이 경우 인덱스 클래스에 대해 별도의 스키마를 선언해야 합니다.

다음 코드는 DynamoDB `cars` 테이블에 대한 인덱스 스키마를 수동으로 생성합니다.

```
import aws.sdk.kotlin.hll.dynamodbmapper.items.ItemConverter
import aws.sdk.kotlin.hll.dynamodbmapper.items.ItemSchema
import aws.sdk.kotlin.hll.dynamodbmapper.model.itemOf

// This is a data class for modelling the index of the Car table. Note
// that it contains a subset of the fields from the Car class and also 
// uses different names for them.
data class Model(val name: String, val manufacturer: String)

// We define an item converter.
val modelConverter = object : ItemConverter<Model> {
    override fun convertTo(from: Model, onlyAttributes: Set<String>?): Item  = itemOf(
        "model" to AttributeValue.S(from.name),
        "make" to AttributeValue.S(from.manufacturer),
    )

    override fun convertFrom(to: Item): Model = Model(
        name = to["model"]?.asSOrNull() ?: error("Invalid attribute `model`"),
        manufacturer = to["make"]?.asSOrNull() ?: error("Invalid attribute `make`"),
    )
}
val modelKey = KeySpec.String("model")
val makeKey = KeySpec.String("make")

val modelSchema = ItemSchema(modelConverter, modelKey, makeKey) // The partition key specification is the second parameter.

/* Note that `Model` index's partition key is `model` and its sort key is `make`,
   whereas the `Car` base table uses `make` as the partition key and `model` as the sort key:

        @DynamoDbItem
        data class Car(
            @DynamoDbPartitionKey
            val make: String,
    
            @DynamoDbSortKey
            val model: String,
    
            val initialYear: Int
        )
*/
```

이제 작업에서 `Model` 인스턴스를 사용할 수 있습니다.

## 작업에 보조 인덱스 사용
<a name="ddb-mapper-gs-index-ops"></a>

DynamoDB Mapper는 인덱스, 즉 `queryPaginated` 및에 대한 작업의 하위 집합을 지원합니다`scanPaginated`. 인덱스에서 이러한 작업을 호출하려면 먼저 테이블 객체에서 인덱스에 대한 참조를 가져와야 합니다. 다음 샘플에서는 `cars-by-model` 인덱스에 대해 이전에 생성한를 사용합니다(생성`modelSchema`은 여기에 표시되지 않음).

```
val table = mapper.getTable("cars", CarSchema)
val index = table.getIndex("cars-by-model", modelSchema)

val modelFlow = index
    .scanPaginated { }
    .items()

modelFlow.collect { model -> println(model) }
```