Help Center/ GeminiDB/ GeminiDB Cassandra API/ GeminiDB (DynamoDB API Compatible) Instance/ Connecting to a GeminiDB (DynamoDB API Compatible) Instance
Updated on 2025-01-26 GMT+08:00

Connecting to a GeminiDB (DynamoDB API Compatible) Instance

This section describes how to connect to a GeminiDB (DynamoDB API compatible) instance using Java or Python.

Prerequisites

  • A GeminiDB (DynamoDB API compatible) instance has been created.
  • For details about how to create an ECS, see Purchasing an ECS in Getting Started with Elastic Cloud Server.
  • JDK has been installed on the ECS.

Obtaining the IP Address of a GeminiDB (DynamoDB API compatible) Instance

  1. Log in to the Huawei Cloud console.
  2. In the service list, choose Databases > GeminiDB Cassandra API.
  3. On the Instances page, click the name of the target instance.

    The GeminiDB (DynamoDB API compatible) instance uses port 8000.

    Method 1

    In the Node Information area on the Basic Information page, obtain the private IP address or EIP of each node.

    Figure 1 Obtaining IP addresses

    Method 2

    In the navigation pane, choose Connections to obtain the private IP address and EIP of the instance.

    Figure 2 Obtaining IP addresses

Using a Load Balancing Plug-in to Connect to a GeminiDB (DynamoDB API Compatible) Instance

  1. To obtain the JAR package and plug-in code, choose Service Tickets > Create Service Ticket in the upper right corner of the console and contact customer service.
  2. Replace the IP address in the following code example with the IP address queried in 3.

    The following is a Java code example:

    package com.huawei.dbs.test;  
    
    import com.huawei.dbs.RequestHandler;  
    
    import com.amazonaws.SDKGlobalConfiguration;  
    import com.amazonaws.auth.AWSCredentialsProvider;  
    import com.amazonaws.auth.AWSStaticCredentialsProvider;  
    import com.amazonaws.auth.BasicAWSCredentials;  
    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.TableCollection;  
    
    import java.net.URI;  
      
    
    public class V1Demo {  
        public static AWSCredentialsProvider myCredentials = new AWSStaticCredentialsProvider(  
            new BasicAWSCredentials("your_ak", "your_sk"));  
        public static String ip = "***.***.***.***";  
        public static void main(String[] args) {  
            disableCertificateChecks();  
            AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()  
                .withRegion("region-a")  
                .withRequestHandlers(new RequestHandler(URI.create("http://" + ip + "8000")))  
                .withCredentials(myCredentials)  
                .build();  
            DynamoDB dynamoDB = new DynamoDB(client);  
            TableCollection res = dynamoDB.listTables();  
            System.out.println(res);  
        }  
      
    }

    The following is a Python code example:

    #!/usr/bin/python  
    import boto3  
    import boto3_lb  
    
    ip = '***.***.***.***'  
    url = boto3_lb.setup([ip], 'http', 8000, 'fake.url.com')  
    dynamodb = boto3.resource('dynamodb',  
                              endpoint_url=url,  
                              aws_access_key_id='ak',  
                              aws_secret_access_key='sk',  
                              region_name="region-a")  
    
    url = boto3_lb.setup_single_connection([ip], 'http', 8000, 'fake.url.com')  
    
    dynamodb = boto3.resource('dynamodb',  
                              endpoint_url=url,  
                              aws_access_key_id='ak',  
                              aws_secret_access_key='sk',  
                              region_name="region-a")  

Connecting to an Instance over HTTPS

Prerequisites:

1. For compatibility purposes, you can still use HTTP after SSL is enabled. To meet high security requirements, you can modify a parameter to disable HTTP. For details, choose Service Tickets > Create Service Ticket in the upper right corner of the console and contact customer service.

2. Currently, an EIP cannot be used over HTTPS.

Procedure

  1. Add Maven dependencies. Add dependencies related to AWS SDK for Java 2.x to the pom.xml file.

    <dependencies>
         <dependency>
             <groupId>software.amazon.awssdk</groupId>
             <artifactId>dynamodb</artifactId>
             <version>2.x.x</version>
         </dependency>
     </dependencies>

  2. Connect to the GeminiDB (DynamoDB API compatible) instance using DynamoDBClient over HTTPS.

    The following is a Java code example:
    import com.amazonaws.auth.AWSCredentialsProvider;
    import com.amazonaws.auth.AWSStaticCredentialsProvider;
    import com.amazonaws.auth.BasicAWSCredentials;
    import com.amazonaws.client.builder.AwsClientBuilder;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
    import com.amazonaws.services.dynamodbv2.model.*;
    public class SourceDemo {
    // AK/SK credentials can be automatically obtained from environment variable file home/.aws/credentials.
    // If there is no such a file, you need to manually specify the AK/SK credentials.
        public static AWSCredentialsProvider myCredentials = new AWSStaticCredentialsProvider(
                new BasicAWSCredentials("your_ak", "your_sk"));
        public static void main(String[] args) {
            AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
                    .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("https://127.0.0.1:8000", "region_a"))
                    .withCredentials(myCredentials)
                    .build();
            System.out.println(client.listTables());
            CreateTableRequest request = new CreateTableRequest()
                    .withTableName("test_001")
                    .withProvisionedThroughput(new ProvisionedThroughput(1000L, 1000L))
                    .withKeySchema(
                            new KeySchemaElement("id", KeyType.HASH)
                    )
                    .withAttributeDefinitions(
                            new AttributeDefinition("id", ScalarAttributeType.N)
                    );
            System.out.println(client.createTable(request));
        }
    }

GeminiDB (DynamoDB API compatible) is completely compatible with DynamoDB. For details about common operations, see official DynamoDB documents.