Search in sources :

Example 81 with Item

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

the class TryDaxTests method scanTest.

void scanTest(String tableName, DynamoDB client, int iterations) {
    long startTime, endTime;
    System.out.println("Scan test - all items in the table");
    Table table = client.getTable(tableName);
    for (int i = 0; i < iterations; i++) {
        startTime = System.nanoTime();
        ItemCollection<ScanOutcome> items = table.scan();
        try {
            Iterator<Item> iter = items.iterator();
            while (iter.hasNext()) {
                iter.next();
            }
        } catch (Exception e) {
            System.err.println("Unable to scan table:");
            e.printStackTrace();
        }
        endTime = System.nanoTime();
        printTime(startTime, endTime, iterations);
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) ScanOutcome(com.amazonaws.services.dynamodbv2.document.ScanOutcome)

Example 82 with Item

use of com.amazonaws.services.dynamodbv2.document.Item 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 83 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project athenz by yahoo.

the class DynamoDBSSHRecordStoreConnection method insertSSHCertRecord.

@Override
public boolean insertSSHCertRecord(SSHCertRecord certRecord) {
    final String primaryKey = getPrimaryKey(certRecord.getInstanceId(), certRecord.getService());
    try {
        Item item = new Item().withPrimaryKey(KEY_PRIMARY, primaryKey).withString(KEY_INSTANCE_ID, certRecord.getInstanceId()).withString(KEY_SERVICE, certRecord.getService()).withString(KEY_CLIENT_IP, certRecord.getClientIP()).withString(KEY_PRINCIPALS, certRecord.getPrincipals()).withString(KEY_PRIVATE_IP, certRecord.getPrivateIP()).withLong(KEY_TTL, System.currentTimeMillis() / 1000L + expiryTime);
        table.putItem(item);
        return true;
    } catch (Exception ex) {
        LOGGER.error("DynamoDB Insert Error for {}: {}/{}", primaryKey, ex.getClass(), ex.getMessage());
        return false;
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item)

Example 84 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project athenz by yahoo.

the class DynamoDBCertRecordStoreConnection method updateLastNotified.

private List<X509CertRecord> updateLastNotified(String lastNotifiedServer, long lastNotifiedTime, List<Item> items) {
    long yesterday = lastNotifiedTime - TimeUnit.DAYS.toMillis(1);
    List<X509CertRecord> updatedRecords = new ArrayList<>();
    for (Item item : items) {
        try {
            Item updatedItem = dynamoDBNotificationsHelper.updateLastNotifiedItem(lastNotifiedServer, lastNotifiedTime, yesterday, item, KEY_PRIMARY, table);
            if (isRecordUpdatedWithNotificationTimeAndServer(lastNotifiedServer, lastNotifiedTime, updatedItem)) {
                X509CertRecord x509CertRecord = itemToX509CertRecord(updatedItem);
                updatedRecords.add(x509CertRecord);
            }
        } catch (ConditionalCheckFailedException ex) {
        // This error appears when the update didn't work because it was already updated by another server. We can ignore it.
        } catch (Exception ex) {
            LOGGER.error("DynamoDB updateLastNotified failed for item: {}, error: {}", item.toString(), ex.getMessage());
        }
    }
    return updatedRecords;
}
Also used : ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 85 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project athenz by yahoo.

the class DynamoDBCertRecordStoreConnection method mostUpdatedHostRecord.

private boolean mostUpdatedHostRecord(Item recordToCheck) {
    try {
        // Query all records with the same hostName / provider / service as recordToCheck
        QuerySpec spec = new QuerySpec().withKeyConditionExpression("hostName = :v_host_name").withFilterExpression("attribute_exists(provider) AND provider = :v_provider AND attribute_exists(service) AND service = :v_service").withValueMap(new ValueMap().withString(":v_host_name", recordToCheck.getString(KEY_HOSTNAME)).withString(":v_provider", recordToCheck.getString(KEY_PROVIDER)).withString(":v_service", recordToCheck.getString(KEY_SERVICE)));
        ItemCollection<QueryOutcome> outcome = itemCollectionRetryDynamoDBCommand.run(() -> hostNameIndex.query(spec));
        List<Item> allRecordsWithHost = new ArrayList<>();
        for (Item item : outcome) {
            allRecordsWithHost.add(item);
        }
        // Verify recordToCheck is the most updated record with this hostName
        return dynamoDBNotificationsHelper.isMostUpdatedRecordBasedOnAttribute(recordToCheck, allRecordsWithHost, KEY_CURRENT_TIME, KEY_PRIMARY);
    } catch (Exception ex) {
        LOGGER.error("DynamoDB mostUpdatedHostRecord failed for item: {}, error: {}", recordToCheck.toString(), ex.getMessage());
        return false;
    }
}
Also used : ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Aggregations

AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)107 Item (com.amazonaws.services.dynamodbv2.document.Item)95 HashMap (java.util.HashMap)60 Table (com.amazonaws.services.dynamodbv2.document.Table)53 Test (org.testng.annotations.Test)49 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)42 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)35 AmazonServiceException (com.amazonaws.AmazonServiceException)31 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)27 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)26 ArrayList (java.util.ArrayList)25 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)24 List (java.util.List)23 Test (org.junit.Test)23 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)22 IOException (java.io.IOException)21 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)20 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)18 Date (java.util.Date)18 Map (java.util.Map)17