Topik ini menjelaskan cara memanggil operasi GetHistograms dari SDK Simple Log Service untuk Java guna mengkueri distribusi log dalam rentang waktu tertentu dan menyediakan contoh kode.
Prasyarat
Simple Log Service telah diaktifkan.
SDK Simple Log Service untuk Python telah diinisialisasi.
SDK Simple Log Service untuk Java telah diinstal. Untuk informasi selengkapnya, lihat Instal SDK Java.
Log telah ditulis ke Logstore, dan pengindeksan telah diaktifkan untuk Logstore tersebut. Untuk informasi selengkapnya, lihat Contoh kode untuk membuat Logstore dan Buat indeks.
Perhatian
Pada contoh ini, titik akhir publik Simple Log Service untuk wilayah Tiongkok (Hangzhou) digunakan. Titik akhir: https://cn-hangzhou.log.aliyuncs.com.
Jika Anda ingin mengakses Simple Log Service dari layanan Alibaba Cloud lain 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 selengkapnya mengenai wilayah dan titik akhir Simple Log Service yang didukung, lihat Titik Akhir.
Log mentah
body_bytes_sent:1750
host:www.example.com
http_referer:www.example.com
http_user_agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; it-it) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
http_x_forwarded_for:203.0.XX.XX
remote_addr:203.0.XX.XX
remote_user:p288
request_length:13741
request_method:GET
request_time:71
request_uri:/request/path-1/file-1
http_code:200
time_local:11/Aug/2021:06:52:27
upstream_response_time:0.66Contoh kode
Kode contoh berikut menunjukkan cara mengkueri distribusi permintaan akses dari 47.100.XX.XX dalam rentang satu jam:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Histogram;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetHistogramsResponse;
public class GetHistograms {
public static void main(String[] args) throws LogException {
// Pada contoh ini, ID AccessKey dan Rahasia AccessKey diambil 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. Contoh ini menggunakan titik akhir wilayah Tiongkok (Hangzhou). Ganti dengan titik akhir wilayah Anda.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Buat client Simple Log Service.
Client client = new Client(host, accessId, accessKey);
try {
// Nama Logstore.
String logstoreName = "ali-test-logstore";
// Kueri jumlah permintaan akses dari 47.100.XX.XX dalam satu jam.
String query = "remote_addr:47.100.XX.XX";
System.out.println(String.format("ready to get histograms from %s",logstoreName));
// fromTime dan toTime menentukan rentang waktu untuk mengkueri log. Nilainya berupa stempel waktu UNIX. Rentang waktu adalah satu jam.
int fromTime = (int) (System.currentTimeMillis()/1000 - 3600);
int toTime = fromTime + 3600;
GetHistogramsResponse response = client.GetHistograms(projectName,logstoreName,fromTime,toTime,"",query);
for (Histogram histogram : response.GetHistograms()) {
// Karena bisa terdapat banyak interval, hanya interval yang berisi log yang dicetak.
if (0 < histogram.GetCount()){
// Jumlah log.
System.out.println("log number is :" + histogram.GetCount());
// Waktu mulai distribusi log.
System.out.println("from time is :" + histogram.GetFrom());
// Waktu akhir distribusi log.
System.out.println("to time is :" + histogram.GetTo());
// Menunjukkan apakah hasil pada interval waktu ini lengkap.
System.out.println("is completed :" + histogram.IsCompleted());
}
}
System.out.println(String.format("get histograms from %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;
}
}
}Hasil yang diharapkan adalah hasil kueri lengkap yang menunjukkan 10 kali percobaan akses dari 47.100.XX.XX dalam rentang satu jam.
ready to get histograms from nginx-moni
log number is :10
from time is :1667875920
to time is :1667879520
is completed :true
get histograms from nginx-moni successReferensi
Selain SDK native-nya, SLS juga mendukung SDK Alibaba Cloud umum. Untuk informasi selengkapnya, lihat Simple Log Service_SDK Center_Alibaba Cloud OpenAPI Explorer.
Untuk informasi selengkapnya mengenai operasi API, lihat Kueri distribusi log.