首页 技术 正文
技术 2022年11月20日
0 收藏 914 点赞 4,581 浏览 3719 个字

准备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);
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893