如何使用 Java 从 dynamodb 读取 table?

How to read a table from dynamodb using Java?

我在 Amazon dynamodb 中使用主键 Issue(String) 创建了一个 table,它的数据存储在 it.I 中,想从我的 table 中读取值。我正在使用以下代码..

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

我在调用 getTable 方法时遇到错误....它是像 createTable() 一样的预定义方法还是我们需要自己编写..如果是这样怎么办? 还有应该使用什么方法来读取 table.. 中的所有项目? 我用这个 link 写了一些代码... http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPIItemCRUD.html#JavaDocumentAPIGetItem

我是新手 Java 请帮忙..

下面是如何使用 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;
}

引用自programcreeks

扫描API可用于获取table中的所有项目。

应该进行扫描,直到 LastEvaluatedKey 不为空,这对于获取所有项目非常重要。否则,如果 table 有很多项目,即 API 将 return 每次扫描 1 MB 的数据。

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);

AmazonDynamoDB 客户端 = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.AP_SOUTH_1).build(); DynamoDB dynamoDB = new DynamoDB(客户端);

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

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