Logstore dalam Simple Log Service adalah unit yang digunakan untuk mengumpulkan, menyimpan, dan menanyakan data. Setiap Logstore termasuk ke dalam sebuah Proyek. Anda dapat membuat beberapa Logstore dalam satu Proyek. Topik ini menjelaskan cara membuat, memodifikasi, menanyakan, dan menghapus Logstore menggunakan Simple Log Service SDK untuk Java serta memberikan contoh kode.
Prasyarat
Pengguna Resource Access Management (RAM) telah dibuat, dan izin yang diperlukan telah diberikan kepada pengguna RAM. Untuk informasi lebih lanjut, lihat Buat Pengguna RAM dan Berikan Izin kepada Pengguna RAM.
Variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID dan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah dikonfigurasi. Untuk informasi lebih lanjut, lihat Konfigurasikan Variabel Lingkungan di Linux, macOS, dan Windows.
PentingAccessKey pasangan dari akun Alibaba Cloud memiliki izin pada semua Operasi API. Kami merekomendasikan agar Anda menggunakan pasangan AccessKey dari pengguna RAM untuk memanggil Operasi API atau melakukan pemeliharaan rutin O&M.
Kami menyarankan Anda untuk tidak menyimpan ID AccessKey atau Rahasia AccessKey dalam kode proyek Anda. Jika tidak, pasangan AccessKey mungkin bocor, dan keamanan semua sumber daya dalam akun Anda mungkin terganggu.
Simple Log Service SDK untuk Java telah diinstal. Untuk informasi lebih lanjut, lihat Instal Simple Log Service SDK untuk Java.
Catatan Penggunaan
Dalam contoh ini, Titik akhir publik Simple Log Service untuk wilayah China (Hangzhou) digunakan, yaitu https://cn-hangzhou.log.aliyuncs.com. Jika Anda ingin mengakses Simple Log Service dengan menggunakan layanan Alibaba Cloud lainnya yang berada di wilayah yang sama dengan proyek Anda, Anda dapat menggunakan Titik akhir internal Simple Log Service, yaitu https://cn-hangzhou-intranet.log.aliyuncs.com. Untuk informasi lebih lanjut tentang wilayah dan Titik akhir yang didukung oleh Simple Log Service, lihat Titik Akhir.
Contoh kode yang digunakan untuk membuat Logstore
Berikut ini adalah contoh kode yang menunjukkan cara membuat Logstore bernama 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 {
// Dalam contoh ini, ID AccessKey dan Rahasia AccessKey diperoleh dari variabel lingkungan.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Nama proyek.
String projectName = "ali-test-project";
// Titik akhir Simple Log Service. Dalam contoh ini, Titik akhir Simple Log Service untuk wilayah China (Hangzhou) digunakan. Ganti nilai parameter dengan Titik akhir sebenarnya.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Buat klien Simple Log Service.
Client client = new Client(host, accessId, accessKey);
try {
// Nama Logstore.
String logstoreName = "ali-test-logstore";
System.out.println("siap untuk membuat logstore");
// Buat Logstore dengan periode retensi data 60 hari dan jumlah shard 2, serta aktifkan fitur pelacakan web.
LogStore logStore = new LogStore(logstoreName, 60, 2, true);
// Aktifkan fitur sharding otomatis.
logStore.setmAutoSplit(true);
// Tetapkan jumlah maksimum shard yang dapat dibagi secara otomatis menjadi 64.
logStore.setmMaxSplitShard(64);
// Rekam alamat IP publik.
logStore.setAppendMeta(true);
// Tetapkan periode retensi data tier penyimpanan panas dalam Logstore menjadi 30 hari.
logStore.setHotTTL(30);
// Tetapkan tipe Logstore menjadi Standard.
logStore.setMode("standard");
CreateLogStoreRequest request = new CreateLogStoreRequest(projectName, logStore);
// Buat Logstore.
client.CreateLogStore(request);
System.out.println(String.format("buat logstore %s berhasil", logstoreName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("kode kesalahan :" + e.GetErrorCode());
System.out.println("pesan kesalahan :" + e.GetErrorMessage());
throw e;
}
}
}Berikut ini adalah respons yang menunjukkan keluaran yang diharapkan:
siap untuk membuat logstore
buat logstore ali-test-logstore berhasilContoh kode yang digunakan untuk memodifikasi Logstore
Berikut ini adalah contoh kode yang menunjukkan cara memodifikasi konfigurasi Logstore 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 {
// Dalam contoh ini, ID AccessKey dan Rahasia AccessKey diperoleh dari variabel lingkungan.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Nama proyek.
String projectName = "ali-test-project";
// Titik akhir Simple Log Service. Dalam contoh ini, Titik akhir Simple Log Service untuk wilayah China (Hangzhou) digunakan. Ganti nilai parameter dengan Titik akhir sebenarnya.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Buat klien Simple Log Service.
Client client = new Client(host, accessId, accessKey);
try {
// Nama Logstore.
String logstoreName = "ali-test-logstore";
System.out.println("siap untuk memperbarui logstore");
// Ubah periode retensi data tier penyimpanan panas dalam Logstore menjadi 45 hari.
LogStore logStore = new LogStore(logstoreName, 60, 2, true);
logStore.setHotTTL(45);
UpdateLogStoreRequest request = new UpdateLogStoreRequest(projectName, logStore);
// Perbarui Logstore.
client.UpdateLogStore(request);
System.out.println(String.format("memperbarui logstore %s berhasil", logstoreName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("kode kesalahan :" + e.GetErrorCode());
System.out.println("pesan kesalahan :" + e.GetErrorMessage());
throw e;
}
}
}Berikut ini adalah respons yang menunjukkan keluaran yang diharapkan:
siap untuk memperbarui logstore
memperbarui logstore ali-test-logstore berhasilContoh kode yang digunakan untuk menanyakan semua Logstore
Berikut ini adalah contoh kode yang menunjukkan cara menanyakan semua Logstore dalam proyek tertentu:
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 {
// Dalam contoh ini, ID AccessKey dan Rahasia AccessKey diperoleh dari variabel lingkungan.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Nama proyek.
String projectName = "ali-test-project";
// Titik akhir Simple Log Service. Dalam contoh ini, Titik akhir Simple Log Service untuk wilayah China (Hangzhou) digunakan. Ganti nilai parameter dengan Titik akhir sebenarnya.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Buat klien Simple Log Service.
Client client = new Client(host, accessId, accessKey);
try {
System.out.println("siap untuk mencantumkan logstore");
// Tanyakan 10 Logstore.
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("kode kesalahan :" + e.GetErrorCode());
System.out.println("pesan kesalahan :" + e.GetErrorMessage());
throw e;
}
}
}Berikut ini adalah respons yang menunjukkan keluaran yang diharapkan:
siap untuk mencantumkan logstore
ali-test-logstoreContoh kode yang digunakan untuk menanyakan Logstore tertentu
Berikut ini adalah contoh kode yang menunjukkan cara menanyakan informasi tentang Logstore tertentu:
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 {
// Dalam contoh ini, ID AccessKey dan Rahasia AccessKey diperoleh dari variabel lingkungan.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Nama proyek.
String projectName = "ali-test-project";
// Titik akhir Simple Log Service. Dalam contoh ini, Titik akhir Simple Log Service untuk wilayah China (Hangzhou) digunakan. Ganti nilai parameter dengan Titik akhir sebenarnya.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Buat klien Simple Log Service.
Client client = new Client(host, accessId, accessKey);
try {
// Nama Logstore.
String logStoreName = "ali-test-logstore";
System.out.println("siap untuk mendapatkan logstore");
// Tanyakan Logstore yang ditentukan.
GetLogStoreRequest request = new GetLogStoreRequest(projectName, logStoreName);
GetLogStoreResponse response = client.GetLogStore(request);
System.out.println("Nama Logstore adalah : " + response.GetLogStore().GetLogStoreName());
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("kode kesalahan :" + e.GetErrorCode());
System.out.println("pesan kesalahan :" + e.GetErrorMessage());
throw e;
}
}
}Berikut ini adalah respons yang menunjukkan keluaran yang diharapkan:
siap untuk mendapatkan logstore
Nama Logstore adalah : ali-test-logstoreContoh kode yang digunakan untuk menghapus Logstore
Berikut ini adalah contoh kode yang menunjukkan cara menghapus Logstore ali-test-logstore.
Setelah Anda menghapus Logstore, semua data dalam Logstore akan dihapus dan tidak dapat dipulihkan. Lanjutkan dengan hati-hati.
Sebelum Anda dapat menghapus Logstore, Anda harus menghapus semua konfigurasi Logtail yang terkait dengan Logstore tersebut.
Jika fitur pengiriman log diaktifkan untuk Logstore, kami sarankan Anda berhenti menulis data ke Logstore dan pastikan bahwa semua data dalam Logstore telah dikirim sebelum Anda menghapus Logstore.
Jika Anda tidak berwenang untuk menghapus Logstore dengan akun Alibaba Cloud Anda, ajukan Tiket untuk menghapus 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 {
// Dalam contoh ini, ID AccessKey dan Rahasia AccessKey diperoleh dari variabel lingkungan.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Nama proyek.
String projectName = "ali-test-project";
// Titik akhir Simple Log Service. Dalam contoh ini, Titik akhir Simple Log Service untuk wilayah China (Hangzhou) digunakan. Ganti nilai parameter dengan Titik akhir sebenarnya.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Membuat klien Simple Log Service.
Client client = new Client(host, accessId, accessKey);
try {
// Nama Logstore.
String logStoreName = "ali-test-logstore";
System.out.println("siap untuk menghapus logstore");
// Hapus Logstore yang ditentukan.
client.DeleteLogStore(projectName,logStoreName);
System.out.println(String.format("hapus logstore %s berhasil", logStoreName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("kode kesalahan :" + e.GetErrorCode());
System.out.println("pesan kesalahan :" + e.GetErrorMessage());
throw e;
}
}
}Berikut ini adalah respons yang menunjukkan keluaran yang diharapkan:
siap untuk menghapus logstore
hapus logstore ali-test-logstore berhasilReferensi
Alibaba Cloud OpenAPI Explorer menyediakan kemampuan debugging, SDK, contoh, dan dokumen terkait. Anda dapat menggunakan OpenAPI Explorer untuk men-debug operasi API Simple Log Service tanpa perlu secara manual mengenkapsulasi atau menandatangani permintaan. Untuk informasi lebih lanjut, kunjungi OpenAPI Portal.
Simple Log Service menyediakan Antarmuka baris perintah (CLI) untuk memenuhi persyaratan konfigurasi otomatis dalam Simple Log Service. Untuk informasi lebih lanjut, lihat Simple Log Service CLI.
Untuk informasi tentang operasi API terkait Logstore, lihat topik-topik berikut:
Untuk informasi lebih lanjut tentang contoh kode, lihat Alibaba Cloud Simple Log Service SDK untuk Java di GitHub.