gpt4 book ai didi

java - 无法将多个对象保存到 DynamoDB?

转载 作者:行者123 更新时间:2023-11-30 08:52:18 24 4
gpt4 key购买 nike

我试图通过一项服务将多个条目添加到我的 DynamoDB 数据库中,但是在执行代码时,只保存了一个条目。这是我到目前为止所做的:

public int onStartCommand(Intent intent, int flags, int startId) {

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(), // Context
"us-east-1:851c121e-326d-4c9e-be47-fec40eb7b693", // Identity Pool ID
Regions.US_EAST_1 // Region
);

AmazonDynamoDB client = new AmazonDynamoDBClient(credentialsProvider);

Toast.makeText(this, "Adding To Database...", Toast.LENGTH_LONG).show();

CognitoCachingCredentialsProvider cognitoProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(), // Context
"us-east-1:851c121e-326d-4c9e-be47-fec40eb7b693", // Identity Pool ID
Regions.US_EAST_1 // Region
);

AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(cognitoProvider);

DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);

String streamName = intent.getStringExtra("streamName");
String userCreated = intent.getStringExtra("userCreated");
ArrayList<String> selectedFriend = intent.getStringArrayListExtra("userInvolved");
intent.getStringExtra("comment");

for(int i = 0; i < selectedFriend.size(); i++){

Streams streams = new Streams();

streams.setStreamname(streamName);
streams.setUsercreated(userCreated);
streams.setUserinvolved(selectedFriend.get(i));
streams.setUnread(true);
streams.setDeleted(false);

mapper.save(streams);

}
}

就像我说的,只会保存一个条目。有人建议我使用数组列表,但我不确定该怎么做。谁能帮忙?

感谢所有帮助。

编辑:Streams.java 类:

@DynamoDBTable(tableName = "Streams")
public class Streams{

private String streamname;
private String usercreated;
private String userinvolved;
private boolean unread;
private boolean deleted;

@DynamoDBAttribute(attributeName = "Stream_Name")
public String getStreamname() {
return streamname;
}

public void setStreamname(String streamname) {
this.streamname = streamname;
}

@DynamoDBHashKey(attributeName = "User_Created")
public String getUsercreated() {
return usercreated;
}

public void setUsercreated(String usercreated) {
this.usercreated = usercreated;
}

@DynamoDBAttribute(attributeName = "User_Involved")
public String getUserinvolved() {
return userinvolved;
}

public void setUserinvolved(String userinvolved) {
this.userinvolved = userinvolved;
}

@DynamoDBAttribute(attributeName = "Unread")
public boolean isUnread() {
return unread;
}

public void setUnread(boolean unread) {
this.unread = unread;
}

@DynamoDBAttribute(attributeName = "Deleted")
public boolean isDeleted() {
return deleted;
}

public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
}

最佳答案

您的 DynamoDB 哈希键似乎是 Stream 类中的 user created 字段。这是 DynamoDB 主键,它唯一标识 DynamoDB 表中的单个项目。

每次您调用 mapper.save(streams) 时,您都会覆盖您在循环的前一次迭代中编写的项目,因为您似乎只设置了 user created 字段一次在这里:

String userCreated = intent.getStringExtra("userCreated");

尝试使用唯一标识符(例如 stream name 作为哈希键)。如果多个项目具有相同的哈希键,您还可以使用具有范围键的架构来创建(哈希键、范围键)主键以唯一标识 DynamoDB 表中的项目。

关于java - 无法将多个对象保存到 DynamoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30201993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com