如何基于分区键和排序键[Java]查询DynamoDB?
我是DynamoDB新手,想知道如何使用hashKey和sortKey在DynamoDB中查询表 我有一个名为如何基于分区键和排序键[Java]查询DynamoDB?,java,amazon-dynamodb,Java,Amazon Dynamodb,我是DynamoDB新手,想知道如何使用hashKey和sortKey在DynamoDB中查询表 我有一个名为Items的表。它的模式是 1. Product (Partition Key of type String) 2. ID (Sort Key of type int) 3. Date ( attribute of type String) 获取所有具有product=10的项目的查询是 Items it = new Items(); it.setProduct("apple");
Items
的表。它的模式是
1. Product (Partition Key of type String)
2. ID (Sort Key of type int)
3. Date ( attribute of type String)
获取所有具有product=10
的项目的查询是
Items it = new Items();
it.setProduct("apple");
DynamoDBQueryExpression<Items> queryExpression = new DynamoDBQueryExpression<Items>()
.withHashKeyValues(it);
List<Items> itemList = mapper.query(Items.class, queryExpression);
Items it=newitems();
it.setProduct(“苹果”);
DynamoDBQueryExpression queryExpression=新建DynamoDBQueryExpression()
.withHashKeyValues(it);
List itemList=mapper.query(Items.class,queryExpression);
但是,现在我想获得所有具有Product=“apple”
和ID=100
的项目
我可以在
Java
中为DynamoDB
编写一个查询 以便使用分区键和排序键从DynamoDB获取数据。您可以使用DynamoDBMapper
类中的load
方法
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
String product = "ball";
Integer id = 1;
Item itemObj = mapper.load(Items.class, product, id);
模型类,即您的案例中的项目类:-
您应该使用哈希和范围键的适当注释定义Item类
@DynamoDBTable(tableName = "Items")
public class Item {
private String product;
private Integer id;
@DynamoDBHashKey(attributeName = "Product")
public String getProduct() {
return autoID;
}
@DynamoDBRangeKey(attributeName = "ID")
public String getId() {
return id;
}
}
我想添加一种更低级的方法(不使用映射器和注释):
String accessKey=…;//不要在生产中硬编码密钥。
字符串secretKey=。。。;
AmazonDynamoDB Dynamodb客户端=
=AmazondynamodClientBuilder
.standard()
.withRegion(“us-east-1”)
.具有凭据(新的AWSStaticCredentialsProvider(新的基本凭据(accessKey、secretKey)))
.build();
字符串tableName=。。。
Map.Entry partitionKey=。。。
Map.Entry sortKey=。。。
GetItemRequest请求=
新建GetItemRequest().withTableName(tableName)
.withKey(分区键、排序键);
GetItemResult=dynamoDBClient.getItem(请求);
Map item=result.getItem();
GetItemRequest GetItemRequest=new GetItemRequest().withTableName(“员工”)。
addKeyEntry(“departmentId”,新的AttributeValue().with(String.valueOf(departmentId)))。
addKeyEntry(“employeeId”,新的AttributeValue().withN(String.valueOf(employeeId)));
Map responseItem=dynamoDbClient.getItem(getItemRequest.getItem();
给定此发电机表:
该表包含以下详细信息:
- 分区键艺术家
- 排序键-歌曲标题
@DynamoDBTable(tableName="Music")
public class MusicItems {
//Set up Data Members that correspond to items in the Music Table
private String artist;
private String songTitle;
private String albumTitle;
private int awards;
@DynamoDBHashKey(attributeName="Artist")
public String getArtist() { return this.artist; }
public void setArtist(String artist) {this.artist = artist; }
@DynamoDBRangeKey(attributeName="SongTitle")
public String getSongTitle() { return this.songTitle; }
public void setSongTitle(String title) {this.songTitle = title; }
@DynamoDBAttribute(attributeName="AlbumTitle")
public String getAlbumTitle() { return this.albumTitle; }
public void setAlbumTitle(String title) {this.albumTitle = title; }
@DynamoDBAttribute(attributeName="Awards")
public int getAwards() { return this.awards; }
public void setAwards(int awards) {this.awards = awards; }
}
此代码适用于:
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
public class UseDynamoMapping {
public static void main(String[] args)
{
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
MusicItems items = new MusicItems();
try{
//add new content to the Music Table
items.setArtist("Famous Band");
items.setSongTitle("Our Famous Song");
items.setAlbumTitle("Our First Album");
items.setAwards(0);
// Save the item
DynamoDBMapper mapper = new DynamoDBMapper(client);
mapper.save(items);
//Load an item based on the Partition Key and Sort Key
//both values need to be passed to the mapper.load method
String artist = "Famous Band";
String songQueryTitle = "Our Famous Song";
// Retrieve the item.
MusicItems itemRetrieved = mapper.load(MusicItems.class, artist, songQueryTitle);
System.out.println("Item retrieved:");
System.out.println(itemRetrieved);
//Modify the Award value
itemRetrieved.setAwards(2);
mapper.save(itemRetrieved);
System.out.println("Item updated:");
System.out.println(itemRetrieved);
System.out.print("Done");
}
catch (Exception e)
{
e.getStackTrace();
}
}
}
示例的URL-我得到一个错误,说,table;没有范围键ID的映射是排序键而不是范围键。已使用模型类更新答案。同样,也可以定义非关键属性。但要使load正常工作,不需要非键属性。如果我想在一个非常大的数据库上运行排序键,它的加载速度和查询速度一样快,这并没有性能。唯一的区别是不能按非关键点属性过滤。查询API有一个功能,可以在筛选条件中包括非关键属性以及关键属性。
@DynamoDBTable(tableName="Music")
public class MusicItems {
//Set up Data Members that correspond to items in the Music Table
private String artist;
private String songTitle;
private String albumTitle;
private int awards;
@DynamoDBHashKey(attributeName="Artist")
public String getArtist() { return this.artist; }
public void setArtist(String artist) {this.artist = artist; }
@DynamoDBRangeKey(attributeName="SongTitle")
public String getSongTitle() { return this.songTitle; }
public void setSongTitle(String title) {this.songTitle = title; }
@DynamoDBAttribute(attributeName="AlbumTitle")
public String getAlbumTitle() { return this.albumTitle; }
public void setAlbumTitle(String title) {this.albumTitle = title; }
@DynamoDBAttribute(attributeName="Awards")
public int getAwards() { return this.awards; }
public void setAwards(int awards) {this.awards = awards; }
}
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
public class UseDynamoMapping {
public static void main(String[] args)
{
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
MusicItems items = new MusicItems();
try{
//add new content to the Music Table
items.setArtist("Famous Band");
items.setSongTitle("Our Famous Song");
items.setAlbumTitle("Our First Album");
items.setAwards(0);
// Save the item
DynamoDBMapper mapper = new DynamoDBMapper(client);
mapper.save(items);
//Load an item based on the Partition Key and Sort Key
//both values need to be passed to the mapper.load method
String artist = "Famous Band";
String songQueryTitle = "Our Famous Song";
// Retrieve the item.
MusicItems itemRetrieved = mapper.load(MusicItems.class, artist, songQueryTitle);
System.out.println("Item retrieved:");
System.out.println(itemRetrieved);
//Modify the Award value
itemRetrieved.setAwards(2);
mapper.save(itemRetrieved);
System.out.println("Item updated:");
System.out.println(itemRetrieved);
System.out.print("Done");
}
catch (Exception e)
{
e.getStackTrace();
}
}
}