Search in sources :

Example 6 with AmazonDynamoDBException

use of com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException in project athenz by yahoo.

the class DynamoDBSSHRecordStoreConnectionTest method testInsertSSHRecordException.

@Test
public void testInsertSSHRecordException() {
    SSHCertRecord certRecord = new SSHCertRecord();
    Mockito.doThrow(new AmazonDynamoDBException("invalid operation")).when(table).putItem(ArgumentMatchers.any(Item.class));
    DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
    boolean requestSuccess = dbConn.insertSSHCertRecord(certRecord);
    assertFalse(requestSuccess);
    dbConn.close();
}
Also used : AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) SSHCertRecord(com.yahoo.athenz.common.server.ssh.SSHCertRecord) Test(org.testng.annotations.Test)

Example 7 with AmazonDynamoDBException

use of com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException in project athenz by yahoo.

the class DynamoDBSSHRecordStoreConnectionTest method testGetSSHCertRecordNotFoundException.

@Test
public void testGetSSHCertRecordNotFoundException() {
    Mockito.doThrow(new AmazonDynamoDBException("item not found")).when(table).getItem("primaryKey", "cn:1234");
    DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
    SSHCertRecord certRecord = dbConn.getSSHCertRecord("1234", "cn");
    assertNull(certRecord);
    dbConn.close();
}
Also used : AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) SSHCertRecord(com.yahoo.athenz.common.server.ssh.SSHCertRecord) Test(org.testng.annotations.Test)

Example 8 with AmazonDynamoDBException

use of com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException in project aws-doc-sdk-examples by awsdocs.

the class UseDynamoMapping method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "To run this example, supply the following values: \n" + "artist name \n" + "song title \n" + "album title \n" + "number of awards \n";
    if (args.length < 4) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String artist = args[0];
    String songTitle = args[1];
    String albumTitle = args[2];
    String awards = args[3];
    // snippet-start:[dynamodb.java.dynamoDB_mapping.main]
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
    MusicItems items = new MusicItems();
    try {
        // Add new content to the Music table
        items.setArtist(artist);
        items.setSongTitle(songTitle);
        items.setAlbumTitle(albumTitle);
        // convert to an int
        items.setAwards(Integer.parseInt(awards));
        // Save the item
        DynamoDBMapper mapper = new DynamoDBMapper(client);
        mapper.save(items);
        // Load an item based on the Partition Key and Sort Key
        // Both values need to be passed to the mapper.load method
        String artistName = artist;
        String songQueryTitle = songTitle;
        // Retrieve the item
        MusicItems itemRetrieved = mapper.load(MusicItems.class, artistName, songQueryTitle);
        System.out.println("Item retrieved:");
        System.out.println(itemRetrieved);
        // Modify the Award value
        itemRetrieved.setAwards(2);
        mapper.save(itemRetrieved);
        System.out.println("Item updated:");
        System.out.println(itemRetrieved);
        System.out.print("Done");
    } catch (AmazonDynamoDBException e) {
        e.getStackTrace();
    }
}
Also used : AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 9 with AmazonDynamoDBException

use of com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBScanItems method main.

public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Please specify a table name");
        System.exit(1);
    }
    // snippet-start:[dynamodb.java.dynamoDB_scan.main]
    String tableName = args[0];
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
    try {
        ScanRequest scanRequest = new ScanRequest().withTableName(tableName);
        ScanResult result = client.scan(scanRequest);
        for (Map<String, AttributeValue> item : result.getItems()) {
            Set<String> keys = item.keySet();
            for (String key : keys) {
                System.out.println("The key name is " + key + "\n");
                System.out.println("The value is " + item.get(key).getS());
            }
        }
    } catch (AmazonDynamoDBException e) {
        e.getStackTrace();
    }
// snippet-end:[dynamodb.java.dynamoDB_scan.main]
}
Also used : ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 10 with AmazonDynamoDBException

use of com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException in project aws-doc-sdk-examples by awsdocs.

the class Query method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    Query <table> <partitionkey> <partitionkeyvalue>\n\n" + "Where:\n" + "    table - the table to put the item in.\n" + "    partitionkey  - partition key name of the table.\n" + "    partitionkeyvalue - value of the partition key that should match.\n\n" + "Example:\n" + "    Query GreetingsTable Language eng \n";
    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    String partition_key_name = args[1];
    String partition_key_val = args[2];
    String partition_alias = "#a";
    System.out.format("Querying %s", table_name);
    System.out.println("");
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    // set up an alias for the partition key name in case it's a reserved word
    HashMap<String, String> attrNameAlias = new HashMap<String, String>();
    attrNameAlias.put(partition_alias, partition_key_name);
    // set up mapping of the partition name with the value
    HashMap<String, AttributeValue> attrValues = new HashMap<String, AttributeValue>();
    attrValues.put(":" + partition_key_name, new AttributeValue().withS(partition_key_val));
    QueryRequest queryReq = new QueryRequest().withTableName(table_name).withKeyConditionExpression(partition_alias + " = :" + partition_key_name).withExpressionAttributeNames(attrNameAlias).withExpressionAttributeValues(attrValues);
    try {
        QueryResult response = ddb.query(queryReq);
        System.out.println(response.getCount());
    } catch (AmazonDynamoDBException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) HashMap(java.util.HashMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Aggregations

AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)16 Test (org.testng.annotations.Test)13 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)4 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)4 WorkloadRecord (com.yahoo.athenz.common.server.workload.WorkloadRecord)4 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)3 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)3 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)3 SSHCertRecord (com.yahoo.athenz.common.server.ssh.SSHCertRecord)3 DeleteItemSpec (com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec)2 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)1 QueryRequest (com.amazonaws.services.dynamodbv2.model.QueryRequest)1 QueryResult (com.amazonaws.services.dynamodbv2.model.QueryResult)1 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)1 ScanResult (com.amazonaws.services.dynamodbv2.model.ScanResult)1 HashMap (java.util.HashMap)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1