全部產品
Search
文件中心

Simple Log Service:使用GetHistograms查詢日誌分布數量

更新時間:Jun 30, 2024

本文通過程式碼範例介紹如何使用GetHistograms介面查詢日誌在某時間區間中的分布數量。

前提條件

  • 已建立RAM使用者並完成授權。具體操作,請參見建立RAM使用者並完成授權

  • 已配置環境變數ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。具體操作,請參見在Linux、macOS和Windows系統配置環境變數

    重要
    • 阿里雲帳號的AccessKey擁有所有API的存取權限,建議您使用RAM使用者的AccessKey進行API訪問或日常營運。

    • 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。

  • 已安裝Log ServiceJava SDK。具體操作,請參見安裝Java SDK

  • 已寫入日誌到Logstore並開啟索引。具體操作,請參見建立Logstore建立索引

注意事項

本樣本以華東1(杭州)的公網Endpoint為例,其公網Endpoint為https://cn-hangzhou.log.aliyuncs.com。如果您通過與Project同地區的其他阿里雲產品訪問Log Service,請使用內網Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com。關於Log Service支援的地區與Endpoint的對應關係,請參見服務入口

原始日誌範例

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

範例程式碼

以下代碼用於查詢一小時內來自47.100.XX.XX的訪問次數。

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 {
        // 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 輸入Project名稱。
        String projectName = "ali-test-project";
        // Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        Client client = new Client(host, accessId, accessKey);

        try {
            // 輸入Logstore名稱。
            String logstoreName = "ali-test-logstore";

            // 查詢一小時內來自47.100.XX.XX的訪問次數。
            String query = "remote_addr:47.100.XX.XX";
            System.out.println(String.format("ready to get histograms from %s",logstoreName));

            // fromTime和toTime表示查詢日誌的時間範圍,Unix時間戳記格式。查詢區間為一個小時。
            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()) {
                // 由於區間較多,只輸出有日誌分布的區間。
                if (0 < histogram.GetCount()){
                    // 輸出日誌數量。
                    System.out.println("log number is :" + histogram.GetCount());
                    // 日誌數量分布的開始時間。
                    System.out.println("from time is :" + histogram.GetFrom());
                    // 日誌數量分布的結束時間。
                    System.out.println("to time is :" + histogram.GetTo());
                    // 該時間區間內的結果是否完整。
                    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;
        }
    }
}

預期結果如下。該查詢結果說明一小時內來自47.100.XX.XX的訪問次數為10,查詢結果完整。

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 success

相關文檔

  • 在調用API介面過程中,若服務端返回結果中包含錯誤資訊,則表示調用API介面失敗。您可以參考API錯誤碼對照表尋找對應的解決方案。更多資訊,請參見API錯誤處理對照表

  • 阿里雲OpenAPI開發人員門戶提供調試、SDK、樣本和配套文檔。通過OpenAPI,您無需手動封裝請求和簽名操作,就可以快速對Log ServiceAPI進行調試。更多資訊,請參見OpenAPI開發人員門戶

  • 為滿足越來越多的自動化Log Service配置需求,Log Service提供命令列工具CLI(Command Line Interface)。更多資訊,請參見Log Service命令列工具CLI

  • 關於API介面說明,請參見GetHistograms

  • 更多範例程式碼,請參見Aliyun Log Java SDK on GitHub