All Products
Search
Document Center

SDK for Java

Last Updated: Jul 28, 2021

Download link

Artificial Intelligence Recommendation (AIRec) SDK for Java allows Java developers to use the Java program to manage the AIRec service. Developers can use Maven dependencies to add AIRec SDK for Java. They can also download the AIRec SDK for Java package to their on-premises machines. The SDK for Java supports Java 2 Platform, Standard Edition (J2SE) 6.0 and later.

You can click here to download AIRec SDK for Java of the latest version. You can click here to view the source code of AIRec SDK for Java of the latest version.

Procedure

To use AIRec SDK for Java, perform the following steps:

Step 1: Create an Alibaba Cloud account

For more information, see Sign up with Alibaba Cloud. We recommend that you complete real-name verification at your earliest opportunity. Otherwise, you cannot use some Alibaba Cloud services.

Step 2: Obtain an Alibaba Cloud AccessKey pair

Before you use AIRec SDK for Java, you must apply for an Alibaba Cloud AccessKey pair.

Go to the Security Management page. Select an AccessKey pair for AIRec SDK for Java. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. If no AccessKey pair is available, create one and make sure that it is in the Enable state. For more information about how to create an AccessKey pair, see Preparations.

Step 3: Set up the Java development environment

AIRec SDK for Java supports Java development environments of J2SE 6.0 or later. You can visit the Java official website to download and set up the Java development environment.

Step 4: Install the SDK for Java

We recommend that you download the latest SDK for Java package by using Apache Maven. You can add the following dependencies to your Maven project. aliyun-java-sdk-core is the core library. We recommend that you use aliyun-java-sdk-core 4.2.2 or later. aliyun-java-sdk-core uses gson 2.8.2 as a dependency. We recommend that you manually configure the Gson dependency. If you have used Gson of an earlier version in your business, we recommend that you upgrade your Gson to 2.8.2 or later.

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-airec</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.2.2</version>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>

Step 5: Use AIRec SDK for Java

In the following example, the DescribeInstance operation is used to describe how to use AIRec SDK for Java. The IClientProfile and IAcsClient classes are contained in the aliyun-java-sdk-core library, and other classes are contained in the aliyun-java-sdk-airec library.

package com.aliyun.airec;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.airec.model.v20181012.DescribeInstanceRequest;
import com.aliyuncs.airec.model.v20181012.DescribeInstanceResponse;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class DescribeInstance {
    public static void main(String args[]) {
        // 1. Create a profile.
        // Create a profile of the IClientProfile class. The profile stores AccessKey ID, AccessKey secret, and the default region information, such as cn-hangzhou.
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyID, accessKeySecret);
        // 2. Configure the endpoint.
        // Use the addEndpoint method of the DefaultProfile class to specify the region ID, service name, and endpoint.
        DefaultProfile.addEndpoint("cn-hangzhou", "Airec", "airec.cn-hangzhou.aliyuncs.com");

        // 3. Create a client.
        // Create a client of the IAcsClient class based on the profile of the IClientProfile class. Subsequent responses are obtained from the profile of the IClientProfile class.
        DefaultAcsClient client = new DefaultAcsClient(profile);

        // 4. Create a request.
        // Create a request for a specific API operation. The class of the request is named by adding Request to the end of the API operation name.
        // For example, the name of the API operation that is used to query instance details is DescribeInstance. In this case, the name of the request class for this operation is DescribeInstanceRequest. You can use a constructor to generate a default request class.
        DescribeInstanceRequest request = new DescribeInstanceRequest();

        // 5. Configure parameters for the request.
        // Configure parameters for the request. After the request class is generated, you must configure the required parameters for the API request by calling the setXxx operation.
        // For example, you want to specify an instance ID. In this case, you must specify the instance ID in the API request. If you want the system to return responses in the JSON format, set AcceptFormat to JSON in the API request.
        request.setInstanceId("airec-cn-xxx");
        request.setAcceptFormat(FormatType.JSON);

        try {
            // 6. Initiate the request by using the method that is supported by the client and obtain the response.
            DescribeInstanceResponse response = client.getAcsResponse(request);

            // 7. Obtain the values of parameters in the response.
            // Call the getXxx operation in the response to obtain values of response parameters. For example, you can call the getName operation to obtain the name of the instance.
            System.out.println(response.getResult().getName());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}