Help Center/ GeminiDB/ GeminiDB Cassandra API/ GeminiDB DynamoDB Instances/ Connecting to a GeminiDB DynamoDB Instance
Updated on 2024-11-20 GMT+08:00

Connecting to a GeminiDB DynamoDB Instance

This section describes how to connect to a GeminiDB DynamoDB instance in Java or using Python.

Prerequisites

  • A GeminiDB DynamoDB instance has been created.
  • For details about how to create an ECS, see Getting Started > Purchasing an ECS in the Elastic Cloud Server User Guide.
  • JDK has been installed on the ECS.

Obtaining the IP Address of a GeminiDB DynamoDB Instance

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

    The port number of the GeminiDB DynamoDB instance is 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 on the left, click Connections to obtain the private IP addresses and EIPs of the instance.

    Figure 2 Obtaining IP addresses

Using the Load Balancing Plug-in to Connect to a GeminiDB DynamoDB Instance

  1. Contact the customer service personnel to obtain required JAR packages and plug-in code.
  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;  
    import java.security.cert.X509Certificate;  
    
    import javax.net.ssl.HttpsURLConnection;  
    import javax.net.ssl.SSLContext;  
    import javax.net.ssl.TrustManager;  
    import javax.net.ssl.X509TrustManager;  
    
    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);  
        }  
    
        static void disableCertificateChecks() {  
            System.setProperty(SDKGlobalConfiguration.DISABLE_CERT_CHECKING_SYSTEM_PROPERTY, "true");  
            TrustManager[] trustAllCerts = new TrustManager[] {  
                new X509TrustManager() {  
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
                        return null;  
                    }  
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {  }  
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {  }  
                }  
            };  
            try {  
                SSLContext sc = SSLContext.getInstance("SSL");  
                sc.init(null, trustAllCerts, new java.security.SecureRandom());  
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            HttpsURLConnection.setDefaultHostnameVerifier((arg0, arg1) -> true);  
        }  
    
    }

    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")  

GeminiDB DynamoDB instances are compatible with DynamoDB APIs. For details about common operations, see DynamoDB official documents.