我通过此处链接的答案找到了一些有关如何完成此任务的帮助。但它不适合我的用例。我的目标是获取 DynamoDB 表中的所有记录(用户名和电子邮件)。浏览文档我必须使用 LastEvaluatedKey 或 ExclusiveStartKey 来实现分页。任何指导将不胜感激。
谢谢
我通过此处链接的答案找到了一些有关如何完成此任务的帮助。但它不适合我的用例。我的目标是获取 DynamoDB 表中的所有记录(用户名和电子邮件)。浏览文档我必须使用 LastEvaluatedKey 或 ExclusiveStartKey 来实现分页。任何指导将不胜感激。
谢谢
此查询将能够从 DynamoDB 表中获取所有记录。
function scanAllData($table,$limit){
$result = $this->getClientdb()->scan(array(
'TableName' => $table,
'Limit' => $limit,
'Select' => 'ALL_ATTRIBUTES'
),
array('limit' => $limit),
);
return $result['Items'];
}
你可以像这样调用这个函数。例如,您有“用户”表,列是usernames
和emails
$getobj = $this->scanAllData('users','10');
foreach($getobj as $cols){
echo $cols['usernames']['S'];
echo $cols['emails']['S'];
}
我不知道 PHP,但这是一个针对DynamoDB Local运行的 Java 8 示例,它依赖于compile group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.10.20'
. 这显示了Limit
(大多数情况下,它没有显示过滤器如何影响返回的项目)LastEvaluatedKey
、、和ExclusiveStartKey
工作方式。您可以查看参数是如何设置的,并将其与 PHP 示例进行比较:
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.amazonaws.services.dynamodbv2.util.Tables;
import com.google.common.collect.ImmutableList;
import java.util.Map;
public class ScanExample {
private static final String TABLE_NAME = "test_table";
private static final String HASH_ATTR_NAME = "hash";
public static void main(String[] args) throws InterruptedException {
AWSCredentials awsCredentials = new BasicAWSCredentials("key", "secret");
AmazonDynamoDB dynamoDBClient = new AmazonDynamoDBClient(awsCredentials);
dynamoDBClient.setEndpoint("http://localhost:4000");
if (Tables.doesTableExist(dynamoDBClient, TABLE_NAME)) {
dynamoDBClient.deleteTable(TABLE_NAME);
}
CreateTableRequest createTableRequest = new CreateTableRequest();
createTableRequest.setTableName(TABLE_NAME);
createTableRequest.setProvisionedThroughput(new ProvisionedThroughput(50l, 50l));
createTableRequest.withKeySchema(
ImmutableList.of(new KeySchemaElement(HASH_ATTR_NAME, KeyType.HASH)))
.withAttributeDefinitions(ImmutableList.of(
new AttributeDefinition(HASH_ATTR_NAME, ScalarAttributeType.N))
);
dynamoDBClient.createTable(createTableRequest);
Tables.awaitTableToBecomeActive(dynamoDBClient, TABLE_NAME);
final Table table = new DynamoDB(dynamoDBClient).getTable(TABLE_NAME);
createItems(table, 8);
final int limit = 3;
performScan(dynamoDBClient, limit);
}
private static void performScan(final AmazonDynamoDB client, final int limit) {
ScanRequest scanRequest = new ScanRequest(TABLE_NAME)
.withLimit(limit);
Map<String, AttributeValue> exclusiveStartKey = null;
do {
final ScanResult scanResult = client.scan(scanRequest);
System.out.println("With exclusiveStartKey=" + exclusiveStartKey);
scanResult.getItems().forEach(System.out::println);
exclusiveStartKey = scanResult.getLastEvaluatedKey();
System.out.println("Result lastEvaluatedKey=" + exclusiveStartKey);
// Reusing same request object, just setting the start key
scanRequest.setExclusiveStartKey(exclusiveStartKey);
System.out.println();
} while(exclusiveStartKey != null);
}
private static void createItems(final Table table, final int n) {
for (int i = 0; i < n; i++) {
table.putItem(new Item().withNumber(HASH_ATTR_NAME, i));
}
}
}
示例输出:
With exclusiveStartKey=null
{hash={N: 2,}}
{hash={N: 1,}}
{hash={N: 3,}}
Result lastEvaluatedKey={hash={N: 3,}}
With exclusiveStartKey={hash={N: 3,}}
{hash={N: 5,}}
{hash={N: 7,}}
{hash={N: 0,}}
Result lastEvaluatedKey={hash={N: 0,}}
With exclusiveStartKey={hash={N: 0,}}
{hash={N: 6,}}
{hash={N: 4,}}
Result lastEvaluatedKey=null