准备jar包
com.amazonaws aws-java-sdk-core 1.11.534 com.amazonaws aws-java-sdk-dynamodb 1.11.46
准备对象:
//用户凭证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 ListgetItemBykey(String key, String value) { //取索引 Index index = table.getIndex(key + "-index"); HashMap nameMap = new HashMap (); nameMap.put("#key", key); HashMap valueMap = new HashMap (); valueMap.put(":value", value); //创建筛选条件,以map的形式传入key和value,条件只能用 = 号,其他未考证 QuerySpec querySpec = new QuerySpec().withKeyConditionExpression("#key = :value").withNameMap(nameMap) .withValueMap(valueMap); ItemCollection items = index.query(querySpec); Iterator - iterator = items.iterator(); Item item = null; List
Users = new ArrayList (); while (iterator.hasNext()) { item = iterator.next(); dashButtonUsers.add(new DashButtonUser(item.getString("Id"),item.getString("userName"),item.getString("telephone")); } return Users; }
根据指定条件扫描多条:
public static ListgetItemByTimeRange(Long startTime, Long endTime) { Map expressionAttributeValues = new HashMap (); 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 users = new ArrayList (); for (Map 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(Listids) { //ids[0] -->{"id":"xxx","telephone":null,"name":null} dbMapper.batchDelete(ids); }
添加、修改:
//添加、修改一条public static void addOrupdateOneItem(User user) { dbMapper.save(user); }//添加、修改多条public static ListaddOrUpdateBatch(List users) { return dbMapper.batchSave(users); }