반응형
사전 작업
- DynamoDB 로컬 설치 : https://momobob.tistory.com/120
- SpringBoot Project 생성 : https://momobob.tistory.com/118
Yaml 파일 DynamoDB 설정값 추가
cloud:
aws:
dynamodb:
region: ap-northeast-2
endpoint-url : http://localhost:8000
accessKey: { access-key }
secretKey: { secret-key }
Config 파일 추가
@Configuration
public class DynamoDBConfig {
@Value("${cloud.aws.dynamodb.region}")
String region;
@Value("${cloud.aws.dynamodb.accessKey}")
String accessKey;
@Value("${cloud.aws.dynamodb.secretKey}")
String secretKey;
@Value("${cloud.aws.dynamodb.endpoint-url}")
String endPointUrl;
@Bean
public DynamoDbAsyncClient dynamoDbAsyncClient() {
return DynamoDbAsyncClient.builder()
.region(Region.of(region))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(accessKey, secretKey)))
.endpointOverride(URI.create(endPointUrl))
.build();
}
@Bean
public DynamoDbEnhancedAsyncClient dynamoDbEnhancedAsyncClient() {
return DynamoDbEnhancedAsyncClient.builder()
.dynamoDbClient(dynamoDbAsyncClient())
.build();
}
}
Entity 추가
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@DynamoDbBean
public class DontworkingEntity {
private String id;
private String name;
private String data;
@DynamoDbPartitionKey
public String getId() {
return id;
}
public Dontworking toDomain() {
return Dontworking.builder()
.id(id)
.name(name)
.data(data)
.build();
}
public static DontworkingEntity toEntity(Dontworking dontworking) {
return DontworkingEntity.builder()
.id(dontworking.getId())
.name(dontworking.getName())
.data(dontworking.getData())
.build();
}
}
Repository 추가
@Slf4j
@Repository
@RequiredArgsConstructor
public class DynamoRepository {
private final DynamoDbEnhancedAsyncClient enhancedAsyncClient;
public Mono<Void> saveDontworking(DontworkingEntity dontworking) {
DynamoDbAsyncTable<DontworkingEntity> table = enhancedAsyncClient.table("DONTWORKING_TEST", TableSchema.fromBean(DontworkingEntity.class));
return Mono.fromFuture(table.putItem((dontworking)));
}
public Flux<DontworkingEntity> findDontworkingById(String id) {
DynamoDbAsyncTable<DontworkingEntity> table = enhancedAsyncClient.table("DONTWORKING_TEST", TableSchema.fromBean(DontworkingEntity.class));
QueryEnhancedRequest queryEnhancedRequest = QueryEnhancedRequest.builder()
.queryConditional(QueryConditional.keyEqualTo(Key.builder()
.partitionValue(id)
.build()))
.build();
return Flux.from(table.query(queryEnhancedRequest).items());
}
}
Test Code 작성
@SpringBootTest
class DynamoRepositoryTest {
@Autowired
DynamoRepository dynamoRepository;
@Test
void saveDontworking() {
DontworkingEntity entity1 = DontworkingEntity.builder()
.id("1000")
.name("홍길동")
.data("길동's DATA")
.build();
Mono<Void> result = dynamoRepository.saveDontworking(entity1);
StepVerifier
.create(result)
.verifyComplete();
}
@Test
void findAllDontworking() {
Flux<DontworkingEntity> result = dynamoRepository.findDontworkingById("1000");
StepVerifier
.create(result)
.expectNextCount(1)
.verifyComplete();
}
}
반응형
테스트 성공!
Local DynamoDB 적재도 성공!
진행하면서 발생한 TroubleShooting은 Error카테고리 참고.
Thank you!
반응형