全部产品
云市场

快速入门

更新时间:2019-10-15 10:03:50

开通服务

购买cassandra数据库后,点击查看集群详情,选择数据库连接tab页,我们可以看到开通Dynamodb功能 链接kaitong

点击开通后,可以得到一个url地址,这个地址后续会绑定公网IPa2

购买ecs,并加入访问白名单

在数据安全页加入客户端ecs ip,之后即可访问dynamodb数据库地址

whitelist

在客户端ecs上telnet host验证一下,如果telnet上了,说明链路畅通

  1. telnet $host 80

host是开通dynamodb数据库给的地址

使用aws cli访问数据库

conf_cli

  • 使用cli 创建表,注意需要指定全部的列

    1. aws dynamodb create-table --table-name MusicCollection --attribute-definitions AttributeName=Artist,AttributeType=S AttributeName=SongTitle,AttributeType=S AttributeName=AlbumTitle,AttributeType=S AttributeName=tag,AttributeType=S AttributeName=newcol,AttributeType=S \
    2. --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE --local-secondary-indexes IndexName=aTitle,KeySchema=["{AttributeName=Artist,KeyType=HASH},{AttributeName=AlbumTitle,KeyType=RANGE}"],Projection={ProjectionType=KEYS_ONLY} --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 --endpoint-url http://$host
  • put-item,注意不能指上述建表语句attribute-definitions 以外的列,否则会插入失败

  1. aws dynamodb put-item --table-name MusicCollection --item '{"Artist": {"S": "No One You Know"},"SongTitle": {"S": "Call Me Today"},"AlbumTitle": {"S": "Somewhat Famous"}, "tag": {"S": "tag1,tag2"}, "newcol": {"S": "new_col_val"}}' --return-consumed-capacity TOTAL --endpoint-url http://$host
  • get-item,获取上述item
  1. aws dynamodb get-item --table-name MusicCollection --key '{"Artist": { "S": "No One You Know"},"SongTitle": {"S": "Call Me Today"}}' --endpoint-url http://$host
  2. 响应:
  3. {
  4. "Item": {
  5. "Artist": {
  6. "S": "No One You Know"
  7. },
  8. "newcol": {
  9. "S": "new_col_val"
  10. },
  11. "tag": {
  12. "S": "tag1,tag2"
  13. },
  14. "SongTitle": {
  15. "S": "Call Me Today"
  16. },
  17. "AlbumTitle": {
  18. "S": "Somewhat Famous"
  19. }
  20. }
  21. }

使用aws dynamodb sdk访问

注意需要在BasicAWSCredentials中指定数据库用户名,密码。之后就可以正常访问数据库了。

  1. AWSStaticCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(new BasicAWSCredentials("$user,$password", "fake"));
  2. AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
  3. new AwsClientBuilder.EndpointConfiguration($host),
  4. "us-west-2"))
  5. .withCredentials(credentialsProvider)
  6. .build();
  7. DynamoDB dynamoDB = new DynamoDB(client);
  8. //TODO: use dynamoDB to create table