AWS DynamoDB
1.配置AWS
$ aws configure
AWS Access Key ID [None]: xxxxx
AWS Secret Access Key [None]: xxxxxxx
Default region name [None]: us-west-2
Default output format [None]: json
aws configure 不起作用就修改 C:UsersAdministrator.awscredentials
$ aws configure set aws_access_key_id aws_access_key_id
$ aws configure set aws_secret_access_key aws_secret_access_key/EOcyx0Ll
$ aws configure set default.region us-west-2
$ aws configure set default.ca_bundle /path/to/ca-bundle.pem
$ aws configure set region us-west-1 --profile testing
$ aws configure set profile.testing2.region eu-west-1
$ aws configure set preview.cloudsearch true
2.启动DynamoDB local
d:
cd D:dbDynamoDBdynamodb_local_latest
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
3.查询
aws dynamodb list-tables --endpoint-url http://localhost:8000 --output json
aws dynamodb --endpoint-url http://localhost:8000 --output json put-item --table-name t02_ATD_INFO --item "{"deviceId":{"S":"ABCDEFG123456789"},"shardId":{"S":"0001"}}" --return-consumed-capacity TOTAL
json参数用"包围 内容中的"要转义 "
4.JAVA
POM.XML
<!--Dependency:-->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<version>[1.11,2.0)</version>
</dependency>
<!--Custom repository:-->
<repositories>
<repository>
<id>dynamodb-local-oregon</id>
<name>DynamoDB Local Release Repository</name>
<url>https://s3-ap-northeast-1.amazonaws.com/dynamodb-local-tokyo/release</url>
</repository>
</repositories>
配置
common.aws.common.accessKey=accessKeyaccessKeyaccessKey
common.aws.common.secretKey=secretKeysecretKeysecretKey
common.aws.common.region=ap-northeast-1
common.aws.common.envId=ut
#common.aws.dynamodb.endpoint=dynamodb.ap-northeast-1.amazonaws.com
common.aws.dynamodb.endpoint=http://localhost:8000
common.aws.dynamodb.config.maxConnections=500
common.aws.dynamodb.config.socketTimeout=50000
common.aws.dynamodb.config.connectionTimeout=50000
common.aws.dynamodb.config.maxErrorRetry=3
调用
DynamoDBMapper mapper = null;
// 1.エンドポイント設定
EndpointConfiguration endpointConfiguration = new EndpointConfiguration(
props.getEndpoint(), commonProps.getRegion());
// 2.認証設定
AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
// 3.コンフィグ設定
ClientConfiguration config = new ClientConfiguration();
new RelaxedDataBinder(config)
.bind(new MutablePropertyValues(props.getConfig()));
// 4.クライアント生成
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withEndpointConfiguration(endpointConfiguration)
.withCredentials(credentialsProvider)
.withClientConfiguration(config).build();
// 5.マッパー生成
DynamoDBMapperConfig mapperConfig = DynamoDBMapperConfig.builder()
.withTableNameOverride(TableNameOverride
.withTableNamePrefix(commonProps.getEnvId() + "_"))
.build();
mapper = new DynamoDBMapper(client, mapperConfig);
// CreateTableRequest req = mapper.generateCreateTableRequest(MailInfo.class);
// req.setProvisionedThroughput(new ProvisionedThroughput(5L, 5L));
// client.createTable(req);
// insert
String mailId = "11";
MailInfo test = new MailInfo();
test.setMailId(mailId);
test.setMailBody("testtesttest");
mapper.save(test);
// select
MailInfo dynamo = mapper.load(MailInfo.class, mailId);
表映射对象
@DynamoDBTable(tableName = "MAIL_INFO_TABLE")
@DynamoDBHashKey(attributeName = "mailId")
@DynamoDBAttribute(attributeName = "mailSubject")
/**
* メールプッシュ情報(Dynamo)DTO.
*
* @author devuser
*
*/
@DynamoDBTable(tableName = "MAIL_INFO_TABLE")
public class MailInfo {
/**
* メールプッシュID.
*/
private String mailId;
/**
* Mail件名.
*/
private String mailSubject;
/**
* PUSH本文.
*/
private String pushBody;
/**
* メール件名.
*/
private String mailSubject;
/**
* メール本文.
*/
private String mailBody;
/**
* メールプッシュID取得.
*
* @return メールプッシュID.
*/
@DynamoDBHashKey(attributeName = "mailId")
public String getMailPushId() {
return mailId;
}
/**
* メールプッシュID設定.
*
* @param mailId
* メールプッシュID.
*/
public void setMailPushId(final String mailId) {
this.mailId = mailId;
}
/**
* Mail件名取得.
*
* @return Mail件名.
*/
@DynamoDBAttribute(attributeName = "mailSubject")
public String getPushSubject() {
return mailSubject;
}
/**
* Mail件名設定.
*
* @param mailSubject
* Mail件名.
*/
public void setPushSubject(final String mailSubject) {
this.mailSubject = mailSubject;
}
/**
* PUSH本文取得.
*
* @return PUSH本文.
*/
@DynamoDBAttribute(attributeName = "pushBody")
public String getPushBody() {
return pushBody;
}
/**
* PUSH本文設定.
*
* @param pushBody
* PUSH本文.
*/
public void setPushBody(final String pushBody) {
this.pushBody = pushBody;
}
/**
* メール件名取得.
*
* @return メール件名.
*/
@DynamoDBAttribute(attributeName = "mailSubject")
public String getMailSubject() {
return mailSubject;
}
/**
* メール件名設定.
*
* @param mailSubject
* メール件名.
*/
public void setMailSubject(final String mailSubject) {
this.mailSubject = mailSubject;
}
/**
* メール本文取得.
*
* @return メール本文.
*/
@DynamoDBAttribute(attributeName = "mailBody")
public String getMailBody() {
return mailBody;
}
/**
* メール本文設定.
*
* @param mailBody
* メール本文.
*/
public void setMailBody(final String mailBody) {
this.mailBody = mailBody;
}
}