我找到了一些有关如何通过链接here的答案完成此操作的帮助。 但它不适合我的用例。 我的目标是获取DynamoDB表中的所有记录(用户名和电子邮件)。通过doc查看我必须使用LastEvaluatedKey或ExclusiveStartKey来实现分页。任何指导将不胜感激。
由于
答案 0 :(得分:3)
此查询将能够从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'];
}
答案 1 :(得分:1)
我不了解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