试用AWS的NoSQL服务DynamoDB

试用AWS的NoSQL服务DynamoDB

摘要

试用AWS的NoSQL服务DynamoDB

首先准备一个Bean类

package net.javablog.dynamodb;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

import java.util.Set;

@DynamoDBTable(tableName = "ProductCatalog")
public class CatalogItem {
    private Integer id;
    private String title;
    private String ISBN;
    private Set bookAuthors;

    @DynamoDBHashKey(attributeName = "Id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @DynamoDBAttribute(attributeName = "Title")
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @DynamoDBAttribute(attributeName = "ISBN")
    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String ISBN) {
        this.ISBN = ISBN;
    }

    @DynamoDBAttribute(attributeName = "Authors")
    public Set

    getBookAuthors() {
        return bookAuthors;
    }

    public void setBookAuthors(Set

                                       bookAuthors) {
        this.bookAuthors = bookAuthors;
    }

    @Override
    public String toString() {
        return "Book [ISBN=" + ISBN + ", bookAuthors=" + bookAuthors
                + ", id=" + id + ", title=" + title + "]";
    }
}

表结构操作

package net.javablog.dynamodb;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.TableCollection;
import com.amazonaws.services.dynamodbv2.model.*;

import java.util.ArrayList;
import java.util.Iterator;

public class DocumentAPITableExample {


    static String ak = "ak";
    static String sk = "sk";
    static String tableName = "ExampleTable";
    static DynamoDB dynamoDB = null;

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

        AWSCredentials awsCredentials = new BasicAWSCredentials(ak, sk);
        AmazonDynamoDBClientBuilder builder = AmazonDynamoDBClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
        builder.setRegion(Regions.AP_SOUTHEAST_1.getName());

        AmazonDynamoDB dynamoDBclient = builder.build();
        dynamoDB = new DynamoDB(dynamoDBclient);

        createExampleTable();
        listMyTables();
        getTableInformation();
        updateExampleTable();

        deleteExampleTable();

    }

    static void createExampleTable() {
        try {

            ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
            attributeDefinitions.add(new AttributeDefinition()
                    .withAttributeName("Id")
                    .withAttributeType("N"));

            ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
            keySchema.add(new KeySchemaElement()
                    .withAttributeName("Id")
                    .withKeyType(KeyType.HASH)); //Partition key

            CreateTableRequest request = new CreateTableRequest()
                    .withTableName(tableName)
                    .withKeySchema(keySchema)
                    .withAttributeDefinitions(attributeDefinitions)
                    .withProvisionedThroughput(new ProvisionedThroughput()
                            .withReadCapacityUnits(5L)
                            .withWriteCapacityUnits(6L));

            System.out.println("Issuing CreateTable request for " + tableName);
            Table table = dynamoDB.createTable(request);

            System.out.println("Waiting for " + tableName
                    + " to be created...this may take a while...");
            table.waitForActive();

            getTableInformation();

        } catch (Exception e) {
            System.err.println("CreateTable request failed for " + tableName);
            System.err.println(e.getMessage());
        }

    }

    static void listMyTables() {

        TableCollection<ListTablesResult> tables = dynamoDB.listTables();
        Iterator<Table> iterator = tables.iterator();

        System.out.println("Listing table names");

        while (iterator.hasNext()) {
            Table table = iterator.next();
            System.out.println(table.getTableName());
        }
    }

    static void getTableInformation() {

        System.out.println("Describing " + tableName);

        TableDescription tableDescription = dynamoDB.getTable(tableName).describe();
        System.out.format("Name: %s:\n" + "Status: %s \n"
                        + "Provisioned Throughput (read capacity units/sec): %d \n"
                        + "Provisioned Throughput (write capacity units/sec): %d \n",
                tableDescription.getTableName(),
                tableDescription.getTableStatus(),
                tableDescription.getProvisionedThroughput().getReadCapacityUnits(),
                tableDescription.getProvisionedThroughput().getWriteCapacityUnits());
    }

    static void updateExampleTable() {

        Table table = dynamoDB.getTable(tableName);
        System.out.println("Modifying provisioned throughput for " + tableName);

        try {
            table.updateTable(new ProvisionedThroughput()
                    .withReadCapacityUnits(6L).withWriteCapacityUnits(7L));

            table.waitForActive();
        } catch (Exception e) {
            System.err.println("UpdateTable request failed for " + tableName);
            System.err.println(e.getMessage());
        }
    }

    static void deleteExampleTable() {

        Table table = dynamoDB.getTable(tableName);
        try {
            System.out.println("Issuing DeleteTable request for " + tableName);
            table.delete();

            System.out.println("Waiting for " + tableName
                    + " to be deleted...this may take a while...");
            table.waitForDelete();
        } catch (Exception e) {
            System.err.println("DeleteTable request failed for " + tableName);
            System.err.println(e.getMessage());
        }
    }

}

数据增删改查

package net.javablog.dynamodb;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.model.*;

import java.io.IOException;
import java.util.*;

public class ObjectPersistenceCRUDExample {

    static String ak = "ak";
    static String sk = "sk";
    static String tableName = "ProductCatalog";

    static AmazonDynamoDB client;
    static DynamoDBMapper mapper;
    static int PRODUCT_ID;

    public static void main(String[] args) throws IOException {
        AWSCredentials awsCredentials = new BasicAWSCredentials(ak, sk);
        AmazonDynamoDBClientBuilder builder = AmazonDynamoDBClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
        builder.setRegion(Regions.AP_SOUTHEAST_1.getName());

        PRODUCT_ID = new Random().nextInt(1000);
        client=builder.build();
        mapper = new DynamoDBMapper(client);

        ObjectPersistenceCRUDExample demo = new ObjectPersistenceCRUDExample();

        demo.dropTable();
        demo.createTable();

        for (int i = 0; i < 100; i++) {
            System.out.println(i);
            PRODUCT_ID = PRODUCT_ID + i;
            demo.insert();
        }
        demo.getAllRows();
        CatalogItem itemRetrieved = demo.load(PRODUCT_ID);
        demo.update(itemRetrieved);
        CatalogItem updatedItem = demo.load(PRODUCT_ID);
        demo.delete(updatedItem);
        demo.load(updatedItem.getId());
        System.out.println("Example complete!");

    }


    static void dropTable() {
        Table table = new DynamoDB(client).getTable(tableName);
        try {
            System.out.println("Issuing DeleteTable request for " + tableName);
            table.delete();

            System.out.println("Waiting for " + tableName
                    + " to be deleted...this may take a while...");
            table.waitForDelete();
        } catch (Exception e) {
            System.err.println("DeleteTable request failed for " + tableName);
            System.err.println(e.getMessage());
        }
    }


    static void createTable( ) {
        try {
            CreateTableRequest createTableRequest = new CreateTableRequest()
                    .withTableName(tableName);
            createTableRequest.withKeySchema(new KeySchemaElement()
                    .withAttributeName("Id").withKeyType(KeyType.HASH));
            createTableRequest
                    .withAttributeDefinitions(new AttributeDefinition()
                            .withAttributeName("Id").withAttributeType(
                                    ScalarAttributeType.N));
            createTableRequest
                    .withProvisionedThroughput(new ProvisionedThroughput()
                            .withReadCapacityUnits(10L).withWriteCapacityUnits(
                                    10L));
            TableDescription createdTableDescription = client.createTable(
                    createTableRequest).getTableDescription();
            System.out.println("Created Table: " + createdTableDescription);
            // Wait for it to become active
            waitForTableToBecomeAvailable(tableName);
        } catch (AmazonServiceException e) {
            e.printStackTrace();
        } catch (AmazonClientException e) {
            e.printStackTrace();
        }
    }

    static void waitForTableToBecomeAvailable(String tableName) {
        System.out.println("Waiting for " + tableName + " to become ACTIVE...");
        long startTime = System.currentTimeMillis();
        long endTime = startTime + (10 * 60 * 1000);
        while (System.currentTimeMillis() < endTime) {
            try {
                Thread.sleep(1000 * 20);
            } catch (Exception e) {
            }
            try {
                DescribeTableRequest request = new DescribeTableRequest()
                        .withTableName(tableName);
                TableDescription tableDescription = client.describeTable(
                        request).getTable();
                String tableStatus = tableDescription.getTableStatus();
                System.out.println("  - current state: " + tableStatus);
                if (tableStatus.equals(TableStatus.ACTIVE.toString()))
                    return;
            } catch (AmazonServiceException ase) {
                if (ase.getErrorCode().equalsIgnoreCase(
                        "ResourceNotFoundException") == false)
                    throw ase;
            }
        }
        throw new RuntimeException("Table " + tableName + " never went active");
    }

    private void insert() {
        CatalogItem item = new CatalogItem();
        item.setId(PRODUCT_ID);
        item.setTitle("Book PRODUCT_ID");
        item.setISBN("611-1111111111");
        item.setBookAuthors(new HashSet

                (Arrays.asList("Author1",
                        "Author2")));
        // Save the item (book).
        mapper.save(item);
    }

    private void update(CatalogItem itemRetrieved) {
        itemRetrieved.setISBN("622-2222222222");
        itemRetrieved.setBookAuthors(new HashSet

                (Arrays.asList(
                        "Author1", "Author3")));
        mapper.save(itemRetrieved);
        System.out.println("Item updated:");
        System.out.println(itemRetrieved);
    }

    private void delete(CatalogItem updatedItem) {
        // Delete the item.
        mapper.delete(updatedItem);
    }

    private CatalogItem load(int id) {
        // Retrieve the updated item.
        DynamoDBMapperConfig config = new DynamoDBMapperConfig(
                DynamoDBMapperConfig.ConsistentReads.CONSISTENT);
        CatalogItem updatedItem = mapper.load(CatalogItem.class, id, config);
        if (updatedItem == null) {
            System.out.println("Done - Sample item is deleted.");
        } else {
            System.out.println("Retrieved item:");
            System.out.println(updatedItem);
        }
        return updatedItem;
    }

    private void getAllRows() {
        ScanRequest scanRequest = new ScanRequest()
                .withTableName("ProductCatalog");
        scanRequest.setLimit(10);
        HashMap scanFilter = new HashMap ();
        Condition condition = new Condition().withComparisonOperator(
                ComparisonOperator.EQ.toString()).withAttributeValueList(
                new AttributeValue().withS("611-1111111111"));
        scanFilter.put("ISBN", condition);
        Condition condition2 = new Condition().withComparisonOperator(
                ComparisonOperator.LE.toString()).withAttributeValueList(
                new AttributeValue().withN("1000"));
        scanFilter.put("Id", condition2);
        scanRequest.withScanFilter(scanFilter);
        try {
            System.out.println("Scan Request: " + scanRequest);
            ScanResult scanResponse = client.scan(scanRequest);
            for (Map item : scanResponse.getItems()) {
                System.out.println(item.get("Id")  + " , " +
                        item.get("ISBN") + " , " +
                        item.get("Authors") + " , " +
                        item.get("Title") );
            }

            System.out.println("Scan Response: " + scanResponse);
            System.out.println("Count: " + scanResponse.getCount());
            System.out.println("Scanned Count: "
                    + scanResponse.getScannedCount());
            System.out.println("Items: " + scanResponse.getItems());
        } catch (AmazonServiceException e) {
            e.printStackTrace();
        } catch (AmazonClientException e) {
            e.printStackTrace();
        }
    }


}

pom.xml

<dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-dynamodb</artifactId>
            <version>1.11.109</version>
</dependency>