아무거나

Kotlin + Spring Boot DynamoDB SDK 적용 본문

Java & Kotlin/Kotlin

Kotlin + Spring Boot DynamoDB SDK 적용

전봉근 2024. 5. 30. 20:08
반응형

DynamoDB 적용

  1. 의존성 추가(build.gradle.kts)
...

dependencies {
  ...

	// AWS SDK
	implementation("software.amazon.awssdk:dynamodb-enhanced")
	implementation(platform("software.amazon.awssdk:bom:2.20.56"))

...
  1. Entity Class 생성
    [SampleTable.kt]
import com.bkjeon.base.feature.constants.DdbIndexConst
import org.springframework.format.annotation.DateTimeFormat
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.*

@DynamoDbBean
data class SampleTable (
    // 파티션키
    @get:DynamoDbPartitionKey
    var hk: String? = null,

    // 정렬키
    @get:DynamoDbSortKey
    var sk: String? = null,

    // 글로벌 보조 인덱스 파티션키
    @get:DynamoDbSecondaryPartitionKey(indexNames = ["gsi_test_01"])
    @get:DynamoDbAttribute(value = "gsi01_hk")
    var gsi01Hk: String? = null,

    // 글로벌 보조 인덱스 정렬키
    @get:DynamoDbSecondarySortKey(indexNames = ["gsi_test_01"])
    @get:DynamoDbAttribute(value = "gsi01_sk")
    var gsi01Sk: String? = null,

    @get:DynamoDbAttribute(value = "title")
    var title: String? = null,

    @get:DynamoDbAttribute(value = "reg_dttm")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    var regDttm: String? = null
)
  1. Repository 생성
    [DynamodbSampleRepository.kt]
import com.bkjeon.base.core.log.logger
import com.bkjeon.base.feature.constants.CommonConst
import com.bkjeon.base.feature.constants.DdbIndexConst
import com.bkjeon.base.feature.entity.dynamodb.SampleTable
import com.bkjeon.base.feature.enums.dynamodb.DpDpFieldType
import org.springframework.stereotype.Repository
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient
import software.amazon.awssdk.enhanced.dynamodb.Expression
import software.amazon.awssdk.enhanced.dynamodb.Key
import software.amazon.awssdk.enhanced.dynamodb.TableSchema
import software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional
import software.amazon.awssdk.services.dynamodb.model.AttributeValue

@Repository
class DynamodbSampleRepository(
    dynamoDbClient: DynamoDbEnhancedClient
) {
    private val table = dynamoDbClient.table("dp_dp", TableSchema.fromBean(SampleTable::class.java))
    private val log = logger()

    fun getSampleList(): List<SampleTable> {
        val result = arrayListOf<SampleTable>()

        try {
            val expression = StringBuilder()
            val expressionValues = mutableMapOf<String, AttributeValue>()

            expression.append("del_yn = :del_yn")
            expressionValues[":del_yn"] = AttributeValue.builder().s("N").build()

            val queryConditional = QueryConditional.keyEqualTo(
                Key.builder().partitionValue("TEST_COL1")
                    .build()
            )

            table.index("gsi_test_01").query { r ->
                r.consistentRead(false)
                    .queryConditional(queryConditional)
                    .filterExpression(
                        Expression.builder().expression(expression.toString()).expressionValues(expressionValues).build()
                    )
            }.forEach { page ->
                page.items().forEach { item ->
                    result.add(item)
                }
            }
        } catch (e: Exception) {
            log.error("getSampleList: $e")
        }

        return result
    }

}
  1. Service 작성
    [DynamodbService.kt]
import com.bkjeon.base.feature.entity.dynamodb.SampleTable
import com.bkjeon.base.feature.repository.dynamodb.DynamodbSampleRepository
import org.springframework.stereotype.Service

@Service
class DynamodbService(
    val dynamodbSampleRepository: DynamodbSampleRepository
) {

    fun getSampleList(): List<SampleTable> {
        return dynamodbSampleRepository.getSampleList()
    }

}
  1. Controller 작성 [DynamodbController.kt]
import com.bkjeon.base.feature.entity.dynamodb.SampleTable
import com.bkjeon.base.v1.api.dynamodb.service.DynamodbService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/dynamodb")
class DynamodbController(
    val dynamodbService: DynamodbService
) {

    @GetMapping("/samples")
    fun getList(): List<SampleTable> {
        return dynamodbService.getSampleList()
    }

}
반응형
Comments