일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- springboot
- ubuntu
- Spring Boot
- Design Patterns
- MySQL
- db
- jenkins
- Spring
- AWS
- Git
- laravel
- elasticsearch
- linux
- jsp
- JVM
- javascript
- IntelliJ
- 맛집
- Gradle
- ReactJS
- tool
- it
- Oracle
- Spring Batch
- php
- devops
- 요리
- java
- redis
- Web Server
Archives
- Today
- Total
아무거나
Kotlin + Spring Boot DynamoDB SDK 적용 본문
반응형
DynamoDB 적용
- 의존성 추가(build.gradle.kts)
...
dependencies {
...
// AWS SDK
implementation("software.amazon.awssdk:dynamodb-enhanced")
implementation(platform("software.amazon.awssdk:bom:2.20.56"))
...
- 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
)
- 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
}
}
- 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()
}
}
- 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()
}
}
반응형
'Java & Kotlin > Kotlin' 카테고리의 다른 글
스케쥴러 구현 (@EnableScheduling) (0) | 2024.11.12 |
---|---|
객체 간 매핑을 위한 Kotlin MapStruct 적용 (1) | 2024.06.28 |
Kotlin + Spring Boot 에 Ehcache 적용 (0) | 2024.05.30 |
Comments