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;
  }
Пример #3
0
 // TODO: test out put item expected and return value
 @Test
 public void getItemTest() {
   AttributeValue hash = createItem(tableName);
   GetItemRequest request = new GetItemRequest().withTableName(tableName);
   request.setKey(new Key().withHashKeyElement(hash));
   GetItemResult res = getClient().getItem(request);
   Assert.assertNotNull(res.getItem());
   Assert.assertEquals(res.getItem().get("id"), hash);
 }
Пример #4
0
 @Test
 public void getItemWithoutKeyTest() {
   GetItemRequest request = new GetItemRequest();
   request.setTableName(tableName);
   try {
     getClient().getItem(request);
     Assert.assertTrue(false); // Should have thrown an exception
   } catch (AmazonServiceException ase) {
   }
 }
Пример #5
0
 @Test
 public void getItemWithoutTableNameTest() {
   GetItemRequest request = new GetItemRequest();
   request.setKey(new Key().withHashKeyElement(new AttributeValue().withNS("123")));
   try {
     getClient().getItem(request);
     Assert.assertTrue(false); // Should have thrown an exception
   } catch (AmazonServiceException ase) {
   }
 }
Пример #6
0
  @Override
  public int scan(
      String table,
      String startkey,
      int recordcount,
      Set<String> fields,
      Vector<HashMap<String, ByteIterator>> result) {
    logger.debug("scan " + recordcount + " records from key: " + startkey + " on table: " + table);
    /*
     * on DynamoDB's scan, startkey is *exclusive* so we need to
     * getItem(startKey) and then use scan for the res
     */
    GetItemRequest greq = new GetItemRequest(table, createPrimaryKey(startkey));
    greq.setAttributesToGet(fields);

    GetItemResult gres = null;

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

    if (null != gres.getItem()) {
      result.add(extractResult(gres.getItem()));
    }

    int count = 1; // startKey is done, rest to go.

    Key startKey = createPrimaryKey(startkey);
    ScanRequest req = new ScanRequest(table);
    req.setAttributesToGet(fields);
    while (count < recordcount) {
      req.setExclusiveStartKey(startKey);
      req.setLimit(recordcount - count);
      ScanResult res = null;
      try {
        res = dynamoDB.scan(req);
      } catch (AmazonServiceException ex) {
        logger.error(ex.getMessage());
        ex.printStackTrace();
        return SERVER_ERROR;
      } catch (AmazonClientException ex) {
        logger.error(ex.getMessage());
        ex.printStackTrace();
        return CLIENT_ERROR;
      }

      count += res.getCount();
      for (Map<String, AttributeValue> items : res.getItems()) {
        result.add(extractResult(items));
      }
      startKey = res.getLastEvaluatedKey();
    }

    return OK;
  }