All Products
Search
Document Center

Simple Log Service:Gunakan GetLogs untuk mengkueri log

Last Updated:Jun 03, 2026

Panggil API GetLogs untuk mengkueri log yang telah dikumpulkan menggunakan Simple Log Service Java SDK.

Prasyarat

Catatan penggunaan

  • Pada contoh ini, titik akhir publik Simple Log Service untuk wilayah China (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.

  • Panggil IsCompleted() pada objek respons SDK untuk memeriksa apakah set hasil sudah lengkap.

Parameter

Parameter permintaan

Name

Type

Required

Description

project

String

Yes

Nama proyek. Proyek dalam Simple Log Service digunakan untuk mengisolasi sumber daya pengguna yang berbeda dan mengontrol akses ke sumber daya tertentu. Lihat Mengelola proyek.

logstore

String

Yes

Nama logstore. Logstore dalam Simple Log Service digunakan untuk mengumpulkan, menyimpan, dan mengkueri log. Lihat Mengelola logstore.

from

int

Yes

Awal rentang waktu untuk dikueri. Nilainya berupa Stempel waktu UNIX.

Catatan
  • Waktu saat logstore menerima log. Bidang __tag__:__receive_time__ merupakan bidang tereservasi Simple Log Service.

  • Rentang waktu yang ditentukan oleh waktu mulai dan waktu akhir. Rentang waktu tersebut merupakan interval tertutup-kiri dan terbuka-kanan. Interval mencakup waktu mulai, tetapi tidak mencakup waktu akhir. Jika keduanya sama, interval tidak valid dan akan mengembalikan error.

  • Untuk memastikan seluruh data dapat dikueri, tentukan rentang waktu kueri yang akurat hingga menit. Jika Anda juga menentukan rentang waktu dalam pernyataan analitik, rentang waktu tersebut akan digunakan untuk kueri dan analisis.

  • Jika Anda ingin menentukan rentang waktu yang akurat hingga detik, Anda dapat menggunakan fungsi tanggal dan waktu untuk mengonversi format waktu. Contoh:

    • * | SELECT * FROM log WHERE from_unixtime(__time__) > from_unixtime(1664186624) AND from_unixtime(__time__) < now()

    • * | SELECT * FROM log WHERE __time__ > to_unixtime(date_parse('2022-10-19 15:46:05', '%Y-%m-%d %H:%i:%s')) AND __time__ < to_unixtime(now())

to

int

Yes

Akhir rentang waktu untuk dikueri. Nilainya berupa Stempel waktu UNIX.

Catatan
  • Waktu saat logstore menerima log. Bidang __tag__:__receive_time__ merupakan bidang tereservasi Simple Log Service.

  • Rentang waktu yang ditentukan oleh waktu mulai dan waktu akhir. Rentang waktu tersebut merupakan interval tertutup-kiri dan terbuka-kanan. Interval mencakup waktu mulai, tetapi tidak mencakup waktu akhir. Jika keduanya sama, interval tidak valid dan akan mengembalikan error.

  • Untuk memastikan seluruh data dapat dikueri, tentukan rentang waktu kueri yang akurat hingga menit. Jika Anda juga menentukan rentang waktu dalam pernyataan analitik, rentang waktu tersebut akan digunakan untuk kueri dan analisis.

  • Jika Anda ingin menentukan rentang waktu yang akurat hingga detik, Anda dapat menggunakan fungsi tanggal dan waktu untuk mengonversi format waktu. Contoh:

    • * | SELECT * FROM log WHERE from_unixtime(__time__) > from_unixtime(1664186624) AND from_unixtime(__time__) < now()

    • * | SELECT * FROM log WHERE __time__ > to_unixtime(date_parse('2022-10-19 15:46:05', '%Y-%m-%d %H:%i:%s')) AND __time__ < to_unixtime(now())

topic

String

No

Topik log. Nilai default adalah string kosong. Untuk informasi selengkapnya, lihat Topik log.

query

String

No

Pernyataan pencarian atau analitik. Ikhtisar Kueri dan Analisis. Untuk menggunakan SQL Eksklusif, tambahkan set session parallel_sql=true; ke pernyataan analitik. Contoh: * | set session parallel_sql=true; select count(*) as pv. Kesalahan umum dalam mengkueri dan menganalisis log.

Catatan

Jika query berisi pernyataan analitik (SQL), parameter line dan offset tidak berlaku. Atur nilainya menjadi 0 dan gunakan klausa LIMIT untuk paging. Melakukan paginasi pada hasil kueri dan analisis.

line

int

No

Hanya berlaku untuk pernyataan pencarian. Jumlah maksimum log yang dikembalikan. Rentang: 0 hingga 100. Default: 100.

offset

int

No

Hanya berlaku untuk pernyataan pencarian. Baris awal untuk kueri. Default: 0.

reverse

boolean

No

Apakah log dikembalikan dalam urutan stempel waktu menurun. Presisi: tingkat menit.

  • true: Urutan menurun (terbaru lebih dulu).

  • false (default): Urutan naik (terlama lebih dulu).

Penting
  • reverse hanya berlaku untuk pernyataan pencarian. Parameter ini mengontrol urutan pengurutan log yang dikembalikan.

  • Untuk pernyataan kueri dan analisis, reverse tidak berlaku. Gunakan klausa order by dalam SQL sebagai gantinya.

powerSql

boolean

No

Apakah akan menggunakan Dedicated SQL. Kueri dan analisis berkinerja tinggi serta akurat (Dedicated SQL).

  • true: Gunakan Dedicated SQL.

  • false (default): Gunakan Standard SQL.

Anda juga dapat mengaktifkan Dedicated SQL dengan menambahkan konfigurasi ke parameter query.

scan

boolean

No

Apakah akan menggunakan mode scan untuk kueri.

forward

boolean

No

Hanya untuk kueri scan. true: mengambil halaman berikutnya. false: mengambil halaman sebelumnya.

Parameter respons

GetLogs.

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.66

Contoh kueri dan analisis

Contoh Java berikut menunjukkan pola kueri umum.

Penting

Saat memanggil GetLogs dengan Simple Log Service Java SDK:

  • Jika query hanya berisi pernyataan pencarian (misalnya, request_method:POST), parameter line mengontrol jumlah log yang dikembalikan (maksimal 100). Untuk mengembalikan lebih dari 100 log, gunakan klausa LIMIT dalam SQL. Klausa LIMIT.

  • Jika query berisi pernyataan kueri dan analisis (misalnya, request_method:POST | SELECT host, COUNT(*) AS pv GROUP BY host LIMIT 5), parameter line diabaikan. Gunakan klausa LIMIT dalam SQL untuk mengontrol jumlah baris. Klausa LIMIT.

Sintaks dasar.

Contoh 1: Mengkueri log menggunakan kata kunci

Mengkueri log berdasarkan kata kunci path-0/file-5. Parameter line membatasi hasil menjadi 3 log.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;

public class GetLogsTest {

    public static void main(String[] args) throws LogException {
        // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String project = "your-project-name";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti nilai ini dengan titik akhir yang sebenarnya.
        String host = "cn-hangzhou.log.aliyuncs.com";
        // Masukkan nama Logstore.
        String logStore = "your-logstore-name";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        // Jalankan kueri di Logstore yang ditentukan.
        try {
            // Gunakan kata kunci "path-0/file-5" untuk mengkueri log.
            String query = "path-0/file-5";

            int from = 1754449503;
            int to = 1754449510;

            // Pada contoh ini, parameter query digunakan untuk menetapkan pernyataan kueri. Parameter line digunakan untuk mengontrol jumlah log yang dikembalikan. Nilainya adalah 3, dan nilai maksimumnya adalah 100.
            GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
            GetLogsResponse logsResponse = client.GetLogs(request);
            System.out.println("-------------Query is started.-------------");
            System.out.println("Returned query result count :" + logsResponse.GetCount());
            System.out.println("from time is :" + from);
            System.out.println("to time is :" + to);
            for (QueriedLog log : logsResponse.getLogs()) {
                LogItem item = log.GetLogItem();
                System.out.println("log time : " + item.mLogTime);
                System.out.println("Jsonstring : " + item.ToJsonString());
            }
            System.out.println("-------------Query is finished.-------------");

        } 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;
        }
    }
}

Respons:

-------------Query is started.-------------
Returned query result count :3
from time is :1644573549
to time is :1644573849
log time : 1644573808
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
log time : 1644573808
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
log time : 1644573788
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
-------------Query is finished.-------------

Process finished with exit code 0

Contoh 2: Mengkueri log dengan menentukan bidang

Mengkueri log di mana metode permintaan adalah POST. Parameter line membatasi hasil menjadi 3 log.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;

public class GetLogsTest {

    public static void main(String[] args) throws LogException {
        // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String project = "your-project-name";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti nilai ini dengan titik akhir yang sebenarnya.
        String host = "cn-hangzhou.log.aliyuncs.com";
        // Masukkan nama Logstore.
        String logStore = "your-logstore-name";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        // Jalankan kueri di Logstore yang ditentukan.
        try {
            // Kueri log di mana metode permintaan adalah POST.
            String query = "request_method:POST";
            // Tetapkan rentang waktu untuk kueri.
            int from = 1754449503;
            int to = 1754449510;

            // Pada contoh ini, parameter query digunakan untuk menetapkan pernyataan kueri. Parameter line digunakan untuk mengontrol jumlah log yang dikembalikan. Nilainya adalah 3, dan nilai maksimumnya adalah 100.
            GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
            GetLogsResponse logsResponse = client.GetLogs(request);
            System.out.println("-------------Query is started.-------------");
            System.out.println("Returned query result count :" + logsResponse.GetCount());
            System.out.println("from time is :" + from);
            System.out.println("to time is :" + to);
            for (QueriedLog log : logsResponse.getLogs()) {
                LogItem item = log.GetLogItem();
                System.out.println("log time : " + item.mLogTime);
                System.out.println("Jsonstring : " + item.ToJsonString());
            }
            System.out.println("-------------Query is finished.-------------");

        } 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;
        }
    }
}

Respons:

-------------Query is started.-------------
Returned query result count :3
from time is :1644574151
to time is :1644574451
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"3604","request_method":"POST"...}
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"3369","request_method":"POST"...}
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"12714","request_method":"POST"...}
-------------Query is finished.-------------

Process finished with exit code 0

Contoh 3: Menganalisis log menggunakan pernyataan SQL

Mengkueri log di mana metode permintaan adalah POST dan menghitung jumlah tampilan halaman (PVs).

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;

public class GetLogsTest {

    public static void main(String[] args) throws LogException {
        // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String project = "your-project-name";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti nilai ini dengan titik akhir yang sebenarnya.
        String host = "cn-hangzhou.log.aliyuncs.com";
        // Masukkan nama Logstore.
        String logStore = "your-logstore-name";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        // Jalankan kueri di Logstore yang ditentukan.
        try {
            // Kueri log di mana metode permintaan adalah POST dan hitung jumlah PV untuk permintaan POST tersebut.
            String query = "request_method:POST|select COUNT(*) as pv";
            // Tetapkan rentang waktu untuk kueri.
            int from = 1754449503;
            int to = 1754449510;

            // Pada contoh ini, parameter query diatur ke pernyataan kueri dan analisis. Parameter line tidak berlaku.
            GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
            GetLogsResponse logsResponse = client.GetLogs(request);
            System.out.println("-------------Query is started.-------------");
            System.out.println("Returned query result count :" + logsResponse.GetCount());
            System.out.println("from time is :" + from);
            System.out.println("to time is :" + to);
            for (QueriedLog log : logsResponse.getLogs()) {
                LogItem item = log.GetLogItem();
                System.out.println("log time : " + item.mLogTime);
                System.out.println("Jsonstring : " + item.ToJsonString());
            }
            System.out.println("-------------Query is finished.-------------");

        } 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;
        }
    }
}

Respons:

-------------Query is started.-------------
Returned query result count :1
from time is :1644574354
to time is :1644574654
log time : 1644574354
Jsonstring : {"pv":"162","logtime":1644574354}
-------------Query is finished.-------------

Process finished with exit code 0

Contoh 4: Menganalisis log menggunakan klausa GROUP BY

Mengkueri log di mana metode permintaan adalah POST dan mengelompokkan hasil berdasarkan host.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;

public class GetLogsTest {

    public static void main(String[] args) throws LogException {
        // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String project = "your-project-name";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti nilai ini dengan titik akhir yang sebenarnya.
        String host = "cn-hangzhou.log.aliyuncs.com";
        // Masukkan nama Logstore.
        String logStore = "your-logstore-name";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        // Jalankan kueri di Logstore yang ditentukan.
        try {
            // Kueri log di mana metode permintaan adalah POST dan kelompokkan hasil berdasarkan host.
            // Gunakan klausa LIMIT dalam sintaks SQL untuk membatasi jumlah baris yang dikembalikan menjadi 5.
            String query = "request_method:POST|select host, COUNT(*) as pv group by host limit 5";
            // Tetapkan rentang waktu untuk kueri.
            int from = 1754449503;
            int to = 1754449510;

            // Pada contoh ini, parameter query diatur ke pernyataan kueri dan analisis. Parameter line tidak berlaku. Jumlah baris yang dikembalikan ditentukan oleh klausa LIMIT dalam kueri.
            GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
            GetLogsResponse logsResponse = client.GetLogs(request);
            System.out.println("-------------Query is started.-------------");
            System.out.println("Returned query result count :" + logsResponse.GetCount());
            System.out.println("from time is :" + from);
            System.out.println("to time is :" + to);
            for (QueriedLog log : logsResponse.getLogs()) {
                LogItem item = log.GetLogItem();
                System.out.println("log time : " + item.mLogTime);
                System.out.println("Jsonstring : " + item.ToJsonString());
            }
            System.out.println("-------------Query is finished.-------------");

        } 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;
        }
    }
}

Respons:

-------------Query is started.-------------
Returned query result count :5
from time is :1644574445
to time is :1644574745
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example1.com","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.org","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.net","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.edu","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.aliyundoc.com","logtime":1644574445}
-------------Query is finished.-------------

Process finished with exit code 0

Contoh 5: Menganalisis log menggunakan klausa GROUP BY (200 log dikembalikan)

Mengkueri log di mana metode permintaan adalah POST, mengelompokkan berdasarkan host, dan mengembalikan hingga 200 baris menggunakan klausa LIMIT.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;

public class GetLogsTest {

    public static void main(String[] args) throws LogException {
        // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String project = "your-project-name";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti nilai ini dengan titik akhir yang sebenarnya.
        String host = "cn-hangzhou.log.aliyuncs.com";
        // Masukkan nama Logstore.
        String logStore = "your-logstore-name";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        // Jalankan kueri di Logstore yang ditentukan.
        try {
            // Kueri log di mana metode permintaan adalah POST dan kelompokkan hasil berdasarkan host.
            // Gunakan klausa LIMIT dalam sintaks SQL untuk mengontrol jumlah baris yang dikembalikan.
            String query = "request_method:POST|select host, COUNT(*) as pv group by host limit 0,200";
            // Tetapkan rentang waktu untuk kueri.
            int from = 1754449503;
            int to = 1754449510;

            // Pada contoh ini, parameter query diatur ke pernyataan kueri dan analisis. Parameter line tidak berlaku. Jumlah baris yang dikembalikan ditentukan oleh klausa LIMIT dalam kueri.
            GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
            GetLogsResponse logsResponse = client.GetLogs(request);
            System.out.println("-------------Query is started.-------------");
            System.out.println("Returned query result count :" + logsResponse.GetCount());
            System.out.println("from time is :" + from);
            System.out.println("to time is :" + to);
            for (QueriedLog log : logsResponse.getLogs()) {
                LogItem item = log.GetLogItem();
                System.out.println("log time : " + item.mLogTime);
                System.out.println("Jsonstring : " + item.ToJsonString());
            }
            System.out.println("-------------Query is finished.-------------");

        } 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;
        }
    }
}

Respons:

-------------Query is started.-------------
Returned query result count :200
from time is :1644574445
to time is :1644574745
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example1.com","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.org","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.net","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.edu","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.aliyundoc.com","logtime":1644574445}
......
-------------Query is finished.-------------

Process finished with exit code 0

Contoh 6: Mengkueri jumlah total log dalam satu jam sebelumnya menggunakan pernyataan SQL

Gunakan *|select count(*) as count untuk mendapatkan jumlah total log dari jam terakhir.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;

import java.util.Date;

public class GetLogsTest {

    public static void main(String[] args) throws LogException {
        // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String project = "your-project-name";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti nilai ini dengan titik akhir yang sebenarnya.
        String host = "cn-hangzhou.log.aliyuncs.com";
        // Masukkan nama Logstore.
        String logStore = "your-logstore-name";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        // Jalankan kueri di Logstore yang ditentukan.
        try {
            // Kueri jumlah total log.
            String query = "*|select count(*) as count";
            // Tetapkan rentang waktu kueri menjadi 1 jam (3.600 detik).
            int from = (int) (new Date().getTime() / 1000 - 3600);
            int to = (int) (new Date().getTime() / 1000);
            int offset = 0;
            int line = 200;
            // Pada contoh ini, pernyataan SQL dalam parameter query digunakan untuk mengkueri jumlah total log dalam rentang waktu yang ditentukan. Parameter line dan offset tidak berlaku untuk kueri SQL.
            GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query,line,offset,true);
            GetLogsResponse logsResponse = client.GetLogs(request);
            System.out.println("-------------Query is started.-------------");
            System.out.println("Returned query result count :" + logsResponse.GetCount());
            System.out.println("from time is :" + from);
            System.out.println("to time is :" + to);
            for (QueriedLog log : logsResponse.getLogs()) {
                LogItem item = log.GetLogItem();
                System.out.println("log time : " + item.mLogTime);
                System.out.println("Jsonstring : " + item.ToJsonString());
            }
            System.out.println("-------------Query is finished.-------------");

        } 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;
        }
    }
}

Jumlah total log dalam satu jam sebelumnya adalah 19.051. Respons:

from time is :1675041679
to time is :1675045279
Returned sql result count :1
Jsonstring : {"count":"19051","logtime":1675041679}
-------------Query is finished.-------------

Referensi