Use Simple Log Service SDK for Java to manage machine groups
Create, modify, query, and delete machine groups in Simple Log Service by using the SDK for Java. Sample code is provided for each operation.
Prerequisites
Simple Log Service is activated.
Simple Log Service SDK for Java is installed. For more information, see Install the Java SDK.
A project is created. For more information, see Use Simple Log Service SDK for Java to manage projects.
The
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variables are configured. These variables are used for authentication when you initialize the Simple Log Service client.
Precautions
In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used. Endpoint: https://cn-hangzhou.log.aliyuncs.com.
If you want to access Simple Log Service from other Alibaba Cloud services that reside in the same region as your project, you can use the internal Simple Log Service endpoint, which is https://cn-hangzhou-intranet.log.aliyuncs.com.
For more information about the supported regions and endpoints of Simple Log Service, see Endpoint.
Create a machine group
The following code creates an IP-based machine group named ali-test-machinegroup:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.GroupAttribute;
import com.aliyun.openservices.log.common.MachineGroup;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.CreateMachineGroupRequest;
import java.util.Collections;
public class CreateMachineGroup {
public static void main(String[] args) throws LogException {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. This example uses China (Hangzhou). For other regions, specify the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Set the machine group name.
String machineGroupName = "ali-test-machinegroup";
System.out.println("ready to create machinegroup");
MachineGroup machineGroup = new MachineGroup();
machineGroup.SetGroupName(machineGroupName);
// Create an IP-based machine group.
machineGroup.SetMachineIdentifyType("ip");
// Set the attributes of the machine group.
GroupAttribute groupAttribute = new GroupAttribute();
// Set the machine group topic.
groupAttribute.SetGroupTopic("ide test");
machineGroup.SetGroupAttribute(groupAttribute);
// Set the machine group identifier. In this example, an IP address is used.
machineGroup.SetMachineList(Collections.singletonList("192.168.XX.XX"));
// Create the machine group.
CreateMachineGroupRequest request = new CreateMachineGroupRequest(projectName, machineGroup);
client.CreateMachineGroup(request);
System.out.println(String.format("create machinegroup %s success", machineGroupName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}Expected result:
ready to create machinegroup
create machinegroup ali-test-machinegroup successModify a machine group
The following code modifies the IP-based machine group named ali-test-machinegroup:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.GroupAttribute;
import com.aliyun.openservices.log.common.MachineGroup;
import com.aliyun.openservices.log.exception.LogException;
import java.util.Collections;
public class UpdateMachineGroup {
public static void main(String[] args) throws LogException {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. This example uses China (Hangzhou). For other regions, specify the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Enter the machine group name.
String machineGroupName = "ali-test-machinegroup";
System.out.println("ready to update machinegroup");
MachineGroup machineGroup = new MachineGroup();
machineGroup.SetGroupName(machineGroupName);
// Set the machine group to an IP-based machine group.
machineGroup.SetMachineIdentifyType("ip");
// Set the attributes of the machine group.
GroupAttribute groupAttribute = new GroupAttribute();
// Modify the machine group topic.
groupAttribute.SetGroupTopic("new ide test");
machineGroup.SetGroupAttribute(groupAttribute);
// Update the machine group.
client.UpdateMachineGroup(projectName, machineGroup);
System.out.println(String.format("update machinegroup %s success", machineGroupName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}Expected result:
ready to update machinegroup
update machinegroup ali-test-machinegroup successQuery all machine groups
The following code lists all machine groups in the specified project:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.ListMachineGroupResponse;
public class ListMachineGroup {
public static void main(String[] args) throws LogException {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. This example uses China (Hangzhou). For other regions, specify the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
System.out.println("ready to list machinegroup");
// Query all machine groups in the project.
ListMachineGroupResponse response = client.ListMachineGroup(projectName);
for (String machineGroup : response.GetMachineGroups()) {
System.out.println(machineGroup.toString());
}
System.out.println(String.format("list project %s machinegroup success", projectName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}Expected result:
ready to list machinegroup
ali-test-machinegroup
ali-test-machinegroup2
list project ali-test-project machinegroup successQuery a specified machine group
The following code retrieves the details of a specified machine group:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.MachineGroup;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetMachineGroupResponse;
public class GetMachineGroup {
public static void main(String[] args) throws LogException {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. This example uses China (Hangzhou). For other regions, specify the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Enter the machine group name.
String machineGroupName = "ali-test-machinegroup";
System.out.println("ready to get machinegroup");
// Query the destination machine group.
GetMachineGroupResponse response = client.GetMachineGroup(projectName, machineGroupName);
MachineGroup machineGroup = response.GetMachineGroup();
// Print the information about the destination machine group.
System.out.println("The machinegroup name is:" + machineGroup.GetGroupName());
System.out.println("The machinegroup topic is:" + machineGroup.GetGroupTopic());
System.out.println("The machinegroup list is:" + machineGroup.GetMachineList());
System.out.println(String.format("get machinegroup from %s success", projectName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}Expected result:
ready to get machinegroup
The machinegroup name is:ali-test-machinegroup
The machinegroup topic is:new ide test
The machinegroup list is:[192.168.XX.XX]
get machinegroup from ali-test-project successDelete a machine group
The following code deletes a machine group from the specified project:
After you delete a machine group, the corresponding Logtail collection configuration is unbound from the machine group, and logs can no longer be collected.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
public class DeleteMachineGroup {
public static void main(String[] args) throws LogException {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. This example uses China (Hangzhou). For other regions, specify the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Enter the machine group name.
String machineGroupName = "ali-test-machinegroup";
System.out.println("ready to delete machinegroup");
// Delete the destination machine group.
client.DeleteMachineGroup(projectName, machineGroupName);
System.out.println(String.format("delete machinegroup from %s success", projectName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}Expected result:
ready to delete machinegroup
delete machinegroup from ali-test-project successReferences
In addition to its native SDK, SLS also supports the common Alibaba Cloud SDKs. For more information, see Log Service_SDKcenter-Alibaba CloudOpenAPIDeveloper Portal.
For more information about the machine group API operations, see the following topics: