Dynamodb 学习

https://github.com/eugenp/tutorials/tree/master/persistence-modules

 

官方指南
https://docs.amazonaws.cn/amazondynamodb/latest/developerguide/CodeSamples.Java.html
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/best-practices.html
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Java.01.html
https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/LSIJavaDocumentAPI.Example.html

proxy

https://docs.aws.amazon.com/zh_cn/cli/latest/userguide/cli-configure-proxy.html

 

DynamoDB 写入读取容量
https://blog.csdn.net/m0_37263637/article/details/80750399

数据偏移、分区陷阱……我们这样避开DynamoDB的5个坑
https://blog.csdn.net/qq_16229873/article/details/102595262

DynamoDB基本概念
https://zhuanlan.zhihu.com/p/72397412

Amazon DynamoDB 入门5:索引管理及创建
https://blog.csdn.net/czongxiao/article/details/100013120

Dynamodb 学习笔记一(主键、二级索引)
https://blog.csdn.net/wuzhong8809/article/details/91040543

AWS DynamoDB 常用操作
https://blog.csdn.net/m0_37263637/article/details/80523196

【AWS】 DynamoDB Java 高级API 实现增删改查 【附图】
https://blog.csdn.net/wenzhouxiaomayi77/article/details/105123494/

Download NoSQL Workbench
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/workbench.settingup.html

Deploying DynamoDB Locally on Your Computer
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html

 

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;

/**
 * AWS Config.
 *
 */
@Configuration
public class DynamoDbConfig {

    @Value("${spring.data.aws.access-key}")
    private String amazonAWSAccessKey;

    @Value("${spring.data.aws.secret-key}")
    private String amazonAWSSecretKey;

    @Value("${dynamodb.local.enable}")
    private Boolean dynamodbLocalEnable;

    public AWSCredentialsProvider amazonAWSCredentialsProvider() {
        return new AWSStaticCredentialsProvider(amazonAWSCredentials());
    }

    @Bean
    public DynamoDBMapperConfig dynamoDBMapperConfig() {
        return DynamoDBMapperConfig.DEFAULT;
    }

    @Bean
    public DynamoDBMapper dynamoDBMapper(AmazonDynamoDB amazonDynamoDB, DynamoDBMapperConfig config) {
        return new DynamoDBMapper(amazonDynamoDB, config);
    }

    @Bean
    public AmazonDynamoDB amazonDynamoDB() {

        if (dynamodbLocalEnable) {
            return AmazonDynamoDBClientBuilder.standard().withCredentials(amazonAWSCredentialsProvider())
                    .withEndpointConfiguration(new EndpointConfiguration("http://127.0.0.1:8000/", "us-east-1"))
                    .build();
        } else {
		ClientConfiguration config = new ClientConfiguration();
		config.setProtocol(Protocol.HTTPS);
		config.setProxyHost("xxx.xxx.xxx.xxx");
		config.setProxyPort(80);
		config.setProxyUsername("xxxx");
		config.setProxyPassword("xxxxx");
		return AmazonDynamoDBClientBuilder.standard().withCredentials(amazonAWSCredentialsProvider())
		.withClientConfiguration(config)
		.withRegion(Regions.US_EAST_2)
		.build();

        }

    }

    @Bean
    public AWSCredentials amazonAWSCredentials() {
        return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
    }

}

  

  

 

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import lombok.ToString;

/**
 * xxxxxxxxxx.
 *
 */
@ToString
@DynamoDBTable(tableName = "user_auth_info")
public class UserAuthInfoDynEntity {

    // xxxxxxxxxx
    private String authToken;

    // xxxxxxxxxx
    private String accountId;

    // xxxxxxx
    private String sbscrbNo;

    // xxxxxxxxxxx
    private String lineId;

    // xxxxxxxxxxxx
    private String simLineId;

    // xxxxxxxxxxxx
    private String lineidId;

    // xxxxxxxxxxxx
    private String authType;

    // xxxxxxxxxxx
    private String accountStatus;

    // xxxxxxxxxxxx
    private String menberStatus;

    // xxxxxxxxx
    private String ekycStatus;

    // xxxxxxxxxxx
    private String expireDate;

    @DynamoDBHashKey(attributeName = "auth_token")
    public String getAuthToken() {
        return authToken;
    }

    public void setAuthToken(String authToken) {
        this.authToken = authToken;
    }

    @DynamoDBIndexHashKey(globalSecondaryIndexName = "index_acount_id",
            attributeName = "account_id")
    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    @DynamoDBAttribute(attributeName = "sbscrb_no")
    public String getSbscrbNo() {
        return sbscrbNo;
    }

    public void setSbscrbNo(String sbscrbNo) {
        this.sbscrbNo = sbscrbNo;
    }

    @DynamoDBAttribute(attributeName = "line_id")
    public String getLineId() {
        return lineId;
    }

    public void setLineId(String lineId) {
        this.lineId = lineId;
    }

    @DynamoDBAttribute(attributeName = "sim_line_id")
    public String getSimLineId() {
        return simLineId;
    }

    public void setSimLineId(String simLineId) {
        this.simLineId = simLineId;
    }

    @DynamoDBAttribute(attributeName = "lineid_id")
    public String getLineidId() {
        return lineidId;
    }

    public void setLineidId(String lineidId) {
        this.lineidId = lineidId;
    }

    @DynamoDBAttribute(attributeName = "auth_type")
    public String getAuthType() {
        return authType;
    }

    public void setAuthType(String authType) {
        this.authType = authType;
    }

    @DynamoDBAttribute(attributeName = "account_status")
    public String getAccountStatus() {
        return accountStatus;
    }

    public void setAccountStatus(String accountStatus) {
        this.accountStatus = accountStatus;
    }

    @DynamoDBAttribute(attributeName = "menber_status")
    public String getMenberStatus() {
        return menberStatus;
    }

    public void setMenberStatus(String menberStatus) {
        this.menberStatus = menberStatus;
    }

    @DynamoDBAttribute(attributeName = "ekyc_status")
    public String getEkycStatus() {
        return ekycStatus;
    }

    public void setEkycStatus(String ekycStatus) {
        this.ekycStatus = ekycStatus;
    }

    @DynamoDBAttribute(attributeName = "expire_date")
    public String getExpireDate() {
        return expireDate;
    }

    public void setExpireDate(String expireDate) {
        this.expireDate = expireDate;
    }

}

  

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.Select;
import com.xxxxx.common.dynamodb.entity.UserAuthInfoDynEntity;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * xxxxxxxxxxxx.
 *
 */
@Component
public class UserAuthInfoDynRepository {

    @Autowired
    DynamoDBMapper dynamoDbMapper;

    static final String ACOUNT_INDEX_NAME = "index_acount_id";

    /**
     * xxxxxxx.
     * @param authToken String
     * @return List(UserAuthInfoDynEntity)
     */
    public UserAuthInfoDynEntity search(String authToken) {
        return dynamoDbMapper.load(UserAuthInfoDynEntity.class, authToken);
    }

    /**
     * xxxxxxxxxxxx.
     * @param data UserAuthInfoDynEntity
     */
    public void save(UserAuthInfoDynEntity data) {
        dynamoDbMapper.save(data);
    }

    /**
     * xxxxxxxxxxxxxx
     * @param  accountId String
     * @return List(UserAuthInfoDynEntity)
     */
    public  List<UserAuthInfoDynEntity> searchByAccountIdIndex(String accountId) {
        HashMap<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
        eav.put(":account_id", new AttributeValue().withS(accountId));
        DynamoDBQueryExpression<UserAuthInfoDynEntity> queryExpression =
                new DynamoDBQueryExpression<UserAuthInfoDynEntity>()
                .withIndexName(ACOUNT_INDEX_NAME)
                .withConsistentRead(false)
                .withKeyConditionExpression("account_id = :account_id")
                .withSelect(Select.ALL_PROJECTED_ATTRIBUTES)
                .withExpressionAttributeValues(eav);

        return dynamoDbMapper.query(UserAuthInfoDynEntity.class, queryExpression);
    }
}

  

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.CreateTableResult;
import com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.Projection;
import com.amazonaws.services.dynamodbv2.model.ProjectionType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class DynamodbInitConfig implements InitializingBean {

    @Autowired
    DynamoDBMapper dynamoDBMapper;

    @Autowired
    AmazonDynamoDB amazonDynamoDB;

    @Override
    public void afterPropertiesSet() throws Exception {
       
        log.info("DynamodbInitConfig afterPropertiesSet ...");
        try {
//            List<String> tableNames = amazonDynamoDB.listTables().getTableNames();
//
////            if (!tableNames.contains("user_auth_info")) {
////                createTable_user_auth_info();
////            }
////            if (!tableNames.contains("token_info")) {
////                createTable_token_info();
////            }
        } catch(Exception e) {
            log.error("DynamodbInitConfig error ", e);
        }
    }

//    private void createTable() {
//
//        List<KeySchemaElement> elements = new ArrayList<KeySchemaElement>();
//        KeySchemaElement keySchemaElement = new KeySchemaElement()
//                .withKeyType(KeyType.HASH)
//                .withAttributeName("name");
//        elements.add(keySchemaElement);
//
//        List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
//
//        attributeDefinitions.add(new AttributeDefinition()
//                .withAttributeName("name")
//                .withAttributeType(ScalarAttributeType.S));
//
//        CreateTableRequest createTableRequest = new CreateTableRequest()
//                .withTableName("Movies")
//                .withKeySchema(elements)
//                .withProvisionedThroughput(new ProvisionedThroughput()
//                        .withReadCapacityUnits(5L)
//                        .withWriteCapacityUnits(5L))
//                .withAttributeDefinitions(attributeDefinitions);
//
//        amazonDynamoDB.createTable(createTableRequest);
//
//
    private void createTable_user_auth_info() {


    // Attribute definitions
    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();

    attributeDefinitions.add(new AttributeDefinition()
        .withAttributeName("auth_token")
        .withAttributeType("S"));
    attributeDefinitions.add(new AttributeDefinition()
        .withAttributeName("account_id")
        .withAttributeType("S"));

    // Table key schema
    ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
    tableKeySchema.add(new KeySchemaElement()
        .withAttributeName("auth_token")
        .withKeyType(KeyType.HASH));  //Partition key


    // PrecipIndex
    GlobalSecondaryIndex precipIndex = new GlobalSecondaryIndex()
        .withIndexName("index_acount_id")
        .withProvisionedThroughput(new ProvisionedThroughput()
            .withReadCapacityUnits((long) 10)
            .withWriteCapacityUnits((long) 1))
            .withProjection(new Projection().withProjectionType(ProjectionType.ALL));

    ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();

    indexKeySchema.add(new KeySchemaElement()
        .withAttributeName("account_id")
        .withKeyType(KeyType.HASH));  //Partition key
    ;

    precipIndex.setKeySchema(indexKeySchema);

    CreateTableRequest createTableRequest = new CreateTableRequest()
        .withTableName("user_auth_info")
        .withProvisionedThroughput(new ProvisionedThroughput()
            .withReadCapacityUnits((long) 5)
            .withWriteCapacityUnits((long) 1))
        .withAttributeDefinitions(attributeDefinitions)
        .withKeySchema(tableKeySchema)
        .withGlobalSecondaryIndexes(precipIndex);

    CreateTableResult table = amazonDynamoDB.createTable(createTableRequest);
    log.info(table.getTableDescription().toString());
}

    private void createTable_token_info() {

        // Attribute definitions
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();

        attributeDefinitions.add(new AttributeDefinition()
            .withAttributeName("account_id")
            .withAttributeType("S"));

        // Table key schema
        ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
        tableKeySchema.add(new KeySchemaElement()
            .withAttributeName("account_id")
            .withKeyType(KeyType.HASH));  //Partition key

        CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName("token_info")
            .withProvisionedThroughput(new ProvisionedThroughput()
                .withReadCapacityUnits((long) 5)
                .withWriteCapacityUnits((long) 1))
            .withAttributeDefinitions(attributeDefinitions)
            .withKeySchema(tableKeySchema);

        CreateTableResult table = amazonDynamoDB.createTable(createTableRequest);
        log.info(table.getTableDescription().toString());
    }


}

  

  

posted @ 2020-10-12 10:50  dunkbird  阅读(331)  评论(1编辑  收藏  举报