Amazon DynamoDB
开发人员指南 (API Version 2012-08-10)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

步骤 4:运行示例应用程序

为了帮助您测试 DAX 功能,我们提供了一个可在您的 Amazon EC2 实例上运行的 Java 程序。该程序包含三个源文件:

  • TryDax.java - 程序的入口点。

  • TryDaxHelper.java - 针对 DynamoDB 和 DAX 客户端以及用于创建测试表和数据的实用程序方法。

  • TryDaxTests.java - 测试表的 GetItemQueryScan 活动。

  1. 安装 Java 开发工具包 (JDK):

    Copy
    sudo yum install -y java-devel
  2. 下载 AWS SDK for Java (.zip 文件),然后解压缩此文件:

    Copy
    wget http://sdk-for-java.amazonwebservices.com/latest/aws-java-sdk.zip unzip aws-java-sdk.zip
  3. 下载最新版本的 DAX Java 客户端 (.jar 文件):

    Copy
    wget http://dax-sdk.s3-website-us-west-2.amazonaws.com/java/DaxJavaClient-latest.jar
  4. 设置您的 CLASSPATH 变量:

    Copy
    export SDKVERSION=sdkVersion export CLASSPATH=.:./DaxJavaClient-latest.jar:aws-java-sdk-$SDKVERSION/lib/aws-java-sdk-$SDKVERSION.jar:aws-java-sdk-$SDKVERSION/third-party/lib/*

    sdkVersion 替换为 AWS 适用于 Java 的开发工具包 的实际版本号。例如:1.11.112

  5. 下载示例程序源代码 (.zip 文件):

    Copy
    wget http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/TryDax.zip

    下载完成后,解压缩源文件:

    Copy
    unzip TryDax.zip
  6. 按如下所示编译代码:

    Copy
    javac TryDax*.java
  7. 运行程序:

    Copy
    java TryDax

    您应该可以看到类似于如下所示的输出内容:

    Copy
    Creating a DynamoDB client Attempting to create table; please wait... Successfully created table. Table status: ACTIVE Writing data to the table... Writing 10 items for partition key: 1 Writing 10 items for partition key: 2 Writing 10 items for partition key: 3 Writing 10 items for partition key: 4 Writing 10 items for partition key: 5 Writing 10 items for partition key: 6 Writing 10 items for partition key: 7 Writing 10 items for partition key: 8 Writing 10 items for partition key: 9 Writing 10 items for partition key: 10 Running GetItem, Scan, and Query tests... First iteration of each test will result in cache misses Next iterations are cache hits GetItem test - partition key 1 and sort keys 1-10 Total time: 136.681 ms - Avg time: 13.668 ms Total time: 122.632 ms - Avg time: 12.263 ms Total time: 167.762 ms - Avg time: 16.776 ms Total time: 108.130 ms - Avg time: 10.813 ms Total time: 137.890 ms - Avg time: 13.789 ms Query test - partition key 5 and sort keys between 2 and 9 Total time: 13.560 ms - Avg time: 2.712 ms Total time: 11.339 ms - Avg time: 2.268 ms Total time: 7.809 ms - Avg time: 1.562 ms Total time: 10.736 ms - Avg time: 2.147 ms Total time: 12.122 ms - Avg time: 2.424 ms Scan test - all items in the table Total time: 58.952 ms - Avg time: 11.790 ms Total time: 25.507 ms - Avg time: 5.101 ms Total time: 37.660 ms - Avg time: 7.532 ms Total time: 26.781 ms - Avg time: 5.356 ms Total time: 46.076 ms - Avg time: 9.215 ms Attempting to delete table; please wait... Successfully deleted table.

    记下计时信息 - GetItemQueryScan 测试所需的时间 (以毫秒为单位)。

  8. 在上一步中,您已针对 DynamoDB 终端节点运行程序。您现在将重新运行程序,但这一次,GetItemQueryScan 操作将由 DAX 集群处理。

    要确定 DAX 集群的终端节点,请选择下列选项之一:

    • Using the DynamoDB console - 选择您的 DAX 集群。集群终端节点显示在控制台中。例如:

      Copy
      mycluster.frfx8h.clustercfg.dax.amazonaws.com:8111
    • Using the AWS CLI - 键入以下命令:

      Copy
      aws dax describe-clusters --query "Clusters[*].ClusterDiscoveryEndpoint"

      集群终端节点端口和地址显示在输出中。例如:

      Copy
      { "Port": 8111, "Address":"mycluster.frfx8h.clustercfg.dax.amazonaws.com" }

    现在重新运行程序 - 但这一次,将集群终端节点指定为命令行参数:

    Copy
    java TryDax mycluster.frfx8h.clustercfg.dax.amazonaws.com:8111

    查看输出的其余内容,并记下计时信息。使用 DAX 时的 GetItemQueryScan 的运行时间应明显少于使用 DynamoDB 时的运行时间。

有关此程序的更多信息,请参阅以下各节: