Example #1
0
  /**
   * Decorates the given <code>updateItemRequest</code> with attributes required for geo spatial
   * querying.
   *
   * @param attributeValueMap the items that needs to be decorated with geo attributes
   * @param latitude the latitude that needs to be attached with the item
   * @param longitude the longitude that needs to be attached with the item
   * @param configs the collection of configurations to be used for decorating the request with geo
   *     attributes
   */
  public void updateAttributeValues(
      Map<String, AttributeValue> attributeValueMap,
      double latitude,
      double longitude,
      List<GeoConfig> configs) {
    if (configs == null) {
      throw new IllegalArgumentException("Geo configs should not be null");
    }
    for (GeoConfig config : configs) {
      // Fail-fast if any of the preconditions fail
      checkConfigParams(
          config.getGeoIndexName(),
          config.getGeoHashKeyColumn(),
          config.getGeoHashColumn(),
          config.getGeoHashKeyLength());

      long geohash = s2Manager.generateGeohash(latitude, longitude);
      long geoHashKey = s2Manager.generateHashKey(geohash, config.getGeoHashKeyLength());

      // Decorate the request with the geohash
      AttributeValue geoHashValue = new AttributeValue().withN(Long.toString(geohash));
      attributeValueMap.put(config.getGeoHashColumn(), geoHashValue);

      AttributeValue geoHashKeyValue;
      if (config.getHashKeyDecorator().isPresent()
          && config.getCompositeHashKeyColumn().isPresent()) {
        AttributeValue compositeHashKeyValue =
            attributeValueMap.get(config.getCompositeHashKeyColumn().get());
        if (compositeHashKeyValue == null) {
          continue;
        }
        String compositeColumnValue = compositeHashKeyValue.getS();
        String hashKey =
            config.getHashKeyDecorator().get().decorate(compositeColumnValue, geoHashKey);
        // Decorate the request with the composite geoHashKey (type String)
        geoHashKeyValue = new AttributeValue().withS(String.valueOf(hashKey));
      } else {
        // Decorate the request with the geoHashKey (type Number)
        geoHashKeyValue = new AttributeValue().withN(String.valueOf(geoHashKey));
      }
      attributeValueMap.put(config.getGeoHashKeyColumn(), geoHashKeyValue);
    }
  }
Example #2
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);
  }