public boolean deleteAllItems(String tableName) throws DataAccessException {
    ScanRequest request = new ScanRequest().withTableName(tableName);
    boolean deleted = false;
    ScanResult result = ddb.scan(request);
    for (Map<String, AttributeValue> item : result.getItems()) {
      Key key = DynamoDBUtil.getIdKey(item);
      deleteItem(tableName, key);
      deleted = true;
    }

    // keep repeating until we get through all matched items
    Key lastKeyEvaluated = null;
    do {
      lastKeyEvaluated = result.getLastEvaluatedKey();
      if (lastKeyEvaluated != null) {
        request = new ScanRequest(tableName).withExclusiveStartKey(lastKeyEvaluated);
        result = ddb.scan(request);
        for (Map<String, AttributeValue> item : result.getItems()) {
          Key key = DynamoDBUtil.getIdKey(item);
          deleteItem(tableName, key);
          deleted = true;
        }
      }
    } while (lastKeyEvaluated != null);

    return deleted;
  }
 @Override
 public void onScanResult(int callbackType, ScanResult result) {
   Log.i("callbackType", String.valueOf(callbackType));
   Log.i("result", result.toString());
   BluetoothDevice btDevice = result.getDevice();
   connectToDevice(btDevice);
 }
  private List<Map<String, AttributeValue>> scanInternal(
      String tableName, Map<String, Condition> filter, int max, int attempt) {
    LinkedList<Map<String, AttributeValue>> items = new LinkedList<Map<String, AttributeValue>>();
    try {
      ScanRequest request = new ScanRequest(tableName).withScanFilter(filter);
      ScanResult result = ddb.scan(request);
      items.addAll(result.getItems());

      // keep repeating until we get through all matched items
      Key lastKeyEvaluated = null;
      do {
        lastKeyEvaluated = result.getLastEvaluatedKey();
        if (lastKeyEvaluated != null) {
          request =
              new ScanRequest(tableName)
                  .withScanFilter(filter)
                  .withExclusiveStartKey(lastKeyEvaluated);
          result = ddb.scan(request);
          items.addAll(result.getItems());
        }
      } while (lastKeyEvaluated != null && items.size() < max);

      // truncate if needed
      while (items.size() > max) {
        items.removeLast();
      }

      return items;
    } 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 scanInternal(tableName, filter, max, attempt);
      } else {
        throw new DataStoreOperationException(
            "problem with table: " + tableName + ", filter: " + filter, e);
      }
    }
  }
 @Override
 protected void innerScanContainerShallow(
     final BeanObjectContainer container, final ScanResult result) {
   for (final Field field : container.getType().getType().getFields()) {
     result.addPropertyField(
         new SimplePropertyBeanPathElement(
             getContext(), container, new BeanObjectAccessor(getContext(), container, field)));
   }
   for (final Method method : container.getType().getType().getMethods()) {
     if (BeanPathStrings.startsWithAny(
         method.getName(), BeanPathReflections.PROPERTY_METHOD_PREFIXES)) {
       result.addPropertyMethod(
           new SimplePropertyBeanPathElement(
               getContext(), container, new BeanObjectAccessor(getContext(), container, method)));
     } else {
       result.addActionMethod(
           new SimpleActionBeanPathElement(
               getContext(), container, new BeanObjectAccessor(getContext(), container, method)));
     }
   }
 }
 @Override
 public void onBatchScanResults(List<ScanResult> results) {
   for (ScanResult sr : results) {
     Log.i("ScanResult - Results", sr.toString());
   }
 }
示例#6
0
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String category = request.getParameter("category");
    HashMap<String, Condition> scanFilter = new HashMap<String, Condition>();
    Condition condition =
        new Condition()
            .withComparisonOperator(ComparisonOperator.NE.toString())
            .withAttributeValueList(new AttributeValue().withN("0"));
    Condition condition2 =
        new Condition()
            .withComparisonOperator(ComparisonOperator.NE.toString())
            .withAttributeValueList(new AttributeValue().withN("0"));
    Condition condition3 = new Condition().withComparisonOperator(ComparisonOperator.CONTAINS);

    scanFilter.put("geoLat", condition);
    scanFilter.put("geoLng", condition2);
    if (category != null && !category.isEmpty()) {
      condition3.withAttributeValueList(new AttributeValue().withS(category));
      scanFilter.put("category", condition3);
    }

    // String queueUrl = SQS.getQueueUrl(new GetQueueUrlRequest(SQS_QUEUE_NAME)).getQueueUrl();

    String tableName = DYNAMODB_TABLE_NAME;
    ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter);
    ScanResult scanResult = DYNAMODB.scan(scanRequest);

    int size = scanResult.getItems().size();
    ArrayList<HashMap<String, String>> tweets = new ArrayList<HashMap<String, String>>();

    for (int i = 0; i < size; i++) {
      // Get latitude, longitude, content, username, created (long), category, sentiment
      String categorydb = "no category";
      String sentiment = "no sentiment";
      if (scanResult.getItems().get(i).get("category") != null) {
        categorydb = scanResult.getItems().get(i).get("category").getS();
        sentiment = scanResult.getItems().get(i).get("sentiment").getS();
        String lat = scanResult.getItems().get(i).get("geoLat").getN();
        String lng = scanResult.getItems().get(i).get("geoLng").getN();
        String content = scanResult.getItems().get(i).get("content").getS();
        String username = scanResult.getItems().get(i).get("username").getS();
        String created = scanResult.getItems().get(i).get("createdLong").getN();
        String createdDate = scanResult.getItems().get(i).get("createdDate").getS();

        // Format date.
        DateFormat fromFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        DateFormat toFormat = new SimpleDateFormat("MMM dd · k:mm z");
        Date date;
        String createdstr;
        try {
          date = fromFormat.parse(createdDate);
          createdstr = toFormat.format(date);
        } catch (ParseException e) {
          createdstr = createdDate;
        }

        // Create tweet hash.
        HashMap<String, String> tweet = new HashMap<String, String>();
        tweet.put("lat", lat);
        tweet.put("lng", lng);
        tweet.put("content", content);
        tweet.put("username", username);
        tweet.put("category", categorydb);
        tweet.put("sentiment", sentiment);
        tweet.put("created", created);
        tweet.put("createdstr", createdstr);

        // Order tweet by time created. Most recent at the top of the list.
        int position = 0;
        while (position < tweets.size()
            && Long.parseLong(tweets.get(position).get("created")) > Long.parseLong(created)) {
          position++;
        }
        tweets.add(position, tweet);
      }
      // else {
      // Send tweet with blank category for sentimental processing
      //    String id = scanResult.getItems().get(i).get("id").getS();
      //    SQS.sendMessage(new SendMessageRequest(queueUrl, id));
      // }

    }

    // Log result.
    System.out.println("Successfully handled GET request.");
    if (category != null) {
      System.out.println("category:" + category);
    }
    System.out.println("size: " + tweets.size());

    // Convert object to JSON format.
    String json = new Gson().toJson(tweets);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);
  }