示例#1
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;
  }
示例#2
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;
  }