更新时间:2024-10-30 GMT+08:00
连接GeminiDB DynamoDB兼容版实例
本章节主要介绍使用Java和Python语言连接GeminiDB DynamoDB兼容版实例的基本操作。
前提条件
- 已成功创建GeminiDB DynamoDB协议兼容版实例。
- 已创建弹性云服务器,创建弹性云服务器的方法,请参见《弹性云服务器快速入门》中“创建弹性云服务器”章节。
- 弹性云服务器上已经安装JDK环境。
查看GeminiDB DynamoDB兼容版实例IP地址
使用负载均衡插件连接实例
- 依赖的Jar包和插件代码可联系客服提供。
- 将下面代码示例中的IP替换为3查到的IP。
Java样例如下:
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); } }
Python样例如下:
#!/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协议兼容版实例与DynamoDB接口完全兼容,故最常用的操作请参考DynamoDB官方文档。
父主题: DynamoDB协议兼容版实例