ABTest SDK for Java
Use the ABTest SDK for Java to allocate experiment traffic and retrieve parameter configurations for your experiments.
Prerequisites
Before you begin, ensure that you have:
Environment variables configured in the runtime environment. For more information, see Configure environment variables in Linux, macOS, and Windows
An experiment created. For more information, see Create an experiment
Add dependencies
Add the following dependencies to the <dependencies> section of your pom.xml file.
<dependency>
<groupId>com.aliyun.openservices.aiservice</groupId>
<artifactId>pai-abtest-sdk</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>Initialize the client
Create a Configuration object with your region and credentials, then initialize the experiment client by calling init(), which loads experiment data from the server before traffic allocation is available.
package com.aliyun.openservices.paiabtest;
import com.aliyun.openservices.paiabtest.api.ApiClient;
import com.aliyun.openservices.paiabtest.api.Configuration;
import com.aliyun.openservices.paiabtest.model.ExperimentContext;
import com.aliyun.openservices.paiabtest.model.ExperimentResult;
import java.util.HashMap;
import java.util.Map;
public class ExperimentTest {
static ExperimentClient experimentClient;
public static void main(String[] args) throws Exception {
// Initialize the experiment client
String regionId = "cn-beijing";
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
Configuration configuration = new Configuration(regionId, accessId, accessKey);
ApiClient apiClient = new ApiClient(configuration);
experimentClient = new ExperimentClient(apiClient);
// Load experiment data before calling matchExperiment
experimentClient.init();
}
}Callinit()before making any calls tomatchExperiment(). The method loads experiment data from the server and must complete before traffic allocation is available.
Match an experiment
Build an ExperimentContext with the user's ID and any filter parameters, then call matchExperiment() to assign the user to an experiment group and retrieve the corresponding parameters.
// Set up the experiment context
ExperimentContext experimentContext = new ExperimentContext();
experimentContext.setRequestId("<req_id>");
// Set the user ID used for traffic allocation
experimentContext.setUid("<uid>");
// Add filter parameters for condition-based matching
Map<String, Object> filterParams = new HashMap<>();
filterParams.put("sex", "male");
experimentContext.setFilterParams(filterParams);
// Match the user to an experiment
ExperimentResult experimentResult = experimentClient.matchExperiment("<DefaultProject>", experimentContext);
// Read experiment information and parameters
System.out.println(experimentResult.info());
System.out.println(experimentResult.getExpId());
System.out.println(experimentResult.getExperimentParams().get("recall_v", "not exist"));
System.out.println(experimentResult.getExperimentParams().get("rank_v", "not exist"));
System.out.println(experimentResult.getExperimentParams().get("male_v", "not exist"));Parameters
| Parameter | Description | Required |
|---|---|---|
regionId | The ID of the region where your ABTest project is deployed, such as cn-hangzhou for China (Hangzhou). | Yes |
<req_id> | A custom request ID used to trace the request in logs. | Yes |
<uid> | The ID used for traffic allocation. Can be a user ID, device ID, or any other identifier that uniquely represents the entity being assigned to an experiment. | Yes |
filterParams | Key-value pairs used for condition-based experiment matching, such as "sex": "male". Modify these based on the filter conditions defined in your experiment. | No |
<DefaultProject> | The name of the ABTest project. Find it on the Project Management > Experiment Projects page. For more information, see Create an experiment project. | Yes |
The second argument to getExperimentParams().get(key, defaultValue) is returned when the specified key is not found in the experiment parameters.