下面列出了怎么用com.amazonaws.services.dynamodbv2.model.BatchGetItemResult的API类实例代码及写法,或者点击链接到github查看源代码。
private BatchGetItemResult getResults(
final Map<String, KeysAndAttributes> requestItems,
final short adapterId,
final Map<ByteArray, GeoWaveRow> resultMap) {
final BatchGetItemRequest request = new BatchGetItemRequest(requestItems);
final BatchGetItemResult result = client.batchGetItem(request);
result.getResponses().values().forEach(results -> results.stream().forEach(objMap -> {
final byte[] dataId = objMap.get(DynamoDBRow.GW_PARTITION_ID_KEY).getB().array();
final AttributeValue valueAttr = objMap.get(DynamoDBRow.GW_VALUE_KEY);
final byte[] value = valueAttr == null ? null : valueAttr.getB().array();
final AttributeValue visAttr = objMap.get(DynamoDBRow.GW_VISIBILITY_KEY);
final byte[] vis = visAttr == null ? new byte[0] : visAttr.getB().array();
resultMap.put(
new ByteArray(dataId),
DataIndexUtils.deserializeDataIndexRow(dataId, adapterId, value, vis));
}));
return result;
}
@Test
public void test_batchGetItem_WithAllParameters() throws Exception {
String TEST_ATTRIBUTE_2 = "Attribute2";
String TEST_ATTRIBUTE_VALUE_2 = "AttributeValue2";
createTable();
putItem(TEST_ATTRIBUTE, TEST_ATTRIBUTE_VALUE);
putItem(TEST_ATTRIBUTE_2, TEST_ATTRIBUTE_VALUE_2);
List<Map<String, AttributeValue>> keys = new ArrayList<Map<String, AttributeValue>>();
Map<String, AttributeValue> key1 = new HashMap<String, AttributeValue>();
key1.put(TEST_ATTRIBUTE, new AttributeValue()
.withS(TEST_ATTRIBUTE_VALUE));
keys.add(key1);
Map<String, AttributeValue> key2 = new HashMap<String, AttributeValue>();
key2.put(TEST_ATTRIBUTE_2, new AttributeValue()
.withS(TEST_ATTRIBUTE_VALUE_2));
keys.add(key2);
Map<String, KeysAndAttributes> requestItems = new HashMap<String, KeysAndAttributes>();
requestItems.put(TEST_TABLE_NAME, new KeysAndAttributes()
.withKeys(keys));
String returnConsumedCapacity = "";
BatchGetItemResult result = dynamoDb.batchGetItem(requestItems,returnConsumedCapacity);
String tableName = result.getResponses().keySet().toArray(new String[1])[0];
List<Map<String, AttributeValue>> items = result.getResponses().get(tableName);
AttributeValue value1 = items.get(0).get(TEST_ATTRIBUTE);
AttributeValue value2 = items.get(1).get(TEST_ATTRIBUTE_2);
assertThat(tableName, equalTo(TEST_TABLE_NAME));
assertThat(items.size(), equalTo(keys.size()));
assertThat(value1.getS(), equalTo(TEST_ATTRIBUTE_VALUE));
assertThat(value2.getS(), equalTo(TEST_ATTRIBUTE_VALUE_2));
}
public Iterator<GeoWaveRow> getRowsFromDataIndex(
final byte[][] dataIds,
final short adapterId,
final String typeName) {
final Map<ByteArray, GeoWaveRow> resultMap = new HashMap<>();
final Iterator<byte[]> dataIdIterator = Arrays.stream(dataIds).iterator();
while (dataIdIterator.hasNext()) {
// fill result map
final Collection<Map<String, AttributeValue>> dataIdsForRequest = new ArrayList<>();
int i = 0;
while (dataIdIterator.hasNext() && (i < MAX_ROWS_FOR_BATCHGETITEM)) {
dataIdsForRequest.add(
Collections.singletonMap(
DynamoDBRow.GW_PARTITION_ID_KEY,
new AttributeValue().withB(ByteBuffer.wrap(dataIdIterator.next()))));
i++;
}
BatchGetItemResult result =
getResults(
Collections.singletonMap(
typeName + "_" + getQualifiedTableName(DataIndexUtils.DATA_ID_INDEX.getName()),
new KeysAndAttributes().withKeys(dataIdsForRequest)),
adapterId,
resultMap);
while (!result.getUnprocessedKeys().isEmpty()) {
result = getResults(result.getUnprocessedKeys(), adapterId, resultMap);
}
}
return Arrays.stream(dataIds).map(d -> resultMap.get(new ByteArray(d))).filter(
r -> r != null).iterator();
}
private void testBatchGetItemsContainsItem(
final TransactionManagerDynamoDBFacade facade,
final Map<String, AttributeValue> item,
final boolean filterAttributes) {
BatchGetItemRequest batchGetItemRequest = createBatchGetItemRequest(filterAttributes);
BatchGetItemResult batchGetItemResult = facade.batchGetItem(batchGetItemRequest);
List<Map<String, AttributeValue>> items = batchGetItemResult.getResponses().get(INTEG_HASH_TABLE_NAME);
assertEquals(1, items.size());
assertContainsNoTransactionAttributes(items.get(0));
assertEquals(item, items.get(0));
}
private void testBatchGetItemsIsEmpty(final TransactionManagerDynamoDBFacade facade) {
BatchGetItemRequest batchGetItemRequest = createBatchGetItemRequest(false);
BatchGetItemResult batchGetItemResult = facade.batchGetItem(batchGetItemRequest);
assertNotNull(batchGetItemResult.getResponses());
assertEquals(1, batchGetItemResult.getResponses().size());
assertNotNull(batchGetItemResult.getResponses().get(INTEG_HASH_TABLE_NAME));
assertEquals(0, batchGetItemResult.getResponses().get(INTEG_HASH_TABLE_NAME).size());
}
@Override
public BatchGetItemResult batchGetItem(
Map<String, KeysAndAttributes> requestItems,
String returnConsumedCapacity) throws AmazonServiceException,
AmazonClientException {
BatchGetItemRequest request = new BatchGetItemRequest()
.withRequestItems(requestItems)
.withReturnConsumedCapacity(returnConsumedCapacity);
return batchGetItem(request);
}
@Override
public BatchGetItemResult batchGetItem(
Map<String, KeysAndAttributes> requestItems)
throws AmazonServiceException, AmazonClientException {
BatchGetItemRequest request = new BatchGetItemRequest()
.withRequestItems(requestItems);
return batchGetItem(request);
}
@Override
public BatchGetItemResult batchGetItem(
Map<String, KeysAndAttributes> requestItems,
String returnConsumedCapacity) throws AmazonServiceException,
AmazonClientException {
throw new UnsupportedOperationException("Use the underlying client instance instead");
}
@Override
public BatchGetItemResult batchGetItem(BatchGetItemRequest batchGetItemRequest) {
throw new UnsupportedOperationException();
}
@Override
public BatchGetItemResult batchGetItem(Map<String, KeysAndAttributes> requestItems, String returnConsumedCapacity) {
throw new UnsupportedOperationException();
}
@Override
public BatchGetItemResult batchGetItem(Map<String, KeysAndAttributes> requestItems) {
throw new UnsupportedOperationException();
}
@Override
public BatchGetItemResult batchGetItem(BatchGetItemRequest request) throws AmazonServiceException, AmazonClientException {
return getBackend().batchGetItem(request);
}
@Override
public BatchGetItemResult batchGetItem(Map<String, KeysAndAttributes> requestItems, String returnConsumedCapacity) throws AmazonServiceException, AmazonClientException {
return getBackend().batchGetItem(requestItems, returnConsumedCapacity);
}
@Override
public BatchGetItemResult batchGetItem(Map<String, KeysAndAttributes> requestItems) throws AmazonServiceException, AmazonClientException {
return getBackend().batchGetItem(requestItems);
}
@Override
public BatchGetItemResult batchGetItem(BatchGetItemRequest arg0)
throws AmazonServiceException, AmazonClientException {
throw new UnsupportedOperationException("Use the underlying client instance instead");
}
@Override
public BatchGetItemResult batchGetItem(
Map<String, KeysAndAttributes> requestItems)
throws AmazonServiceException, AmazonClientException {
throw new UnsupportedOperationException("Use the underlying client instance instead");
}