すべてのプロダクト
Search
ドキュメントセンター

Simple Log Service:Simple Log Service Java SDK を使用したプロジェクトの管理

最終更新日:Feb 28, 2026

Simple Log Service のプロジェクトは、Logstore、Metricstore、およびマシングループのリソース管理単位です。プロジェクトエンドポイントを使用して、Simple Log Service リソースにアクセスします。

このトピックでは、プロジェクトの作成、更新、照会、および削除を行うための Java SDK サンプルコードを提供します。

前提条件

重要

API 操作を呼び出すには、Alibaba Cloud アカウントではなく、RAM ユーザーの AccessKey ペアを使用します。AccessKey ID または AccessKey Secret をプロジェクトコードに保存しないでください。そうしないと、AccessKey ペアが漏洩し、アカウント内のすべてのリソースが侵害される可能性があります。

注意事項

以下の例では、中国 (杭州) リージョンのパブリック Simple Log Service エンドポイント https://cn-hangzhou.log.aliyuncs.com を使用します。

同じリージョン内の他の Alibaba Cloud サービスから Simple Log Service にアクセスするには、内部エンドポイント https://cn-hangzhou-intranet.log.aliyuncs.com を使用します。

すべてのサポート対象リージョンおよびエンドポイントについては、「エンドポイント」をご参照ください。

プロジェクトの作成

説明付きで ali-test-project という名前のプロジェクトを作成します。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.UpdateProjectRequest;

public class UpdateProject {
    public static void main(String[] args) throws LogException {
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        String projectName = "ali-test-project";
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        Client client = new Client(host, accessId, accessKey);

        try {
            String projectDescription = "これは新しいプロジェクトの説明です";
            UpdateProjectRequest request = new UpdateProjectRequest(projectName, projectDescription);
            client.updateProject(request);
            System.out.println(String.format("プロジェクト %s の更新に成功しました", projectName));
        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("エラーコード :" + e.GetErrorCode());
            System.out.println("エラーメッセージ :" + e.GetErrorMessage());
            throw e;
        }
    }
}

期待される出力:

create project ali-test-project success

プロジェクトの更新

UpdateProjectRequest を使用して、ali-test-project プロジェクトの説明を更新します。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.UpdateProjectRequest;

public class UpdateProject {
    public static void main(String[] args) throws LogException {
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        String projectName = "ali-test-project";
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        Client client = new Client(host, accessId, accessKey);

        try {
            String projectDescription = "This is the new project description";
            UpdateProjectRequest request = new UpdateProjectRequest(projectName, projectDescription);
            client.updateProject(request);
            System.out.println(String.format("update project %s success", projectName));
        } 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;
        }
    }
}

期待される出力:

update project ali-test-project success

すべてのプロジェクトの一覧表示

現在のリージョン内のすべてのプロジェクトを一覧表示し、各プロジェクト名を出力します。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Project;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.ListProjectResponse;

public class ListProject {
    public static void main(String[] args) throws LogException {
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        Client client = new Client(host, accessId, accessKey);

        try {
            ListProjectResponse response = client.ListProject();
            for (Project project : response.getProjects()) {
                System.out.println(project.getProjectName());
            }
        } 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;
        }
    }
}

期待される出力:

ali-test-project
local-ide

プロジェクトの照会

特定のプロジェクトの詳細 (説明、オーナー、リージョン、ステータスなど) を取得します。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetProjectResponse;

public class GetProject {
    public static void main(String[] args) throws LogException {
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        String projectName = "ali-test-project";
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        Client client = new Client(host, accessId, accessKey);

        try {
            GetProjectResponse response = client.GetProject(projectName);
            System.out.println("description: " + response.GetProjectDescription());
            System.out.println("owner: " + response.GetProjectOwner());
            System.out.println("region: " + response.GetProjectRegion());
            System.out.println("status: " + response.GetProjectStatus());
        } 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;
        }
    }
}

期待される出力:

description: project description
owner: Ownerid
region: cn-hangzhou
status: Normal

プロジェクト内のログの照会

Logstore に対して SQL クエリを実行し、指定された時間範囲内のログエントリをカウントします。

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

public class GetProjectLogs {
    public static void main(String[] args) throws LogException {
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        String projectName = "ali-test-project";
        String logstoreName = "ali-test-logstore";
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        Client client = new Client(host, accessId, accessKey);

        try {
            // Use __time__ to specify a UNIX timestamp range for the query
            String query = "SELECT COUNT(*) as pv FROM " + logstoreName + " where __time__ > 1646102500 and __time__ < 1646103400";
            GetLogsResponse response = client.GetProjectLogs(projectName, query);
            for (QueriedLog log : response.getLogs()) {
                System.out.println(log.GetLogItem().ToJsonString());
            }
        } 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;
        }
    }
}

期待される出力:

{"pv":"4615","logtime":1}

プロジェクトの削除

ali-test-project プロジェクトを削除します。呼び出しが成功すると、例外はスローされません。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;

public class DeleteProject {
    public static void main(String[] args) throws LogException {
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        String projectName = "ali-test-project";
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        Client client = new Client(host, accessId, accessKey);

        try {
            client.DeleteProject(projectName);
            System.out.println(String.format("delete project %s success", projectName));
        } 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;
        }
    }
}

期待される出力:

delete project ali-test-project success

参考資料