Search in sources :

Example 1 with PrimaryKey

use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project YCSB by brianfrankcooper.

the class DynamoDBClient method init.

@Override
public void init() throws DBException {
    String debug = getProperties().getProperty("dynamodb.debug", null);
    if (null != debug && "true".equalsIgnoreCase(debug)) {
        LOGGER.setLevel(Level.DEBUG);
    }
    String configuredEndpoint = getProperties().getProperty("dynamodb.endpoint", null);
    String credentialsFile = getProperties().getProperty("dynamodb.awsCredentialsFile", null);
    String primaryKey = getProperties().getProperty("dynamodb.primaryKey", null);
    String primaryKeyTypeString = getProperties().getProperty("dynamodb.primaryKeyType", null);
    String consistentReads = getProperties().getProperty("dynamodb.consistentReads", null);
    String connectMax = getProperties().getProperty("dynamodb.connectMax", null);
    if (null != connectMax) {
        this.maxConnects = Integer.parseInt(connectMax);
    }
    if (null != consistentReads && "true".equalsIgnoreCase(consistentReads)) {
        this.consistentRead = true;
    }
    if (null != configuredEndpoint) {
        this.endpoint = configuredEndpoint;
    }
    if (null == primaryKey || primaryKey.length() < 1) {
        throw new DBException("Missing primary key attribute name, cannot continue");
    }
    if (null != primaryKeyTypeString) {
        try {
            this.primaryKeyType = PrimaryKeyType.valueOf(primaryKeyTypeString.trim().toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new DBException("Invalid primary key mode specified: " + primaryKeyTypeString + ". Expecting HASH or HASH_AND_RANGE.");
        }
    }
    if (this.primaryKeyType == PrimaryKeyType.HASH_AND_RANGE) {
        // When the primary key type is HASH_AND_RANGE, keys used by YCSB
        // are range keys so we can benchmark performance of individual hash
        // partitions. In this case, the user must specify the hash key's name
        // and optionally can designate a value for the hash key.
        String configuredHashKeyName = getProperties().getProperty("dynamodb.hashKeyName", null);
        if (null == configuredHashKeyName || configuredHashKeyName.isEmpty()) {
            throw new DBException("Must specify a non-empty hash key name when the primary key type is HASH_AND_RANGE.");
        }
        this.hashKeyName = configuredHashKeyName;
        this.hashKeyValue = getProperties().getProperty("dynamodb.hashKeyValue", DEFAULT_HASH_KEY_VALUE);
    }
    try {
        AWSCredentials credentials = new PropertiesCredentials(new File(credentialsFile));
        ClientConfiguration cconfig = new ClientConfiguration();
        cconfig.setMaxConnections(maxConnects);
        dynamoDB = new AmazonDynamoDBClient(credentials, cconfig);
        dynamoDB.setEndpoint(this.endpoint);
        primaryKeyName = primaryKey;
        LOGGER.info("dynamodb connection created with " + this.endpoint);
    } catch (Exception e1) {
        LOGGER.error("DynamoDBClient.init(): Could not initialize DynamoDB client.", e1);
    }
}
Also used : PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) AmazonDynamoDBClient(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient) AWSCredentials(com.amazonaws.auth.AWSCredentials) File(java.io.File) ClientConfiguration(com.amazonaws.ClientConfiguration) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException)

Example 2 with PrimaryKey

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

the class CreateTablesLoadData method loadSampleThreads.

private static void loadSampleThreads(String tableName) {
    try {
        // 7
        long time1 = (new Date()).getTime() - (7 * 24 * 60 * 60 * 1000);
        // days
        // ago
        // 14
        long time2 = (new Date()).getTime() - (14 * 24 * 60 * 60 * 1000);
        // days
        // ago
        // 21
        long time3 = (new Date()).getTime() - (21 * 24 * 60 * 60 * 1000);
        // days
        // ago
        Date date1 = new Date();
        date1.setTime(time1);
        Date date2 = new Date();
        date2.setTime(time2);
        Date date3 = new Date();
        date3.setTime(time3);
        dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        Table table = dynamoDB.getTable(tableName);
        System.out.println("Adding data to " + tableName);
        Item item = new Item().withPrimaryKey("ForumName", "Amazon DynamoDB").withString("Subject", "DynamoDB Thread 1").withString("Message", "DynamoDB thread 1 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date2)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("index", "primarykey", "table")));
        table.putItem(item);
        item = new Item().withPrimaryKey("ForumName", "Amazon DynamoDB").withString("Subject", "DynamoDB Thread 2").withString("Message", "DynamoDB thread 2 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date3)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("index", "partitionkey", "sortkey")));
        table.putItem(item);
        item = new Item().withPrimaryKey("ForumName", "Amazon S3").withString("Subject", "S3 Thread 1").withString("Message", "S3 Thread 3 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date1)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("largeobjects", "multipart upload")));
        table.putItem(item);
    } catch (Exception e) {
        System.err.println("Failed to create item in " + tableName);
        System.err.println(e.getMessage());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) Date(java.util.Date) HashSet(java.util.HashSet)

Example 3 with PrimaryKey

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

the class MoviesItemOps06 method main.

public static void main(String[] args) throws Exception {
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")).build();
    DynamoDB dynamoDB = new DynamoDB(client);
    Table table = dynamoDB.getTable("Movies");
    int year = 2015;
    String title = "The Big New Movie";
    DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey(new PrimaryKey("year", year, "title", title)).withConditionExpression("info.rating <= :val").withValueMap(new ValueMap().withNumber(":val", 5.0));
    try {
        System.out.println("Attempting a conditional delete...");
        table.deleteItem(deleteItemSpec);
        System.out.println("DeleteItem succeeded");
    } catch (Exception e) {
        System.err.println("Unable to delete item: " + year + " " + title);
        System.err.println(e.getMessage());
    }
}
Also used : DeleteItemSpec(com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec) Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) PrimaryKey(com.amazonaws.services.dynamodbv2.document.PrimaryKey) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 4 with PrimaryKey

use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project YCSB by brianfrankcooper.

the class DynamoDBClient method init.

@Override
public void init() throws DBException {
    String debug = getProperties().getProperty("dynamodb.debug", null);
    if (null != debug && "true".equalsIgnoreCase(debug)) {
        LOGGER.setLevel(Level.DEBUG);
    }
    String configuredEndpoint = getProperties().getProperty("dynamodb.endpoint", null);
    String credentialsFile = getProperties().getProperty("dynamodb.awsCredentialsFile", null);
    String primaryKey = getProperties().getProperty("dynamodb.primaryKey", null);
    String primaryKeyTypeString = getProperties().getProperty("dynamodb.primaryKeyType", null);
    String consistentReads = getProperties().getProperty("dynamodb.consistentReads", null);
    String connectMax = getProperties().getProperty("dynamodb.connectMax", null);
    String configuredRegion = getProperties().getProperty("dynamodb.region", null);
    if (null != connectMax) {
        this.maxConnects = Integer.parseInt(connectMax);
    }
    if (null != consistentReads && "true".equalsIgnoreCase(consistentReads)) {
        this.consistentRead = true;
    }
    if (null != configuredEndpoint) {
        this.endpoint = configuredEndpoint;
    }
    if (null == primaryKey || primaryKey.length() < 1) {
        throw new DBException("Missing primary key attribute name, cannot continue");
    }
    if (null != primaryKeyTypeString) {
        try {
            this.primaryKeyType = PrimaryKeyType.valueOf(primaryKeyTypeString.trim().toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new DBException("Invalid primary key mode specified: " + primaryKeyTypeString + ". Expecting HASH or HASH_AND_RANGE.");
        }
    }
    if (this.primaryKeyType == PrimaryKeyType.HASH_AND_RANGE) {
        // When the primary key type is HASH_AND_RANGE, keys used by YCSB
        // are range keys so we can benchmark performance of individual hash
        // partitions. In this case, the user must specify the hash key's name
        // and optionally can designate a value for the hash key.
        String configuredHashKeyName = getProperties().getProperty("dynamodb.hashKeyName", null);
        if (null == configuredHashKeyName || configuredHashKeyName.isEmpty()) {
            throw new DBException("Must specify a non-empty hash key name when the primary key type is HASH_AND_RANGE.");
        }
        this.hashKeyName = configuredHashKeyName;
        this.hashKeyValue = getProperties().getProperty("dynamodb.hashKeyValue", DEFAULT_HASH_KEY_VALUE);
    }
    if (null != configuredRegion && configuredRegion.length() > 0) {
        region = configuredRegion;
    }
    try {
        AmazonDynamoDBClientBuilder dynamoDBBuilder = AmazonDynamoDBClientBuilder.standard();
        dynamoDBBuilder = null == endpoint ? dynamoDBBuilder.withRegion(this.region) : dynamoDBBuilder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(this.endpoint, this.region));
        dynamoDB = dynamoDBBuilder.withClientConfiguration(new ClientConfiguration().withTcpKeepAlive(true).withMaxConnections(this.maxConnects)).withCredentials(new AWSStaticCredentialsProvider(new PropertiesCredentials(new File(credentialsFile)))).build();
        primaryKeyName = primaryKey;
        LOGGER.info("dynamodb connection created with " + this.endpoint);
    } catch (Exception e1) {
        LOGGER.error("DynamoDBClient.init(): Could not initialize DynamoDB client.", e1);
    }
}
Also used : AmazonDynamoDBClientBuilder(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) File(java.io.File) ClientConfiguration(com.amazonaws.ClientConfiguration) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException)

Example 5 with PrimaryKey

use of com.amazonaws.services.dynamodbv2.document.PrimaryKey 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)

Aggregations

Test (org.testng.annotations.Test)15 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)11 Item (com.amazonaws.services.dynamodbv2.document.Item)9 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)8 Table (com.amazonaws.services.dynamodbv2.document.Table)7 DeleteItemSpec (com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec)5 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)5 UpdateItemOutcome (com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 SSHCertRecord (com.yahoo.athenz.common.server.ssh.SSHCertRecord)3 Date (java.util.Date)3 AmazonClientException (com.amazonaws.AmazonClientException)2 ClientConfiguration (com.amazonaws.ClientConfiguration)2 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)2 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)2 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)2 PrimaryKey (com.amazonaws.services.dynamodbv2.document.PrimaryKey)2 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)2 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)2 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)2