You can create a .json file with the data you want to enter.
This is the content of my json file.
[ { "PhoneNo" : ... , "EmpName" : ... }]
Here is the code I have used to insert data in my database.
import java.io.File;
import java.util.Iterator;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class InsertDataDynamoDb
{
private static final AWSCredentials AWS_CREDENTIALS;
static {
// Your accesskey and secretkey
AWS_CREDENTIALS = new BasicAWSCredentials(
"access key",
"secretkey"
);
}
public static void main(String[] args) throws Exception {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(AWS_CREDENTIALS))
.build();
DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable("Employee");
JsonParser parser = new JsonFactory().createParser(new File("employeedata.json"));
JsonNode rootNode = new ObjectMapper().readTree(parser);
Iterator<JsonNode> iter = rootNode.iterator();
ObjectNode currentNode;
while (iter.hasNext()) {
currentNode = (ObjectNode) iter.next();
int phoneno = currentNode.path("PhoneNo").asInt();
String empname = currentNode.path("EmpName").asText();
try {
table.putItem(new Item().withPrimaryKey("PhoneNo", phoneno, "EmpName", empname).withJSON("info",
currentNode.path("info").toString()));
System.out.println("PutItem succeeded: " + phoneno + " " + empname);
}
catch (Exception e) {
System.err.println("Unable to add movie: " + phoneno + " " + empname);
System.err.println(e.getMessage());
break;
}
}
parser.close();
}
}
Hope this helps.
secretkey