본문으로 바로가기

Spring Boot DynamoDb연동하기

category Language/JAVA 2018.05.08 14:56

Spring Boot DynamoDb연동하기


ServerModule.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
 
public abstract class ServerModule {
    @Value("${aws.region}")
    String awsRegion;
 
    public abstract AWSCredentialsProvider getCredentialsProvider();
 
    @Bean
    public AmazonDynamoDB dynamoDB(AWSCredentialsProvider credentialsProvider) {
        return AmazonDynamoDBClientBuilder
                .standard()
                .withCredentials(credentialsProvider)
                .withRegion(this.awsRegion)
                .build();
    }
}
cs



LocalServerModule.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.*;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
 
@Profile({"local"})
@Slf4j
@Configuration
@EnableAsync
@EnableScheduling
public class LocalServerModule extends ServerModule {
 
    @Bean
    @Override
    public AWSCredentialsProvider getCredentialsProvider() {
        log.warn("[DefaultAWSCredentialsProviderChain]");
        return DefaultAWSCredentialsProviderChain.getInstance();
    }
}
cs


application-local.yml
1
2
aws:
  region: us-west-2
cs



AwsDynamoDbTest.java
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
@ActiveProfiles("local")
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class AwsDynamoDbTest {
 
    @Autowired
    AmazonDynamoDB amazonDynamoDB;
 
    @Autowired
    AWSCredentialsProvider awsCredentialsProvider;
 
    private Worker worker;
 
    @Test
    public void name() {
 
        String tableName = "<table_name>";
        DescribeTableResult describeTableResult = amazonDynamoDB.describeTable(tableName);
        System.out.println(describeTableResult);
 
        String streamArn = describeTableResult.getTable().getLatestStreamArn();
        log.info("streamArn:{}",streamArn);
    }
}
cs

end.



'Language > JAVA' 카테고리의 다른 글

LMAX Disruptor 튜터리얼  (0) 2018.05.10
java uuid만들기  (0) 2018.05.09
Spring Boot DynamoDb연동하기  (0) 2018.05.08
java do while  (0) 2018.05.02
java 재귀 호출, 재귀 함수  (0) 2018.04.05
RxJava에 대해 알아보자 - 제3편 데이터 형변환 해서 Flowable로 만들기  (0) 2018.04.04

댓글을 달아 주세요