AnalyticDB for PostgreSQL supports Alibaba Cloud Software Development Kits (SDKs) for Java and Python.

Example of using the Alibaba Cloud AnalyticDB for PostgreSQL SDK for Java

  • Make sure that JDK 1.6.0 or later is used.
  • This section introduces the procedure of using the Alibaba Cloud AnalyticDB for PostgreSQL SDK for Java:
    1. Install the Alibaba Cloud 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.37</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.7</version>
      </dependency>
    2. Use the Alibaba Cloud AnalyticDB for PostgreSQL SDK for Java.

      The following sample code shows how to use the Alibaba Cloud AnalyticDB for PostgreSQL SDK for Java:

      1. Create a DefaultAcsClient instance and initialize the instance.
      2. Create an API request and configure the parameters.
      3. 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[]) {
               /*
               Create a DefaultAcsClient instance and initialize the instance.
               ${region}: the region name
               ${accessKey}: the AccessKey ID of the user
               ${secret}: the AccessKey secret of the user
               */
              DefaultProfile profile = DefaultProfile.getProfile("${region}",
                      "${accessKey}", "${secret}");
              IAcsClient client = new DefaultAcsClient(profile);
              // Create an API request and configure the parameters.
              String insName = "${insName}";
              DescribeSQLCollectorPolicyRequest request
      = new DescribeSQLCollectorPolicyRequest();
              request.setDBInstanceId(insName);
              // Initiate the API request and handle the response or exceptions.
              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());
              }
      
          }
      }

Example of using the Alibaba Cloud AnalyticDB for PostgreSQL SDK for Python

  • Make sure that Python 2 or Python 3 is used.
  • This section introduces the procedure of using the Alibaba Cloud AnalyticDB for PostgreSQL SDK for Python.
    1. Run the following pip command to install the Alibaba Cloud AnalyticDB for PostgreSQL SDK for Python:
      pip install aliyun-python-sdk-gpdb
    2. Use the Alibaba Cloud AnalyticDB for PostgreSQL SDK for Python.

      The following sample code uses Python 2 as an example to show how to use the Alibaba Cloud AnalyticDB for PostgreSQL SDK for Python:

      1. Create an AcsClient instance. Before you create an AcsClient instance, you must obtain the region ID, the AccessKey ID, and the AccessKey secret.
      2. Create an API request and configure the parameters.
      3. 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. 
      # ${accessKey}: the AccessKey ID of the user
      # ${accessSecret}: the AccessKey secret of the user
      # ${regionId}: the region ID
      client = AcsClient(
              "${accessKey}",
              "${accessSecret}",
              "${regionId}"
              )
      # Create an API request and configure the parameters.
      request = DescribeSQLCollectorPolicyRequest()
      # ${insName}: the instance name
      request.set_DBInstanceId("${insName}")               
      # Initiate the API request and display the response. 
      try:
          response = client.do_action_with_exception(request)
          print response
      except ServerException as e:
          print e
      except ClientException as e:
          print e