Exemplo n.º 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;
  }
Exemplo n.º 2
0
  /*
   * (non-Javadoc)
   *
   * @see com.dtcc.awsticker.dao.IDaoTicker#getTickerRow(java.lang.Integer)
   */
  public TickerRow getTickerRow(Integer index) {
    // return LocalTickerTable.get(index);

    AttributeValue id = new AttributeValue().withN(index + "");
    Key primaryKey = new Key().withHashKeyElement(id);
    GetItemRequest request = new GetItemRequest().withTableName(tableName).withKey(primaryKey);

    GetItemResult result = db.getItem(request);
    if (result.getItem() == null) return null;
    TickerRow tr = new TickerRow();
    tr.setIndex(index);
    ItemToModel(result.getItem(), tr);

    return tr;
  }
Exemplo n.º 3
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;
  }