Logstore, Simple Log Service'de veri toplama, depolama ve sorgulama birimidir. Her Logstore bir projeye aittir ve her proje birden çok Logstore içerebilir. Java SDK'yı kullanarak Logstore oluşturabilir, güncelleyebilir, sorgulayabilir ve silebilirsiniz.
Önkoşullar
Simple Log Service etkinleştirilmiştir.
Python için Simple Log Service SDK başlatılmıştır.
Java için Simple Log Service SDK yüklenmiştir. Daha fazla bilgi için bkz. Java SDK'yı yükleme.
Dikkat edilmesi gerekenler
Bu örnekte Çin (Hangzhou) bölgesi için genel Simple Log Service uç noktası kullanılır. Uç nokta: https://cn-hangzhou.log.aliyuncs.com.
Projenizle aynı bölgede bulunan diğer Alibaba Cloud hizmetlerinden Simple Log Service'e erişmek istiyorsanız, dahili Simple Log Service uç noktasını kullanabilirsiniz. Dahili uç nokta: https://cn-hangzhou-intranet.log.aliyuncs.com.
Simple Log Service tarafından desteklenen bölgeler ve uç noktalar hakkında daha fazla bilgi için bkz. Uç nokta.
Logstore oluşturma
Aşağıdaki örnek, ali-test-logstore adlı bir Logstore oluşturur.
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;
}
}
}
Aşağıdaki çıktı döndürülür:
ready to create logstore
create logstore ali-test-logstore success
Logstore güncelleme
Aşağıdaki örnek, ali-test-logstore adlı bir Logstore'un yapılandırmasını günceller.
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;
}
}
}
Aşağıdaki çıktı döndürülür:
ready to update logstore
update logstore ali-test-logstore success
Tüm Logstore'ları listeleme
Aşağıdaki örnek, bir projedeki tüm Logstore'ları listeler.
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;
}
}
}
Aşağıdaki çıktı döndürülür:
ready to list logstore
ali-test-logstore
Belirli bir Logstore'u sorgulama
Aşağıdaki örnek belirli bir Logstore'u sorgular.
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;
}
}
}
Aşağıdaki çıktı döndürülür:
ready to get logstore
The Logstore name is : ali-test-logstore
Logstore silme
Aşağıdaki örnek, ali-test-logstore adlı bir Logstore'u siler.
Bir Logstore silindiğinde içindeki veriler kalıcı olarak silinir ve kurtarılamaz. İşlemi dikkatlice gerçekleştirin.
Bir Logstore'u silmeden önce bu Logstore ile ilişkili tüm Logtail yapılandırmalarını silin.
Logstore için LogShipper etkinleştirildiyse, Logstore'u silmeden önce yeni veri yazmayı durdurun. Logstore'daki mevcut tüm verilerin teslim edildiğinden emin olun.
Bir Alibaba Cloud hesabı kullanarak bir Logstore'u silmeye çalıştığınızda yetersiz izin hatası alırsanız, Logstore'u silmek için bir destek talebi gönderin.
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;
}
}
}
Aşağıdaki çıktı döndürülür:
ready to delete logstore
delete logstore ali-test-logstore success
İlgili belgeler
SLS, yerel SDK'sına ek olarak ortak Alibaba Cloud SDK'larını da destekler. Daha fazla bilgi için bkz. Simple Log Service_SDK Center_Alibaba Cloud OpenAPI Explorer.
-
Projeyle ilgili API işlemleri:
GitHub'daki Alibaba Cloud Simple Log Service SDK for Java projesi.