This topic describes how to connect to a DynamoDB-compatible ApsaraDB for MongoDB instance by using the Amazon Web Services (AWS) CLI and SDKs for programing languages such as Python and Java.
Prerequisites
Preparations
- Obtain the connection string of a DynamoDB-compatible ApsaraDB for MongoDB instance.
- Log on to the ApsaraDB for MongoDB console.
- In the top navigation bar, select the resource group and region to which the sharded cluster instance belongs.
- In the left-side navigation pane, click Sharded Cluster Instances.
- On the page that appears, find the instance and click its ID.
- In the left-side navigation pane, click Database Connection.
- In the Internal Connections - VPC section, view the connection sting of the instance.
- Optional:If your application is deployed on an Elastic Compute Service (ECS) instance, make
sure that your sharded cluster instance and the ECS instance meet the following requirements
to ensure network connectivity:
- Your sharded cluster instance and the ECS instance belong to the same region. You can view the region of a created ECS instance. For more information, see View instance information.
- Optional:Your sharded cluster instance and the ECS instance belong to the same zone. This reduces network latency. You can view the zone of a created ECS instance. For more information, see View instance information.
- Your sharded cluster instance and the ECS instance reside in the same type of network. You can view the network type of a created ECS instance. For more information, see View instance information. If an ECS instance resides in the classic network, you can migrate the ECS instance from the classic network to a virtual private cloud (VPC). For more information, see Migrate an ECS instance from the classic network to a VPC.
Use the AWS CLI to connect to the instance
This section demonstrates how to use the AWS CLI to connect to the DynamoDB-compatible ApsaraDB for MongoDB instance. Ubuntu 16.04.6 LTS is used in the example. For more information about the AWS CLI, see What is the AWS Command Line Interface?
Use the SDK for Python to connect to the instance
- Python 2.6 or later is installed. For more information, visit the official website of Python.
- Boto3 is installed. For more information, see Boto3 documentation.
Book
:import boto3
def create_book_table(dynamodb=None):
if not dynamodb:
dynamodb = boto3.resource('dynamodb', endpoint_url="http://dds-xxxx.mongodb.rds.aliyuncs.com:3717")
table = dynamodb.create_table(
TableName='Book',
KeySchema=[
{
'AttributeName':'title',
'KeyType':'HASH'
},
{
'AttributeName':'year',
'KeyType':'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName':'title',
'AttributeType':'S'
},
{
'AttributeName':'year',
'AttributeType':'N'
},
],
ProvisionedThroughput={
'ReadCapacityUnits':5,
'WriteCapacityUnits':5
}
)
return table
if __name__ == '__main__':
book_table =create_book_table()
print("Tablestatus:", book_table.table_status)
Use the SDK for Java to connect to the instance
Install AWS SDK for Java. For more information, see Setting up the AWS SDK for Java 2.x.
Book
:package com.amazonaws.codesamples.gsg;
import java.util.Arrays;
import com.amazonaws.client.builder.AwsClientBuilder;
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.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
public class MoviesCreateTable {
public static void main(String[] args) throws Exception {
AmazonDynamoDB client =AmazonDynamoDBClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://dds-xxxx.mongodb.rds.aliyuncs.com:3717",
"us-east-1"))
.build();
DynamoDB dynamoDB = new DynamoDB(client);
String tableName ="Book";
try {
System.out.println("Creating table...");
Table table =dynamoDB.createTable(tableName,
Arrays.asList(new
KeySchemaElement("title", KeyType.HASH), // Partition key
new KeySchemaElement("year", KeyType.RANGE)), // Sort key
Arrays.asList(new AttributeDefinition("title", ScalarAttributeType.S),
new AttributeDefinition("year", ScalarAttributeType.N)),
new ProvisionedThroughput(5L, 5L));
table.waitForActive();
System.out.println("OK. Table status: " + table.getDescription().getTableStatus());
}
catch (Exception e) {
System.err.println("Unable to create table: ");
System.err.println(e.getMessage());
}
}
}
Use SDKs for other programing languages to connect to the instance
For more information, see Getting Started with DynamoDB and AWS SDKs.