예제 #1
0
  private void printNodeHistoryItems(String nid, String time) {

    String hashKey = nid;
    String rangeKey = time;
    NodeHistory nhKey = new NodeHistory();
    nhKey.setNodeID(hashKey);
    List<NodeHistory> latestNodeHistory;

    if (time != null) {
      Condition rangeKeyCondition =
          new Condition()
              .withComparisonOperator(ComparisonOperator.EQ.toString())
              .withAttributeValueList(new AttributeValue().withS(rangeKey));
      DynamoDBQueryExpression<NodeHistory> queryExpression =
          new DynamoDBQueryExpression<NodeHistory>()
              .withHashKeyValues(nhKey)
              .withRangeKeyCondition("Time", rangeKeyCondition);
      latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);
    } else {
      DynamoDBQueryExpression<NodeHistory> queryExpression =
          new DynamoDBQueryExpression<NodeHistory>().withHashKeyValues(nhKey);
      latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);
    }

    for (NodeHistory nh : latestNodeHistory) {
      System.out.format(
          "SerialNumber=%s, Time=%s, SAValues=%s\n",
          nh.getNodeID(), nh.getTime(), nh.getSAValues());
    }
  }
예제 #2
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());
  }
예제 #3
0
  @Test
  public void queryWithHashKey() {
    putItemWithHashKey();
    putItemWithHashKeyOverwriteItem();

    String code = "hash1";
    TestClassWithHashKey hashKeyTemplate = new TestClassWithHashKey();
    hashKeyTemplate.setCode(code);

    Map<String, Condition> emptyRangeConditions = new HashMap<String, Condition>();

    DynamoDBQueryExpression<TestClassWithHashKey> query =
        new DynamoDBQueryExpression<TestClassWithHashKey>()
            .withHashKeyValues(hashKeyTemplate)
            .withRangeKeyConditions(emptyRangeConditions);

    List<TestClassWithHashKey> valueList = mapper.query(TestClassWithHashKey.class, query);
    Assert.assertNotNull("Value list is null.", valueList);
    Assert.assertNotSame("Value list is empty.", 0, valueList.size());
    Assert.assertEquals("Value list has more than one item.", 1, valueList.size());

    TestClassWithHashKey value = valueList.get(0);
    Assert.assertEquals("Wrong code.", code, value.getCode());
    Assert.assertEquals("Wrong stringData.", "string1", value.getStringData());
    Assert.assertEquals("Wrong intData.", 1, value.getIntData());
    Assert.assertEquals(
        "Wrong stringSetData.", set("stringSetVal1", "stringSetVal2"), value.getStringSetData());
    Assert.assertEquals("Wrong numberSetData.", set(1, 2), value.getNumberSetData());
  }
예제 #4
0
  private void createNodeHistoryItems(
      NodeHistory nHistoryDB, String nid, String time, String values) {
    nHistoryDB.setNodeID(nid);
    nHistoryDB.setTime(time);
    nHistoryDB.setSAValues(values);

    mapper.save(nHistoryDB);

    String hashKey = nid;
    String rangeKey = time;
    NodeHistory nhKey = new NodeHistory();
    nhKey.setNodeID(hashKey);

    Condition rangeKeyCondition =
        new Condition()
            .withComparisonOperator(ComparisonOperator.EQ.toString())
            .withAttributeValueList(new AttributeValue().withS(rangeKey));
    DynamoDBQueryExpression<NodeHistory> queryExpression =
        new DynamoDBQueryExpression<NodeHistory>()
            .withHashKeyValues(nhKey)
            .withRangeKeyCondition("Time", rangeKeyCondition);

    List<NodeHistory> latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);

    System.out.println("Item created: ");
    for (NodeHistory nh : latestNodeHistory) {
      System.out.format(
          "SerialNumber=%s, Time=%s, SAValues=%s\n",
          nh.getNodeID(), nh.getTime(), nh.getSAValues());
    }
  }
예제 #5
0
  // range format example = (7L*24L*60L*60L*1000L) (1 week)
  public List<NodeHistoryEntity> findSpecificNodeHistoryInRangeTime(String nid, Long range) {
    String hashKey = nid;
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MMdd'T'HH:mm:ss.SSS'Z'");

    Long currentTimeMilli = (new Date()).getTime();
    Long rangeTimeMilli = (new Date()).getTime() - range;
    dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    String currentTime = dateFormatter.format(currentTimeMilli);
    String rangeTime = dateFormatter.format(rangeTimeMilli);

    NodeHistory nhKey = new NodeHistory();
    nhKey.setNodeID(hashKey);
    List<NodeHistory> latestNodeHistory;

    Condition rangeKeyCondition =
        new Condition()
            .withComparisonOperator(ComparisonOperator.BETWEEN.toString())
            .withAttributeValueList(
                new AttributeValue().withS(currentTime), new AttributeValue().withS(rangeTime));

    DynamoDBQueryExpression<NodeHistory> queryExpression =
        new DynamoDBQueryExpression<NodeHistory>()
            .withHashKeyValues(nhKey)
            .withRangeKeyCondition("Time", rangeKeyCondition);

    latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);

    return this.transform(latestNodeHistory);
  }
  /**
   * Returns list of all items that are in cart for that user.
   *
   * @param userId
   * @return
   */
  public List<CartItem> getCartItemsFromUserId(int userId) {
    // Declare mapper
    DynamoDBMapper mapper = this.conn.getMapper();

    // Create hash key values which to form a query
    CartItem hashKeyValues = new CartItem();
    hashKeyValues.setUserId(userId);

    // Form a query
    DynamoDBQueryExpression<CartItem> queryExpression =
        new DynamoDBQueryExpression<CartItem>()
            .withHashKeyValues(hashKeyValues)
            .withQueryFilterEntry(
                "IsOrdered",
                new Condition()
                    .withComparisonOperator(ComparisonOperator.EQ)
                    .withAttributeValueList(new AttributeValue().withN("0")));
    // add query filter

    queryExpression.setHashKeyValues(hashKeyValues);

    // execute that query
    List<CartItem> itemList = mapper.query(CartItem.class, queryExpression);

    for (int i = 0; i < itemList.size(); i++) {
      System.out.println(itemList.get(i).getUserId());
      System.out.println(itemList.get(i).getCartId());
      System.out.println(itemList.get(i).getProductId());
      System.out.println(itemList.get(i).getQuantity());
    }

    // return the first item (we will have only one item)
    return itemList;
  }
예제 #7
0
  @Test
  public void limitTest() {
    createGenericHashRangeTable(hashRangeTableName, "hashCode", "rangeCode");

    for (int i = 0; i < 10; i++) {
      TestClassWithHashRangeKey c = new TestClassWithHashRangeKey();
      c.setHashCode("code");
      c.setRangeCode(i + "");
      mapper.save(c);
    }

    String code = "code";
    TestClassWithHashRangeKey hashKeyTemplate = new TestClassWithHashRangeKey();
    hashKeyTemplate.setHashCode(code);

    Map<String, Condition> emptyRangeConditions = new HashMap<String, Condition>();

    DynamoDBQueryExpression<TestClassWithHashRangeKey> query1 =
        new DynamoDBQueryExpression<TestClassWithHashRangeKey>()
            .withHashKeyValues(hashKeyTemplate)
            .withRangeKeyConditions(emptyRangeConditions)
            .withLimit(1);
    DynamoDBQueryExpression<TestClassWithHashRangeKey> query3 =
        new DynamoDBQueryExpression<TestClassWithHashRangeKey>()
            .withHashKeyValues(hashKeyTemplate)
            .withRangeKeyConditions(emptyRangeConditions)
            .withLimit(3);
    DynamoDBQueryExpression<TestClassWithHashRangeKey> query20 =
        new DynamoDBQueryExpression<TestClassWithHashRangeKey>()
            .withHashKeyValues(hashKeyTemplate)
            .withRangeKeyConditions(emptyRangeConditions)
            .withLimit(20);

    Assert.assertEquals(1, mapper.query(TestClassWithHashRangeKey.class, query1).size());
    Assert.assertEquals(3, mapper.query(TestClassWithHashRangeKey.class, query3).size());
    Assert.assertEquals(10, mapper.query(TestClassWithHashRangeKey.class, query20).size());
  }
예제 #8
0
  private List<NodeHistoryEntity> findNodeHistories(String nid, String time) {
    String hashKey = nid;
    String rangeKey = time;
    NodeHistory nhKey = new NodeHistory();
    nhKey.setNodeID(hashKey);
    List<NodeHistory> latestNodeHistory;

    if (time != null) {
      Condition rangeKeyCondition =
          new Condition()
              .withComparisonOperator(ComparisonOperator.EQ.toString())
              .withAttributeValueList(new AttributeValue().withS(rangeKey));
      DynamoDBQueryExpression<NodeHistory> queryExpression =
          new DynamoDBQueryExpression<NodeHistory>()
              .withHashKeyValues(nhKey)
              .withRangeKeyCondition("Time", rangeKeyCondition);
      latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);
    } else {
      DynamoDBQueryExpression<NodeHistory> queryExpression =
          new DynamoDBQueryExpression<NodeHistory>().withHashKeyValues(nhKey);
      latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);
    }

    List<NodeHistoryEntity> list = new ArrayList<>();

    for (NodeHistory n : latestNodeHistory) {
      NodeHistoryEntity ret = new NodeHistoryEntity();

      ret.setNID(n.getNodeID());
      ret.setTime(n.getTime());
      ret.setSAValues(n.getSAValues());

      list.add(ret);
    }

    return list;
  }
예제 #9
0
  @Test
  public void queryWithUnknownHashKey() {
    putItemWithHashKey();

    String code = "hash1x";
    TestClassWithHashKey hashKeyTemplate = new TestClassWithHashKey();
    hashKeyTemplate.setCode(code);

    Map<String, Condition> emptyRangeConditions = new HashMap<String, Condition>();

    DynamoDBQueryExpression<TestClassWithHashKey> query =
        new DynamoDBQueryExpression<TestClassWithHashKey>()
            .withHashKeyValues(hashKeyTemplate)
            .withRangeKeyConditions(emptyRangeConditions);

    List<TestClassWithHashKey> valueList = mapper.query(TestClassWithHashKey.class, query);
    Assert.assertNotNull("Value list is null.", valueList);
    Assert.assertEquals("Value list should be empty.", 0, valueList.size());
  }
예제 #10
0
  @Test
  public void scanIndexForwardFalseTest() {
    createTable(
        hashRangeTableName,
        createStringAttributeDefinition("hashCode"),
        createStringAttributeDefinition("rangeCode"));

    {
      TestClassWithHashRangeKey c = new TestClassWithHashRangeKey();
      c.setHashCode("code");
      c.setRangeCode("1");
      c.setStringData("first");
      mapper.save(c);
    }

    {
      TestClassWithHashRangeKey c = new TestClassWithHashRangeKey();
      c.setHashCode("code");
      c.setRangeCode("2");
      c.setStringData("second");
      mapper.save(c);
    }

    String code = "code";
    TestClassWithHashRangeKey hashKeyTemplate = new TestClassWithHashRangeKey();
    hashKeyTemplate.setHashCode(code);

    Map<String, Condition> emptyRangeConditions = new HashMap<String, Condition>();

    DynamoDBQueryExpression<TestClassWithHashRangeKey> query =
        new DynamoDBQueryExpression<TestClassWithHashRangeKey>()
            .withHashKeyValues(hashKeyTemplate)
            .withRangeKeyConditions(emptyRangeConditions)
            .withScanIndexForward(false)
            .withLimit(1);

    TestClassWithHashRangeKey res = mapper.query(TestClassWithHashRangeKey.class, query).get(0);
    Assert.assertEquals("second", res.getStringData());
  }
예제 #11
0
  @Test
  public void queryWithUnknownHashRangeKey1() {
    putItemWithHashKeyAndRangeKey();

    String hashCode = "hash1x";

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

    Condition rangeKeyCondition =
        new Condition().withComparisonOperator(ComparisonOperator.NOT_NULL);

    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());
  }
예제 #12
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());
  }