AnalyticDB for PostgreSQL supports SDKs for Java and Python.
Configure an access credential
Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M.
Configure the environment variables ADBPG_AK_ENV
and ADBPG_SK_ENV
.
Configuration method in Linux and macOS
Run the following commands:
export ADBPG_AK_ENV=<access_key_id> export ADBPG_SK_ENV=<access_key_secret>
Specify your own AccessKey ID and AccessKey secret for the
<access_key_id>
and<access_key_secret>
parameters.Configuration method in Windows
Create the environment variables
ADBPG_AK_ENV
andADBPG_SK_ENV
, and set them to your own AccessKey ID and AccessKey secret.Restart the Windows operating system.
Use SDK for Java
Make sure that JDK 1.6.0 or later is used.
Procedure
Install AnalyticDB for PostgreSQL SDK for Java.
If you use Maven to manage Java projects, add Maven dependencies to the pom.xml file.
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83_noneautotype</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.4.0</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-gpdb</artifactId> <version>1.0.19</version> </dependency>
Use AnalyticDB for PostgreSQL SDK for Java.
The following sample code shows how to use AnalyticDB for PostgreSQL SDK for Java:
Create a DefaultAcsClient instance and initialize the instance.
Create an API request and configure the required parameters.
Initiate the API request and handle the response or exceptions.
import com.alibaba.fastjson.JSON; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.gpdb.model.v20160503.DescribeSQLCollectorPolicyRequest; import com.aliyuncs.gpdb.model.v20160503.DescribeSQLCollectorPolicyResponse; import com.aliyuncs.profile.DefaultProfile; public class GpdbDemo2 { public static void main(String args[]) { // We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and all resources within your account are exposed to risks. // In this example, the AccessKey ID and AccessKey secret are stored in environment variables. Before you run this code, configure an access credential. DefaultProfile profile = DefaultProfile.getProfile("${region}", System.getenv("ADBPG_AK_ENV"), System.getenv("ADBPG_SK_ENV")); IAcsClient client = new DefaultAcsClient(profile); // Create an API request and configure the required parameters. String insName = "${insName}"; DescribeSQLCollectorPolicyRequest request = new DescribeSQLCollectorPolicyRequest(); request.setDBInstanceId(insName); // Initiate the API request and handle the response or exception. try { DescribeSQLCollectorPolicyResponse response = client.getAcsResponse(request); System.out.println(JSON.toJSONString(response)); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); System.out.println("RequestId:" + e.getRequestId()); } } }
Use SDK for Python
Make sure that Python 2 or Python 3 is used.
Procedure
Run the following pip command to install AnalyticDB for PostgreSQL SDK for Python:
pip install aliyun-python-sdk-gpdb
Use AnalyticDB for PostgreSQL SDK for Python.
In this example, Python 2 is used. The following sample code shows how to use AnalyticDB for PostgreSQL SDK for Python:
Create a Client instance. When you create the Client instance, you must obtain a region ID, AccessKey ID, and AccessKey secret.
Create an API request and configure the required parameters.
Initiate the API request and handle the response or exceptions.
# from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkgpdb.request.v20160503.DescribeSQLCollectorPolicyRequest import DescribeSQLCollectorPolicyRequest # Create an AcsClient instance. # ${regionId}: the region ID. client = AcsClient( os.getenv('ADBPG_AK_ENV'), os.getenv('ADBPG_SK_ENV'), "${regionId}" ) # We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and all resources within your account are exposed to risks. # In this example, the AccessKey ID and AccessKey secret are stored in environment variables. Before you run this code, configure an access credential. # Create an API request and configure the required parameters. request = DescribeSQLCollectorPolicyRequest() # ${insName}: the instance name. request.set_DBInstanceId("${insName}") # Initiate the API request and handle the response or exceptions. try: response = client.do_action_with_exception(request) print response except ServerException as e: print e except ClientException as e: print e