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);
    }

转载于:https://www.cnblogs.com/ddopp/p/10876768.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值