Example usage for com.amazonaws.services.dynamodbv2 AmazonDynamoDBClient AmazonDynamoDBClient

List of usage examples for com.amazonaws.services.dynamodbv2 AmazonDynamoDBClient AmazonDynamoDBClient

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2 AmazonDynamoDBClient AmazonDynamoDBClient.

Prototype

AmazonDynamoDBClient(AwsSyncClientParams clientParams) 

Source Link

Document

Constructs a new client to invoke service methods on DynamoDB using the specified parameters.

Usage

From source file:AmazonDynamoDBSample.java

License:Open Source License

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 *
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.ProfilesConfigFile
 * @see com.amazonaws.ClientConfiguration
 *///from   www .jav  a  2 s.com
private static void init() throws Exception {
    /*
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (/home/madhurima/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider("default").getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (/home/madhurima/.aws/credentials), and is in valid format.", e);
    }
    dynamoDB = new AmazonDynamoDBClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    dynamoDB.setRegion(usWest2);
}

From source file:AddUserInfo.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());

    DynamoDB dynamoDB = new DynamoDB(client);

    Table table = dynamoDB.getTable("UserInfo");

    int UserID = 0;
    String FirstName = "Patrick";
    String LastName = "Laflin";
    String PhoneNumber = "(850)276-3816";
    String StreetAddressLine1 = "118 E Lakeshore Drive";
    String StreetAddressLine2 = "Unit B";
    String City = "Panama City Beach";
    String State = "FL";
    int ZipCode = 32413;
    String EmailAddress = "pbl14@my.fsu.edu";

    try {/*from  w  w w .j a v a  2 s.c  om*/
        System.out.println("Adding a new item...");
        PutItemOutcome outcome = table.putItem(new Item().withPrimaryKey("UserID", UserID)
                .withString("First Name", FirstName).withString("Last Name", LastName)
                .withString("Phone Number", PhoneNumber).withString("Street Address Line 1", StreetAddressLine1)
                .withString("Street Address Line 2", StreetAddressLine2).withString("City", City)
                .withString("State", State).withNumber("Zip Code", ZipCode)
                .withString("Email Address", EmailAddress));

        System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult());

    } catch (Exception e) {
        System.err.println("Unable to add item: " + UserID);
        System.err.println(e.getMessage());
    }

}

From source file:RandomQuery1OnDynamoDB.java

License:Open Source License

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 * /*from   w  ww.j  a va2 s.  com*/
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.ProfilesConfigFile
 * @see com.amazonaws.ClientConfiguration
 */
private static void init() throws Exception {
    /*
     * The ProfileCredentialsProvider will return your [default] credential
     * profile by reading from the credentials file located at
     * (C:\\Users\\Divendar\\.aws\\credentials).
     */

    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider("default").getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (C:\\Users\\Divendar\\.aws\\credentials), and is in valid format.", e);
    }
    dynamoDB = new AmazonDynamoDBClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    dynamoDB.setRegion(usWest2);
}

From source file:AddUserRoutes.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());

    DynamoDB dynamoDB = new DynamoDB(client);

    Table table = dynamoDB.getTable("UserRoutes");

    int UserID = 0;
    String Route1 = "NULL";
    String Route2 = "NULL";
    String Route3 = "NULL";
    String Route4 = "NULL";
    String Route5 = "NULL";

    try {/*from ww w.ja  v  a 2s .  c o m*/
        System.out.println("Adding a new item...");
        PutItemOutcome outcome = table.putItem(new Item().withPrimaryKey("UserID", UserID)
                .withString("Route 1", Route1).withString("Route 2", Route2).withString("Route 3", Route3)
                .withString("Route 4", Route4).withString("Route 5", Route5));

        System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult());

    } catch (Exception e) {
        System.err.println("Unable to add item: " + UserID);
        System.err.println(e.getMessage());
    }

}

From source file:CreateUserInfoTable.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());

    DynamoDB dynamoDB = new DynamoDB(client);

    String tableName = "UserInfo";

    try {//from   w w  w . j  a v  a2 s  .  c o  m
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName,
                Arrays.asList(new KeySchemaElement("UserID", KeyType.HASH)), //Partition key
                Arrays.asList(new AttributeDefinition("UserID", ScalarAttributeType.N)),
                new ProvisionedThroughput(10L, 10L));
        table.waitForActive();
        System.out.println("Success.  Table status: " + table.getDescription().getTableStatus());

    } catch (Exception e) {
        System.err.println("Unable to create table: ");
        System.err.println(e.getMessage());
    }
}

From source file:sqsAlertPersist.java

License:Open Source License

public static void main(String[] args) throws Exception {

    // get credentials
    String user = "jreilly";
    AWSCredentials credentials = whgHelper.getCred(user);

    // use credentials to set access to SQS
    AmazonSQS sqs = whgHelper.setQueueAccess(credentials);

    // define queue that messages will be retrieved from
    String thisQueue = "alertPersist";
    String nextQueue = "alertCache";

    // set access to database with credentials
    dynamoDB = new AmazonDynamoDBClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    dynamoDB.setRegion(usEast1);// ww w  . j  a  v a2s  .  c  o m

    // check for table, create one if missing
    String tableName = "alerts";
    whgHelper.setTable(dynamoDB, tableName);

    while (1 > 0) {

        // pull list of current messages (up to 10) in the queue
        List<Message> messages = whgHelper.getMessagesFromQueue(thisQueue, sqs);
        System.out.println("Count of messages in " + thisQueue + ": " + messages.size());

        try {

            for (Message message : messages) {

                whgHelper.printMessage(message);
                for (Entry<String, String> entry : message.getAttributes().entrySet()) {
                    whgHelper.printMessageEntry(entry);
                }

                // Add an item to DynamoDB table
                Map<String, AttributeValue> item = whgHelper.newAlert(message.getBody());
                PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
                PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
                System.out.println();
                System.out.println("Result: " + putItemResult);

                // then send message to cache queue
                System.out.println("Sending messages to next queue.");
                sqs.sendMessage(new SendMessageRequest(nextQueue, message.getBody()));

                // delete message after sending to persist queue
                System.out.println("Deleting message from this queue.\n");
                String messageRecieptHandle = message.getReceiptHandle();
                sqs.deleteMessage(new DeleteMessageRequest(thisQueue, messageRecieptHandle));

            }
            Thread.sleep(20000); // do nothing for 2000 miliseconds (2 second)

        } catch (AmazonServiceException ase) {
            whgHelper.errorMessagesAse(ase);
        } catch (AmazonClientException ace) {
            whgHelper.errorMessagesAce(ace);
        }
    }
}

From source file:AddUserFavorites.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());

    DynamoDB dynamoDB = new DynamoDB(client);

    Table table = dynamoDB.getTable("UserFavorites");

    int UserID = 0;
    String Location1 = "NULL";
    String Location2 = "NULL";
    String Location3 = "NULL";
    String Location4 = "NULL";
    String Location5 = "NULL";

    try {/*from  w  w  w .j a v  a2  s. c  o  m*/
        System.out.println("Adding a new item...");
        PutItemOutcome outcome = table
                .putItem(new Item().withPrimaryKey("UserID", UserID).withString("Location 1", Location1)
                        .withString("Location 2", Location2).withString("Location 3", Location3)
                        .withString("Location 4", Location4).withString("Location 5", Location5));

        System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult());

    } catch (Exception e) {
        System.err.println("Unable to add item: " + UserID);
        System.err.println(e.getMessage());
    }

}

From source file:CreateUserRoutesTable.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());

    DynamoDB dynamoDB = new DynamoDB(client);

    String tableName = "UserRoutes";

    try {// w w  w.ja  v a2s. c om
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName,
                Arrays.asList(new KeySchemaElement("UserID", KeyType.HASH)), //Partition key
                Arrays.asList(new AttributeDefinition("UserID", ScalarAttributeType.N)),
                new ProvisionedThroughput(10L, 10L));
        table.waitForActive();
        System.out.println("Success.  Table status: " + table.getDescription().getTableStatus());

    } catch (Exception e) {
        System.err.println("Unable to create table: ");
        System.err.println(e.getMessage());
    }
}

From source file:awskinesis.AmazonKinesisApplicationSample.java

License:Apache License

public static void deleteResources() {
    AWSCredentials credentials = credentialsProvider.getCredentials();

    // Delete the stream
    AmazonKinesis kinesis = new AmazonKinesisClient(credentials);
    kinesis.setEndpoint("kinesis.cn-north-1.amazonaws.com.cn");
    System.out.printf("Deleting the Amazon Kinesis stream used by the sample. Stream Name = %s.\n",
            SAMPLE_APPLICATION_STREAM_NAME);
    try {// ww w .j a v a 2s.co  m
        kinesis.deleteStream(SAMPLE_APPLICATION_STREAM_NAME);
    } catch (ResourceNotFoundException ex) {
        // The stream doesn't exist.
    }

    // Delete the table
    AmazonDynamoDBClient dynamoDB = new AmazonDynamoDBClient(credentialsProvider.getCredentials());
    dynamoDB.setEndpoint("dynamodb.cn-north-1.amazonaws.com.cn");
    System.out.printf(
            "Deleting the Amazon DynamoDB table used by the Amazon Kinesis Client Library. Table Name = %s.\n",
            SAMPLE_APPLICATION_NAME);
    try {
        dynamoDB.deleteTable(SAMPLE_APPLICATION_NAME);
    } catch (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException ex) {
        // The table doesn't exist.
    }
}

From source file:awslabs.lab22.Lab22.java

License:Open Source License

/**
 * Controls the flow of the lab code execution.
 */// www.j  av  a  2  s .co  m
public static void main(String[] args) {
    try {
        // Create DynamoDB client and set the region to US East (Virginia)
        AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(
                new ClasspathPropertiesFileCredentialsProvider());
        ddbClient.setRegion(region);

        List<Account> accounts = new ArrayList<Account>();
        accounts.add(new Account().withCompany("Amazon.com").withEmail("johndoe@amazon.com").withFirst("John")
                .withLast("Doe").withAge("33"));
        accounts.add(new Account().withCompany("Asperatus Tech").withEmail("janedoe@amazon.com")
                .withFirst("Jane").withLast("Doe").withAge("24"));
        accounts.add(new Account().withCompany("Amazon.com").withEmail("jimjohnson@amazon.com").withFirst("Jim")
                .withLast("Johnson"));

        // Verify that the table schema is as we expect, and correct any
        // problems we find.
        if (!confirmTableSchema(ddbClient, tableName)) {
            System.out.print("Deleting. ");
            optionalLabCode.deleteTable(ddbClient, tableName);
            System.out.print("Rebuilding. ");
            optionalLabCode.buildTable(ddbClient, tableName);
            System.out.println("Done.");
        }

        System.out.println("Adding items to table.");
        // Create the accounts
        for (Account account : accounts) {
            labCode.createAccountItem(ddbClient, tableName, account);
            System.out.println("Added item: " + account.getCompany() + "/" + account.getEmail());
        }

        System.out.println("Requesting matches for Company == Amazon.com");
        QueryResult queryResult = labCode.lookupByHashKey(ddbClient, tableName, "Amazon.com");
        if (queryResult != null && queryResult.getCount() > 0) {
            // Record was found
            for (Map<String, AttributeValue> item : queryResult.getItems()) {
                System.out.println("Item Found-");

                for (Entry<String, AttributeValue> attribute : item.entrySet()) {
                    System.out.print("    " + attribute.getKey() + ":");
                    if (attribute.getKey().equals("Age")) {
                        System.out.println(attribute.getValue().getN());
                    } else {
                        System.out.println(attribute.getValue().getS());
                    }
                }
            }
        } else {
            System.out.println("No matches found.");
        }

        // Conditionally update a record
        System.out.print("Attempting update. ");
        labCode.updateIfMatch(ddbClient, tableName, "jimjohnson@amazon.com", "Amazon.com", "James", "Jim");
        System.out.println("Done.");
    } catch (Exception ex) {
        LabUtility.dumpError(ex);
    }
}