2

I created a table in Amazon dynamodb with primary key Issue(String) which has data stored in it.I want to read the values from my table. I'm using the following code..

@DynamoDBTable(tableName="Incident")
 AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient();
 String tableName = "Incident";
 Table table = dynamoDBClient.getTable("Incident");
 Item getItem=dynamoDBClient.getItem();

I'm getting an error when calling the getTable method.... is it a predefined method just like createTable() or do we need to write our own..if so how? And also what method should be used to read all items in the table..? I used this link to write some of the code... http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPIItemCRUD.html#JavaDocumentAPIGetItem

I'm new to Java please help..

3 Answers 3

5

Scan API can be used to get all the items from the table.

The scan should be done until LastEvaluatedKey is not null which is very important to get all the items. Otherwise, you will not get all the items if the table has many items i.e. the API will return 1 MB of data per scan.

A Scan operation performs eventually consistent reads by default, and it can return up to 1 MB (one page) of data.

Scan API

Map<String, AttributeValue> lastKeyEvaluated = null;
do {
    ScanRequest scanRequest = new ScanRequest()
        .withTableName("ProductCatalog")
        .withLimit(10)
        .withExclusiveStartKey(lastKeyEvaluated);

    ScanResult result = client.scan(scanRequest);
    for (Map<String, AttributeValue> item : result.getItems()){
        printItem(item);
    }
    lastKeyEvaluated = result.getLastEvaluatedKey();
} while (lastKeyEvaluated != null);
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot @notionquest....i was able to retrieve all the items could you also help me with a code on how to retrieve a single item based on primary key..
Use getItem Api.. docs.aws.amazon.com/amazondynamodb/latest/developerguide/….. Refer retrieveItem() method.
I use this code and can confirm that this is working well.
0

Here is example how to read data using Scan API :

@Override
protected ArrayList<String> doInBackground(String... params) {
    String tableName = params[0];

    ArrayList<String> tempList = new ArrayList<String>();

    AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient (
            new BasicAWSCredentials(Constants.ACCESS_KEY_ID,
                    Constants.SECRET_KEY));

    ScanRequest scanRequest = new ScanRequest()
        .withTableName(tableName);
        //.withAttributesToGet("name");
    com.amazonaws.services.dynamodb.model.ScanResult result = dynamoDBClient.scan(scanRequest);


    for (Map<String, AttributeValue> item : result.getItems()) {
        tempList.add(item.toString());
        //analizeItem(tempList, item);
    }

    return tempList;
}

Reference from programcreeks

1 Comment

Won't this only get the first set of items from the scan? If there's more than a page's worth, it will not get the entire table unless you use withExclusiveStartKey , correct? docs.aws.amazon.com/amazondynamodb/latest/APIReference/…
0

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.AP_SOUTH_1).build(); DynamoDB dynamoDB = new DynamoDB(client);

    Table table = dynamoDB.getTable("Student");

    Item item = table.getItem("PK", "portion Key","SK","Sort Key");
    System.out.println(item.toJSONPretty());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.