Java DynamoDB 增加、刪除、修改、查詢


准備jar包

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-core</artifactId>
    <version>1.11.534</version>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-dynamodb</artifactId>
    <version>1.11.46</version>
</dependency>

准備對象:

//用戶憑證
private static String AWSAccessKeyId = "xxx";
private static String AWSSecretKey = "xxx";
//表名
private static String TABLE_NAME = "xxx";
//用戶憑證對象
private static AWSCredentialsProvider awsCredentialsProvider = new AWSCredentialsProvider() {
  public void refresh() {}
  public AWSCredentials getCredentials() {return new BasicAWSCredentials(AWSAccessKeyId, AWSSecretKey);}
};
//表的相關對象
private static AmazonDynamoDB amazonDynamoDBClient = null;
private static DynamoDBMapper dbMapper = null;
private static Table table = null;

數據庫表映射對象:

@DynamoDBTable(tableName = "xxx")
public class User {
    private String id = null;
    private String name = null;
    private String telephone = null;
public User(String id, String name, String telephone) { super(); this.id = id; this.name = name; this.telephone = telephone; }
  //主鍵 @DynamoDBHashKey(attributeName
= "Id") public String getId() { return id; } public void setId(String id) { this.id = id; } public User() { }   //配有索引 userName-index @DynamoDBAttribute(attributeName = "userName") public String getName() { return name; } public void setName(String name) { this.name = name; }   //配有索引 telephone-index @DynamoDBAttribute(attributeName = "telephone") public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } }

初始化對象:

 

amazonDynamoDBClient =  AmazonDynamoDBClientBuilder.standard().withCredentials(awsCredentialsProvider).withRegion(Regions.AP_NORTHEAST_1).build();
dbMapper = new DynamoDBMapper(amazonDynamoDBClient);
table = new  DynamoDB(amazonDynamoDBClient).getTable(TABLE_NAME);

根據id查詢一條:

public static user getItemById(String id) {
        return dbMapper.load(User.class, id);
    }

根據指定索引查詢多條:

public static List<User> getItemBykey(String key, String value) {
    //取索引 Index index
= table.getIndex(key + "-index"); HashMap<String, String> nameMap = new HashMap<String, String>(); nameMap.put("#key", key); HashMap<String, Object> valueMap = new HashMap<String, Object>(); valueMap.put(":value", value);
    //創建篩選條件,以map的形式傳入key和value,條件只能用 = 號,其他未考證 QuerySpec querySpec
= new QuerySpec().withKeyConditionExpression("#key = :value").withNameMap(nameMap) .withValueMap(valueMap); ItemCollection<QueryOutcome> items = index.query(querySpec); Iterator<Item> iterator = items.iterator(); Item item = null; List<User> Users = new ArrayList<User>(); while (iterator.hasNext()) { item = iterator.next(); dashButtonUsers.add(new DashButtonUser(item.getString("Id"),item.getString("userName"),item.getString("telephone")); } return Users; }

根據指定條件掃描多條:

public static List<User> getItemByTimeRange(Long startTime, Long endTime) {
        Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
        expressionAttributeValues.put(":startTime", new AttributeValue().withN("" + startTime));
        expressionAttributeValues.put(":endTime", new AttributeValue().withN("" + endTime));
    //篩選條件 ScanRequest scanRequest
= new ScanRequest().withTableName(TABLE_NAME) .withFilterExpression("startTime >= :startTime and endTime <= :endTime") .withExpressionAttributeValues(expressionAttributeValues); ScanResult result = amazonDynamoDBClient.scan(scanRequest); List<User> users = new ArrayList<User>(); for (Map<String, AttributeValue> item : result.getItems()) { dashButtonUsers.add(new DashButtonUser(/* 略 */)); } return users; }

根據id刪除:

//刪除一條
public static void deleteItemById(String id) {
    dbMapper.delete(new DashButtonUser(id, null, null, null, null, null, null));
    }
//刪除多條
public static void deleteBatch(List<User> ids) {
  //ids[0] -->{"id":"xxx","telephone":null,"name":null} dbMapper.batchDelete(ids); }

添加、修改:

//添加、修改一條
public static void addOrupdateOneItem(User user) {
        dbMapper.save(user);
    }
//添加、修改多條
public static List<FailedBatch>  addOrUpdateBatch(List<User> users) {
        return dbMapper.batchSave(users);
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM