Un logstore es la unidad básica de recolección, almacenamiento y consulta en Simple Log Service. Cada logstore pertenece a un proyecto, y cada proyecto puede contener varios logstores. Con el SDK de Java, usted puede crear, actualizar, consultar y eliminar logstores.
Requisitos previos
Simple Log Service está activado.
El SDK de Simple Log Service para Python está initialized.
El SDK de Simple Log Service para Java está instalado. Para obtener más información, consulte Install the Java SDK.
Precauciones
En este ejemplo se utiliza el endpoint público de Simple Log Service correspondiente a la región China (Hangzhou). Endpoint: https://cn-hangzhou.log.aliyuncs.com.
Si desea acceder a Simple Log Service desde otros servicios de Alibaba Cloud que residen en la misma región que su proyecto, puede utilizar el endpoint interno de Simple Log Service: https://cn-hangzhou-intranet.log.aliyuncs.com.
Para obtener más información sobre las regiones y endpoints compatibles con Simple Log Service, consulte Endpoint.
Crear un logstore
A continuación se muestra un ejemplo que crea un logstore llamado ali-test-logstore.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogStore;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.CreateLogStoreRequest;
public class CreateLogstore {
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");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with 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 {
// Specify the Logstore name.
String logstoreName = "ali-test-logstore";
System.out.println("ready to create logstore");
// Create a Logstore. Set the data retention period to 60 days and the number of shards to 2. Enable web tracking.
LogStore logStore = new LogStore(logstoreName, 60, 2, true);
// Enable auto split for shards.
logStore.setmAutoSplit(true);
// Set the maximum number of shards for auto split to 64.
logStore.setmMaxSplitShard(64);
// Enable the feature of recording the public IP address.
logStore.setAppendMeta(true);
// Set the storage period of data in the hot tier of the Logstore to 30 days.
logStore.setHotTTL(30);
// Set the Logstore type to standard.
logStore.setMode("standard");
CreateLogStoreRequest request = new CreateLogStoreRequest(projectName, logStore);
// Create the Logstore.
client.CreateLogStore(request);
System.out.println(String.format("create logstore %s success", logstoreName));
} 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;
}
}
}
Se devuelve el siguiente resultado:
ready to create logstore
create logstore ali-test-logstore success
Actualizar un logstore
El siguiente ejemplo actualiza la configuración de un logstore llamado ali-test-logstore.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogStore;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.UpdateLogStoreRequest;
public class UpdateLogstore {
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");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with 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 {
// Specify the Logstore name.
String logstoreName = "ali-test-logstore";
System.out.println("ready to update logstore");
// Update the storage period of data in the hot tier of the Logstore to 45 days.
LogStore logStore = new LogStore(logstoreName, 60, 2, true);
logStore.setHotTTL(45);
UpdateLogStoreRequest request = new UpdateLogStoreRequest(projectName, logStore);
// Update the Logstore.
client.UpdateLogStore(request);
System.out.println(String.format("update logstore %s success", logstoreName));
} 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;
}
}
}
Se devuelve el siguiente resultado:
ready to update logstore
update logstore ali-test-logstore success
Consultar todos los logstores
El siguiente ejemplo lista todos los logstores de un proyecto.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.ListLogStoresRequest;
import com.aliyun.openservices.log.response.ListLogStoresResponse;
public class ListLogstore {
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");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with 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 logstore");
// Query 10 Logstores.
ListLogStoresRequest request = new ListLogStoresRequest(projectName, 0, 10, "", "None");
ListLogStoresResponse response = client.ListLogStores(request);
for (String logStore : response.GetLogStores()) {
System.out.println(logStore.toString());
}
} 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;
}
}
}
Se devuelve el siguiente resultado:
ready to list logstore
ali-test-logstore
Consultar un logstore específico
El siguiente ejemplo consulta un logstore específico.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogStoreRequest;
import com.aliyun.openservices.log.response.GetLogStoreResponse;
public class GetLogstore {
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");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with 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 {
// Specify the Logstore name.
String logStoreName = "ali-test-logstore";
System.out.println("ready to get logstore");
// Query the specified Logstore.
GetLogStoreRequest request = new GetLogStoreRequest(projectName, logStoreName);
GetLogStoreResponse response = client.GetLogStore(request);
System.out.println("The Logstore name is : " + response.GetLogStore().GetLogStoreName());
} 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;
}
}
}
Se devuelve el siguiente resultado:
ready to get logstore
The Logstore name is : ali-test-logstore
Eliminar un logstore
El siguiente ejemplo elimina un logstore llamado ali-test-logstore.
Una vez eliminado un logstore, los datos que contiene se borran permanentemente y no se pueden recuperar. Proceda con precaución.
Antes de eliminar un logstore, elimine todas las configuraciones de Logtail asociadas a él.
Si LogShipper está habilitado para el logstore, detenga la escritura de nuevos datos antes de eliminarlo. Asegúrese de que todos los datos existentes en el logstore se hayan entregado correctamente.
Si utiliza una cuenta de Alibaba Cloud para eliminar un logstore y recibe un error de permisos insuficientes, envíe un ticket para solicitar la eliminación del logstore.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
public class DeleteLogstore {
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");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with 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 {
// Specify the Logstore name.
String logStoreName = "ali-test-logstore";
System.out.println("ready to delete logstore");
// Delete the specified Logstore.
client.DeleteLogStore(projectName,logStoreName);
System.out.println(String.format("delete logstore %s success", logStoreName));
} 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;
}
}
}
Se devuelve el siguiente resultado:
ready to delete logstore
delete logstore ali-test-logstore success
Referencias
Además de su SDK nativo, SLS también es compatible con los SDK comunes de Alibaba Cloud. Para obtener más información, consulte Simple Log Service_SDK Center_Alibaba Cloud OpenAPI Explorer.
-
Operaciones de API relacionadas con proyectos: