private Map<String, AttributeValue> getConsistentInternal(
     String tableName, Key key, int attempt) {
   GetItemRequest request = new GetItemRequest(tableName, key);
   request.setConsistentRead(true);
   try {
     GetItemResult result = ddb.getItem(request);
     Map<String, AttributeValue> attributes = result.getItem();
     if (attributes == null || attributes.isEmpty()) {
       return null;
     }
     return attributes;
   } catch (AmazonServiceException e) {
     if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
       throw new IllegalArgumentException("no such table: " + tableName, e);
     } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) {
       // retry after a small pause
       DynamoDBUtil.sleepBeforeRetry(attempt);
       attempt++;
       return getConsistentInternal(tableName, key, attempt);
     } else {
       throw new DataStoreOperationException(
           "problem with table: " + tableName + ", key: " + key, e);
     }
   }
 }
예제 #2
0
  @Override
  public int read(
      String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {

    logger.debug("readkey: " + key + " from table: " + table);
    GetItemRequest req = new GetItemRequest(table, createPrimaryKey(key));
    req.setAttributesToGet(fields);
    req.setConsistentRead(consistentRead);
    GetItemResult res = null;

    try {
      res = dynamoDB.getItem(req);
    } catch (AmazonServiceException ex) {
      logger.error(ex.getMessage());
      return SERVER_ERROR;
    } catch (AmazonClientException ex) {
      logger.error(ex.getMessage());
      return CLIENT_ERROR;
    }

    if (null != res.getItem()) {
      result.putAll(extractResult(res.getItem()));
      logger.debug("Result: " + res.toString());
    }
    return OK;
  }