Reputation: 584
I'm attempting to add multiple entries to my DynamoDB database through a service, but when the code is executed, Only one entry is saved. This is what I have done so far:
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);
}
}
And like i said, only one entry will be saved. I was suggested to use an arraylist, but I'm not sure how to go about doing that. Can anyone help?
All help is appreciated.
EDIT: The Streams.java
class:
@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;
}
}
Upvotes: 2
Views: 2314
Reputation: 1756
It seems your DynamoDB hash key is the user created
field in the Stream class. This is the DynamoDB primary key which uniquely identifies a single item in the DynamoDB table.
Each time you call mapper.save(streams)
, you are overwriting the item you wrote in the previous iteration of the loop since it seems you only set the user created
field once here:
String userCreated = intent.getStringExtra("userCreated");
.
Try using a unique identifier (e.g. stream name
as the hash key). If multiple items have the same hash key you can also use a schema with a range key to create a (hash key, range key) primary key to uniquely identify an item in the DynamoDB table.
Upvotes: 2
Reputation: 1876
DynamodbMapper has a batch save method that allows you to save multiple objects in a batch. Check out the DynamoDBMapper.batchSave.
Upvotes: 0