Ejemplo n.º 1
0
  @Test
  public void queryWithUnknownHashRangeKey2() {
    putItemWithHashKeyAndRangeKey();

    String hashCode = "hash2";

    Condition rangeKeyCondition = new Condition();
    List<AttributeValue> attributeValueList = new ArrayList<AttributeValue>();
    attributeValueList.add(new AttributeValue().withS("range2x"));
    attributeValueList.add(new AttributeValue().withS("range2y"));
    rangeKeyCondition.setAttributeValueList(attributeValueList);
    rangeKeyCondition.setComparisonOperator(ComparisonOperator.BETWEEN);

    TestClassWithHashRangeKey hashKeyTemplate = new TestClassWithHashRangeKey();
    hashKeyTemplate.setHashCode(hashCode);

    DynamoDBQueryExpression<TestClassWithHashRangeKey> query =
        new DynamoDBQueryExpression<TestClassWithHashRangeKey>()
            .withHashKeyValues(hashKeyTemplate)
            .withRangeKeyCondition("rangeCode", rangeKeyCondition);

    List<TestClassWithHashRangeKey> valueList =
        mapper.query(TestClassWithHashRangeKey.class, query);
    Assert.assertNotNull("Value list is null.", valueList);
    Assert.assertEquals("Value list should be empty.", 0, valueList.size());
  }
Ejemplo n.º 2
0
  @Test
  public void queryWithHashRangeKey() {
    putItemWithHashKeyAndRangeKey();

    TestClassWithHashRangeKey value2c = new TestClassWithHashRangeKey();
    value2c.setHashCode("hash2");
    value2c.setRangeCode("range2c");
    value2c.setStringData("string2c");
    value2c.setIntData(23);
    value2c.setStringSetData(set("stringSetVal2c1", "stringSetVal2c2"));
    value2c.setNumberSetData(set(7, 8));
    mapper.save(value2c);

    TestClassWithHashRangeKey value2d = new TestClassWithHashRangeKey();
    value2d.setHashCode("hash2");
    value2d.setRangeCode("range2d");
    value2d.setStringData("string2d");
    value2d.setIntData(24);
    value2d.setStringSetData(set("stringSetVal2d1", "stringSetVal2d2"));
    value2d.setNumberSetData(set(9, 10));
    mapper.save(value2d);

    TestClassWithHashRangeKey value2e = new TestClassWithHashRangeKey();
    value2e.setHashCode("hash2");
    value2e.setRangeCode("range2e");
    value2e.setStringData("string2e");
    value2e.setIntData(25);
    value2e.setStringSetData(set("stringSetVal2e1", "stringSetVal2e2"));
    value2e.setNumberSetData(set(11, 12));
    mapper.save(value2e);

    String hashCode = "hash2";
    TestClassWithHashRangeKey hashKeyTemplate = new TestClassWithHashRangeKey();
    hashKeyTemplate.setHashCode(hashCode);

    Condition rangeKeyCondition = new Condition();
    List<AttributeValue> rangeValues = new ArrayList<AttributeValue>();
    rangeValues.add(new AttributeValue().withS("range2c"));
    rangeValues.add(new AttributeValue().withS("range2d"));
    rangeKeyCondition.setComparisonOperator(ComparisonOperator.BETWEEN);
    rangeKeyCondition.setAttributeValueList(rangeValues);

    DynamoDBQueryExpression<TestClassWithHashRangeKey> query =
        new DynamoDBQueryExpression<TestClassWithHashRangeKey>()
            .withHashKeyValues(hashKeyTemplate)
            .withRangeKeyCondition("rangeCode", rangeKeyCondition);

    List<TestClassWithHashRangeKey> valueList =
        mapper.query(TestClassWithHashRangeKey.class, query);
    Assert.assertNotNull("Value list is null.", valueList);
    Assert.assertNotSame("Value list is empty.", 0, valueList.size());

    Assert.assertEquals("Value list should have 2 items.", 2, valueList.size());

    TestClassWithHashRangeKey value = valueList.get(0);
    Assert.assertEquals("Wrong hashCode.", hashCode, value.getHashCode());
    Assert.assertEquals("Wrong rangeCode.", "range2c", value.getRangeCode());
    Assert.assertEquals("Wrong stringData.", "string2c", value.getStringData());
    Assert.assertEquals("Wrong intData.", 23, value.getIntData());
    Assert.assertEquals(
        "Wrong stringSetData.",
        set("stringSetVal2c1", "stringSetVal2c2"),
        value.getStringSetData());
    Assert.assertEquals("Wrong numberSetData.", set(7, 8), value.getNumberSetData());

    value = valueList.get(1);
    Assert.assertEquals("Wrong hashCode.", hashCode, value.getHashCode());
    Assert.assertEquals("Wrong rangeCode.", "range2d", value.getRangeCode());
    Assert.assertEquals("Wrong stringData.", "string2d", value.getStringData());
    Assert.assertEquals("Wrong intData.", 24, value.getIntData());
    Assert.assertEquals(
        "Wrong stringSetData.",
        set("stringSetVal2d1", "stringSetVal2d2"),
        value.getStringSetData());
    Assert.assertEquals("Wrong numberSetData.", set(9, 10), value.getNumberSetData());
  }
Ejemplo n.º 3
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);
  }