com.amazonaws.services.dynamodbv2.model.AttributeValue Java Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.model.AttributeValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: beam   Author: apache   File: AttributeValueCoderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldPassForMapType() throws IOException {
  AttributeValue expected = new AttributeValue();

  Map<String, AttributeValue> attrMap = new HashMap<>();
  attrMap.put("innerMapAttr1", new AttributeValue("innerMapValue1"));
  attrMap.put(
      "innerMapAttr2",
      new AttributeValue().withB(ByteBuffer.wrap("8976234".getBytes(StandardCharsets.UTF_8))));

  expected.setM(attrMap);

  AttributeValueCoder coder = AttributeValueCoder.of();
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  coder.encode(expected, output);

  ByteArrayInputStream in = new ByteArrayInputStream(output.toByteArray());

  AttributeValue actual = coder.decode(in);

  Assert.assertEquals(expected, actual);
}
 
Example #2
Source Project: aws-dynamodb-encryption-java   Author: aws   File: BinaryAttributesITCase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLoad() throws Exception {
    DynamoDBMapper util = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo);

    for ( Map<String, AttributeValue> attr : attrs ) {
    	// test BinaryAttributeClass
        BinaryAttributeByteBufferTestClass x = util.load(BinaryAttributeByteBufferTestClass.class, attr.get(KEY_NAME).getS());
        assertEquals(x.getKey(), attr.get(KEY_NAME).getS());
        assertEquals(x.getBinaryAttribute(), ByteBuffer.wrap(generateByteArray(contentLength)));
        assertTrue(x.getBinarySetAttribute().contains(ByteBuffer.wrap(generateByteArray(contentLength))));
        assertTrue(x.getBinarySetAttribute().contains(ByteBuffer.wrap(generateByteArray(contentLength + 1))));

        // test BinaryAttributeByteArrayTestClass
        BinaryAttributeByteArrayTestClass y = util.load(BinaryAttributeByteArrayTestClass.class, attr.get(KEY_NAME).getS());
        assertEquals(y.getKey(), attr.get(KEY_NAME).getS());
        assertTrue(Arrays.equals(y.getBinaryAttribute(), (generateByteArray(contentLength))));
        assertTrue(2 == y.getBinarySetAttribute().size());
        assertTrue(setContainsBytes(y.getBinarySetAttribute(), generateByteArray(contentLength)));
        assertTrue(setContainsBytes(y.getBinarySetAttribute(), generateByteArray(contentLength+1)));
    }

}
 
Example #3
Source Project: aws-dynamodb-encryption-java   Author: aws   File: DelegatedEncryptionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void RsaSignedOnly() throws GeneralSecurityException {
    KeyPairGenerator rsaGen = KeyPairGenerator.getInstance("RSA");
    rsaGen.initialize(2048, Utils.getRng());
    KeyPair sigPair = rsaGen.generateKeyPair();
    encryptor = DynamoDBEncryptor.getInstance(
            new SymmetricStaticProvider(encryptionKey, sigPair,
                    Collections.<String, String>emptyMap()), "encryptor-");

    Map<String, AttributeValue> encryptedAttributes = encryptor.encryptAllFieldsExcept(attribs, context, attribs.keySet().toArray(new String[0]));
    assertThat(encryptedAttributes, AttrMatcher.invert(attribs));
    Map<String, AttributeValue> decryptedAttributes =
            encryptor.decryptAllFieldsExcept(encryptedAttributes, context, attribs.keySet().toArray(new String[0]));
    assertThat(decryptedAttributes, AttrMatcher.match(attribs));

    // Make sure keys and version are not encrypted
    assertAttrEquals(attribs.get("hashKey"), encryptedAttributes.get("hashKey"));
    assertAttrEquals(attribs.get("rangeKey"), encryptedAttributes.get("rangeKey"));
    assertAttrEquals(attribs.get("version"), encryptedAttributes.get("version"));

    // Make sure String has not been encrypted (we'll assume the others are correct as well)
    assertAttrEquals(attribs.get("stringValue"), encryptedAttributes.get("stringValue"));
}
 
Example #4
Source Project: geowave   Author: locationtech   File: DynamoDBOperations.java    License: Apache License 2.0 6 votes vote down vote up
private ScanResult getResults(
    final String tableName,
    final short adapterId,
    final List<GeoWaveRow> resultList,
    final Map<String, AttributeValue> lastEvaluatedKey) {
  final ScanRequest request = new ScanRequest(tableName);
  if ((lastEvaluatedKey != null) && !lastEvaluatedKey.isEmpty()) {
    request.setExclusiveStartKey(lastEvaluatedKey);
  }
  final ScanResult result = client.scan(request);
  result.getItems().forEach(objMap -> {
    final byte[] dataId = objMap.get(DynamoDBRow.GW_PARTITION_ID_KEY).getB().array();
    final AttributeValue valueAttr = objMap.get(DynamoDBRow.GW_VALUE_KEY);
    final byte[] value = valueAttr == null ? null : valueAttr.getB().array();
    final AttributeValue visAttr = objMap.get(DynamoDBRow.GW_VISIBILITY_KEY);
    final byte[] vis = visAttr == null ? new byte[0] : visAttr.getB().array();

    resultList.add(DataIndexUtils.deserializeDataIndexRow(dataId, adapterId, value, vis));
  });
  return result;
}
 
Example #5
Source Project: emr-dynamodb-connector   Author: awslabs   File: HiveDynamoDBTypeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleTypeList() {
  List<AttributeValue> avList = new ArrayList<>();
  avList.add(new AttributeValue(STRING_LIST.get(0)));
  avList.add(new AttributeValue().withN(STRING_LIST.get(0)));
  AttributeValue av = new AttributeValue().withL(avList);

  HiveDynamoDBType ddType = HiveDynamoDBTypeFactory.getTypeObjectFromHiveType(STRING_LIST_OBJECT_INSPECTOR);
  List<String> expectedStringList = Lists.newArrayList(STRING_LIST.get(0), null);
  Object actualList = ddType.getHiveData(av, STRING_LIST_OBJECT_INSPECTOR);
  assertEquals(expectedStringList, actualList);

  ddType = HiveDynamoDBTypeFactory.getTypeObjectFromHiveType(LONG_LIST_OBJECT_INSPECTOR);
  List<Long> expectedLongList = Lists.newArrayList(null, LONG_LIST.get(0));
  actualList = ddType.getHiveData(av, LONG_LIST_OBJECT_INSPECTOR);
  assertEquals(expectedLongList, actualList);
}
 
Example #6
Source Project: spring-data-dynamodb   Author: michaellavelle   File: AbstractDynamoDBQueryCriteria.java    License: Apache License 2.0 6 votes vote down vote up
protected Condition createSingleValueCondition(String propertyName, ComparisonOperator comparisonOperator, Object o,
		Class<?> propertyType, boolean alreadyMarshalledIfRequired) {

	Assert.notNull(o, "Creating conditions on null property values not supported: please specify a value for '"
			+ propertyName + "'");

	Object attributeValue = !alreadyMarshalledIfRequired ? getPropertyAttributeValue(propertyName, o) : o;

	boolean marshalled = !alreadyMarshalledIfRequired && attributeValue != o
			&& !entityInformation.isCompositeHashAndRangeKeyProperty(propertyName);

	Class<?> targetPropertyType = marshalled ? String.class : propertyType;
	List<AttributeValue> attributeValueList = new ArrayList<AttributeValue>();
	attributeValueList = addAttributeValue(attributeValueList, attributeValue, propertyName, targetPropertyType, true);
	return new Condition().withComparisonOperator(comparisonOperator).withAttributeValueList(attributeValueList);

}
 
Example #7
Source Project: dynamodb-transactions   Author: awslabs   File: TransactionsIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void getThenUpdateExistingItem() {
    Transaction t1 = manager.newTransaction();
    
    Map<String, AttributeValue> item0a = new HashMap<String, AttributeValue>(item0);
    item0a.put("wef", new AttributeValue("new attr"));
    
    Map<String, AttributeValueUpdate> updates1 = new HashMap<String, AttributeValueUpdate>();
    updates1.put("wef", new AttributeValueUpdate(new AttributeValue("new attr"), AttributeAction.PUT));
    
    Map<String, AttributeValue> getResult = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0)).getItem();
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false);
    assertEquals(item0, getResult);
    
    Map<String, AttributeValue> updateResult = t1.updateItem(new UpdateItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0)
            .withAttributeUpdates(updates1).withReturnValues(ReturnValue.ALL_NEW)).getAttributes();
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0a, t1.getId(), false, true);
    assertEquals(item0a, updateResult);
    
    t1.commit();
    
    assertItemNotLocked(INTEG_HASH_TABLE_NAME, key0, item0a, true);
}
 
Example #8
Source Project: aws-dynamodb-encryption-java   Author: aws   File: AttributeEncryptorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = DynamoDBMappingException.class)
public void badVersionNumber() {
    Parameters<BaseClass> params = FakeParameters.getInstance(BaseClass.class, attribs, null,
            TABLE_NAME, HASH_KEY, RANGE_KEY);
    Map<String, AttributeValue> encryptedAttributes = encryptor.transform(params);
    ByteBuffer materialDescription = encryptedAttributes.get(
            encryptor.getEncryptor().getMaterialDescriptionFieldName()).getB();
    byte[] rawArray = materialDescription.array();
    assertEquals(0, rawArray[0]); // This will need to be kept in sync with the current version.
    rawArray[0] = 100;
    encryptedAttributes.put(encryptor.getEncryptor().getMaterialDescriptionFieldName(),
            new AttributeValue().withB(ByteBuffer.wrap(rawArray)));
    params = FakeParameters.getInstance(BaseClass.class, encryptedAttributes, null, TABLE_NAME,
            HASH_KEY, RANGE_KEY);
    encryptor.untransform(params);
}
 
Example #9
Source Project: dynamodb-transactions   Author: awslabs   File: TransactionsIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void getItemCommittedInsert() {
    Transaction t1 = manager.newTransaction();
    
    Map<String, AttributeValue> key1 = newKey(INTEG_HASH_TABLE_NAME);
    Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(key1);
    item1.put("asdf", new AttributeValue("wef"));
    
    t1.putItem(new PutItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withItem(item1));
    
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true);
    
    Map<String, AttributeValue> item = manager.getItem(new GetItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withKey(key1), IsolationLevel.COMMITTED).getItem();
    assertNull(item);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true);
    
    t1.rollback();
}
 
Example #10
Source Project: dynamodb-transactions   Author: awslabs   File: TransactionsIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void getItemWithDelete() {
    Transaction t1 = manager.newTransaction();
    Map<String, AttributeValue> getResult1 = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0)).getItem();
    assertEquals(getResult1, item0);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false);
    
    t1.deleteItem(new DeleteItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0));
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false);
    
    Map<String, AttributeValue> getResult2 = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0)).getItem();
    assertNull(getResult2);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false);
    
    t1.commit();
}
 
Example #11
Source Project: aws-dynamodb-encryption-java   Author: aws   File: AttributeEncryptorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = DynamoDBMappingException.class)
public void testSignOnlyWithUnknownAttributeAnnotationBadSignature() {
    Map<String, AttributeValue> attributes = new HashMap<>(attribs);
    attributes.put("newAttribute", new AttributeValue().withS("foo"));
    Parameters<? extends SignOnlyWithUnknownAttributeAnnotation> params = FakeParameters.getInstance(
            SignOnlyWithUnknownAttributeAnnotationWithNewAttribute.class, attributes, null,
            TABLE_NAME, HASH_KEY, RANGE_KEY);
    Map<String, AttributeValue> encryptedAttributes = encryptor.transform(params);
    assertThat(encryptedAttributes, AttrMatcher.invert(attributes));
    assertAttrEquals(new AttributeValue().withS("foo"), encryptedAttributes.get("newAttribute"));
    params = FakeParameters.getInstance(
            SignOnlyWithUnknownAttributeAnnotation.class, encryptedAttributes, null,
            TABLE_NAME, HASH_KEY, RANGE_KEY);
    encryptedAttributes.get("newAttribute").setS("bar");
    encryptor.untransform(params);
}
 
Example #12
Source Project: aws-dynamodb-encryption-java   Author: aws   File: AttributeEncryptorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testEncryptWithFieldLevelDoNotTouchAnnotation() {
    Map<String, AttributeValue> attributes = new HashMap<>(attribs);
    attributes.put("value", new AttributeValue().withN("100"));
    Parameters<? extends DoNotTouchField> params = FakeParameters.getInstance(
        DoNotTouchField.class, attributes, null,
        TABLE_NAME, HASH_KEY, RANGE_KEY);
    Map<String, AttributeValue> encryptedAttributes = encryptor.transform(params);
    assertThat(encryptedAttributes, AttrMatcher.invert(attributes));
    assertAttrEquals(attributes.get("value"), encryptedAttributes.get("value"));
    params = FakeParameters.getInstance(
        DoNotTouchField.class, encryptedAttributes, null,
        TABLE_NAME, HASH_KEY, RANGE_KEY);
    Map<String, AttributeValue> decryptedAttributes = encryptor.untransform(params);
    assertThat(decryptedAttributes, AttrMatcher.match(attributes));
}
 
Example #13
Source Project: aws-dynamodb-encryption-java   Author: aws   File: AttributeEncryptorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void fullEncryption() {
    Parameters<BaseClass> params = FakeParameters.getInstance(BaseClass.class, attribs, null,
            TABLE_NAME, HASH_KEY, RANGE_KEY);
    Map<String, AttributeValue> encryptedAttributes = encryptor.transform(params);
    assertThat(encryptedAttributes, AttrMatcher.invert(attribs));
    params = FakeParameters.getInstance(BaseClass.class, encryptedAttributes, null, TABLE_NAME,
            HASH_KEY, RANGE_KEY);
    Map<String, AttributeValue> decryptedAttributes = encryptor.untransform(params);
    assertThat(decryptedAttributes, AttrMatcher.match(attribs));

    // Make sure keys and version are not encrypted
    assertAttrEquals(attribs.get(HASH_KEY), encryptedAttributes.get(HASH_KEY));
    assertAttrEquals(attribs.get(RANGE_KEY), encryptedAttributes.get(RANGE_KEY));
    assertAttrEquals(attribs.get("version"), encryptedAttributes.get("version"));

    // Make sure String has been encrypted (we'll assume the others are correct as well)
    assertTrue(encryptedAttributes.containsKey("stringValue"));
    assertNull(encryptedAttributes.get("stringValue").getS());
    assertNotNull(encryptedAttributes.get("stringValue").getB());
}
 
Example #14
Source Project: aws-dynamodb-encryption-java   Author: aws   File: NumericSetAttributesITCase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLoad() throws Exception {
    DynamoDBMapper util = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo);

    for ( Map<String, AttributeValue> attr : attrs ) {
        NumberSetAttributeTestClass x = util.load(NumberSetAttributeTestClass.class, attr.get(KEY_NAME).getS());
        assertEquals(x.getKey(), attr.get(KEY_NAME).getS());
        
        // Convert all numbers to the most inclusive type for easy comparison
        assertNumericSetsEquals(x.getBigDecimalAttribute(), attr.get(BIG_DECIMAL_ATTRIBUTE).getNS());
        assertNumericSetsEquals(x.getBigIntegerAttribute(), attr.get(BIG_INTEGER_ATTRIBUTE).getNS());
        assertNumericSetsEquals(x.getFloatObjectAttribute(), attr.get(FLOAT_OBJECT_ATTRIBUTE).getNS());
        assertNumericSetsEquals(x.getDoubleObjectAttribute(), attr.get(DOUBLE_OBJECT_ATTRIBUTE).getNS());
        assertNumericSetsEquals(x.getIntegerAttribute(), attr.get(INTEGER_ATTRIBUTE).getNS());
        assertNumericSetsEquals(x.getLongObjectAttribute(), attr.get(LONG_OBJECT_ATTRIBUTE).getNS());
        assertNumericSetsEquals(x.getByteObjectAttribute(), attr.get(BYTE_OBJECT_ATTRIBUTE).getNS());
        assertSetsEqual(toSet("0", "1"), attr.get(BOOLEAN_ATTRIBUTE).getNS());
    }
}
 
Example #15
Source Project: aws-dynamodb-encryption-java   Author: aws   File: DelegatedEncryptionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void signedOnlyNullCryptoKey() throws GeneralSecurityException {
    prov = new SymmetricStaticProvider(null, macKey, Collections.<String, String>emptyMap());
    encryptor = DynamoDBEncryptor.getInstance(prov, "encryptor-");
    Map<String, AttributeValue> encryptedAttributes =
            encryptor.encryptAllFieldsExcept(attribs, context, attribs.keySet().toArray(new String[0]));
    assertThat(encryptedAttributes, AttrMatcher.invert(attribs));
    Map<String, AttributeValue> decryptedAttributes = encryptor.decryptAllFieldsExcept(encryptedAttributes, context, attribs.keySet().toArray(new String[0]));
    assertThat(decryptedAttributes, AttrMatcher.match(attribs));

    // Make sure keys and version are not encrypted
    assertAttrEquals(attribs.get("hashKey"), encryptedAttributes.get("hashKey"));
    assertAttrEquals(attribs.get("rangeKey"), encryptedAttributes.get("rangeKey"));
    assertAttrEquals(attribs.get("version"), encryptedAttributes.get("version"));

    // Make sure String has not been encrypted (we'll assume the others are correct as well)
    assertAttrEquals(attribs.get("stringValue"), encryptedAttributes.get("stringValue"));
}
 
Example #16
@Test
public void signedOnlyNullCryptoKey() throws GeneralSecurityException {
    prov = new SymmetricStaticProvider(null, macKey, Collections.<String, String>emptyMap());
    encryptor = DynamoDBEncryptor.getInstance(prov, "encryptor-");
    Map<String, AttributeValue> encryptedAttributes =
            encryptor.encryptAllFieldsExcept(attribs, context, attribs.keySet().toArray(new String[0]));
    assertThat(encryptedAttributes, AttrMatcher.invert(attribs));
    Map<String, AttributeValue> decryptedAttributes = encryptor.decryptAllFieldsExcept(encryptedAttributes, context, attribs.keySet().toArray(new String[0]));
    assertThat(decryptedAttributes, AttrMatcher.match(attribs));

    // Make sure keys and version are not encrypted
    assertAttrEquals(attribs.get("hashKey"), encryptedAttributes.get("hashKey"));
    assertAttrEquals(attribs.get("rangeKey"), encryptedAttributes.get("rangeKey"));
    assertAttrEquals(attribs.get("version"), encryptedAttributes.get("version"));

    // Make sure String has not been encrypted (we'll assume the others are correct as well)
    assertAttrEquals(attribs.get("stringValue"), encryptedAttributes.get("stringValue"));
}
 
Example #17
Source Project: dynamodb-transactions   Author: awslabs   File: TransactionsIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void updateItemAllNewOverwrite() {
    Transaction t1 = manager.newTransaction();
    Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(item0);
    item1.put("asdf", new AttributeValue("wef"));
    Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>();
    updates.put("asdf", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("wef")));
    
    Map<String, AttributeValue> result1 = t1.updateItem(new UpdateItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withKey(key0)
        .withAttributeUpdates(updates)
        .withReturnValues(ReturnValue.ALL_NEW)).getAttributes();
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item1, t1.getId(), false, true);
    assertEquals(result1, item1);
    
    t1.commit();
    
    assertItemNotLocked(INTEG_HASH_TABLE_NAME, key0, item1, true);
}
 
Example #18
Source Project: aws-dynamodb-encryption-java   Author: aws   File: DynamoDBEncryptor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @see #marshallDescription(Map)
 */
protected static Map<String, String> unmarshallDescription(AttributeValue attributeValue) {
    attributeValue.getB().mark();
    try (DataInputStream in = new DataInputStream(
                new ByteBufferInputStream(attributeValue.getB())) ) {
        Map<String, String> result = new HashMap<String, String>();
        int version = in.readInt();
        if (version != CURRENT_VERSION) {
            throw new IllegalArgumentException("Unsupported description version");
        }

        String key, value;
        int keyLength, valueLength;
        try {
            while(in.available() > 0) {
                keyLength = in.readInt();
                byte[] bytes = new byte[keyLength];
                if (in.read(bytes) != keyLength) {
                    throw new IllegalArgumentException("Malformed description");
                }
                key = new String(bytes, UTF8);
                valueLength = in.readInt();
                bytes = new byte[valueLength];
                if (in.read(bytes) != valueLength) {
                    throw new IllegalArgumentException("Malformed description");
                }
                value = new String(bytes, UTF8);
                result.put(key, value);
            }
        } catch (EOFException eof) {
            throw new IllegalArgumentException("Malformed description", eof);
        }
        return result;
    } catch (IOException ex) {
        // Due to the objects in use, an IOException is not possible.
        throw new RuntimeException("Unexpected exception", ex);
    } finally {
        attributeValue.getB().reset();
    }
}
 
Example #19
Source Project: reinvent2013-mobile-photo-share   Author: aws-samples   File: DeviceAuthentication.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the specified UID from the identity table.
 * 
 * @param uid
 *            Unique device identifier
 */
public void deleteDevice(String uid) throws DataAccessException {
    HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
    key.put(ATTRIBUTE_UID, new AttributeValue().withS(uid));

    DeleteItemRequest deleteItemRequest = new DeleteItemRequest()
            .withTableName(DEVICE_TABLE)
            .withKey(key);

    try {
        ddb.deleteItem(deleteItemRequest);
    } catch (AmazonClientException e) {
        throw new DataAccessException("Failed to delete device: " + uid, e);
    }
}
 
Example #20
Source Project: emr-dynamodb-connector   Author: awslabs   File: DynamoDBObjectInspector.java    License: Apache License 2.0 5 votes vote down vote up
private Object getColumnData(StructField fieldRef, DynamoDBItemWritable rowData) {
  try {
    /* Get the hive data type for this column. */
    DynamoDBField ddFieldRef = (DynamoDBField) fieldRef;
    ObjectInspector fieldOI = ddFieldRef.getFieldObjectInspector();

    /* Get the Hive to DynamoDB type mapper for this column. */
    HiveDynamoDBType ddType = ddFieldRef.getDynamoDBType();

    /* See if column is of item type. */
    if (HiveDynamoDBTypeFactory.isHiveDynamoDBItemMapType(ddType)) {
      /*
       * User has mapped a DynamoDB item to a single hive column of
       * type map<string,string>.
       */
      HiveDynamoDBItemType ddItemType = (HiveDynamoDBItemType) ddType;
      return ddItemType.buildHiveData(rowData.getItem());
    } else {
      /* User has mapped individual attributes in DynamoDB to hive. */
      String attributeName = ddFieldRef.getAttributeName();
      if (rowData.getItem().containsKey(attributeName)) {
        AttributeValue fieldValue = rowData.getItem().get(attributeName);
        return fieldValue == null ? null : ddType.getHiveData(fieldValue, fieldOI);
      } else {
        return null;
      }
    }
  } catch (Exception e) {
    throw new RuntimeException("Exception while processing record: " + rowData.toString(), e);
  }
}
 
Example #21
Source Project: Cheddar   Author: travel-cloud   File: DynamoDBUnmarshallerUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static NUnmarshaller getBigIntegerNUnmarshaller() {
    return new NUnmarshaller() {

        @Override
        public Object unmarshall(final AttributeValue value) {
            return new BigInteger(value.getN());
        }
    };
}
 
Example #22
Source Project: dynamodb-transactions   Author: awslabs   File: TransactionDynamoDBFacade.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public UpdateItemResult updateItem(String tableName,
        Map<String, AttributeValue> key,
        Map<String, AttributeValueUpdate> attributeUpdates)
        throws AmazonServiceException, AmazonClientException {
    return updateItem(new UpdateItemRequest()
            .withTableName(tableName)
            .withKey(key)
            .withAttributeUpdates(attributeUpdates));
}
 
Example #23
Source Project: amazon-cognito-developer-authentication-sample   Author: aws-samples   File: DeviceAuthentication.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the specified UID from the identity table.
 * 
 * @param uid
 *            Unique device identifier
 */
public void deleteDevice(String uid) throws DataAccessException {
    HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
    key.put(ATTRIBUTE_UID, new AttributeValue().withS(uid));

    DeleteItemRequest deleteItemRequest = new DeleteItemRequest()
            .withTableName(DEVICE_TABLE)
            .withKey(key);

    try {
        ddb.deleteItem(deleteItemRequest);
    } catch (AmazonClientException e) {
        throw new DataAccessException("Failed to delete device: " + uid, e);
    }
}
 
Example #24
Source Project: dynamodb-transactions   Author: awslabs   File: Request.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Map<String, AttributeValue> getKey(TransactionManager txManager) {
    if(key == null) {
         key = getKeyFromItem(getTableName(), request.getItem(), txManager);
    }
    return key;
}
 
Example #25
Source Project: dynamodb-import-export-tool   Author: amazon-archives   File: BlockingQueueWorker.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Void call() {
    final ScanResult scanResult = result.getScanResult();
    final List<Map<String, AttributeValue>> items = scanResult.getItems();
    final Iterator<Map<String, AttributeValue>> it = items.iterator();
    boolean interrupted = false;
    try {
        do {
            try {
                Map<String, AttributeValue> item = it.next();
                DynamoDBEntryWithSize entryWithSize = new DynamoDBEntryWithSize(
                        item,
                        ItemSizeCalculator.calculateItemSizeInBytes(item));
                queue.put(entryWithSize);
            } catch (InterruptedException e) {
                interrupted = true;
                LOGGER.warn("interrupted when writing item to queue: "
                        + e.getMessage());
            }
        } while (it.hasNext());
    } finally {
        if (interrupted) {
            Thread.currentThread().interrupt();
        }
    }
    return null;
}
 
Example #26
Source Project: dynamodb-transactions   Author: awslabs   File: TransactionDynamoDBFacade.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean expectedValueMatches(AttributeValue expected, AttributeValue actual) {
    if (expected.getN() != null) {
        return actual.getN() != null && new BigDecimal(expected.getN()).compareTo(new BigDecimal(actual.getN())) == 0;
    } else if (expected.getS() != null || expected.getB() != null) {
        return expected.equals(actual);
    } else {
        throw new IllegalArgumentException("Expect condition using unsupported value type: " + expected);
    }
}
 
Example #27
Source Project: dynamodb-import-export-tool   Author: amazon-archives   File: ItemSizeCalculator.java    License: Apache License 2.0 5 votes vote down vote up
public static int calculateScanResultSizeInBytes(ScanResult result) {
    final Iterator<Map<String, AttributeValue>> it = result.getItems().iterator();
    int totalBytes = 0;
    while(it.hasNext()){
        totalBytes += calculateItemSizeInBytes(it.next());
    }
    return totalBytes;
}
 
Example #28
Source Project: dynamodb-transactions   Author: awslabs   File: TransactionItem.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Fetches this transaction item from the tx table.  Uses consistent read.
 * 
 * @return the latest copy of the transaction item, or null if it has been completed (and deleted) 
 */
private Map<String, AttributeValue> get() {
    GetItemRequest getRequest = new GetItemRequest()
        .withTableName(txManager.getTransactionTableName())
        .withKey(txKey)
        .withConsistentRead(true);
    return txManager.getClient().getItem(getRequest).getItem();
}
 
Example #29
Source Project: emr-dynamodb-connector   Author: awslabs   File: DynamoDBDataParser.java    License: Apache License 2.0 5 votes vote down vote up
public static Object getStructObject(Map<String, AttributeValue> data, ObjectInspector objectInspector) {
  StructObjectInspector structOI = (StructObjectInspector) objectInspector;
  List<? extends StructField> structFields = structOI.getAllStructFieldRefs();

  List<Object> values = new ArrayList<>();
  for (StructField field : structFields) {
    values.add(getStructFieldObject(data, field));
  }

  return values;
}
 
Example #30
Source Project: Serverless-Programming-Cookbook   Author: PacktPublishing   File: DynamoDBServiceImpl2.java    License: MIT License 5 votes vote down vote up
@Override
public final Response scan(final Request request) {

    final String projectionExpression = request.getPartitionKey() + ", " + request.getSortKey();
    ScanRequest scanRequest = new ScanRequest()
            .withTableName(request.getTableName())
            .withProjectionExpression(projectionExpression);

    StringBuilder filterExpressionBuilder;
    Map<String, AttributeValue> expressionAttributeValues;
    if (request.getFilterData() != null) {
        filterExpressionBuilder = new StringBuilder();
        expressionAttributeValues = new HashMap<>();
        processFilterData(request, filterExpressionBuilder, expressionAttributeValues);
        // Add to ScanRequest.
        scanRequest.withFilterExpression(filterExpressionBuilder.toString());
        scanRequest.withExpressionAttributeValues(expressionAttributeValues);
    }



    final ScanResult scanResult = dynamoDBClient.scan(scanRequest);

    final StringBuilder response = new StringBuilder();
    response.append("PK of items read with scan (V2): ");
    for (Map<String, AttributeValue> item : scanResult.getItems()) {
        response.append(prepareKeyStr(item, request));
    }

    return new Response(response.toString(), null);
}