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

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

最終更新日:Jul 03, 2025

Simple Log Serviceのプロジェクトは、Logstore、Metricstore、マシングループなどのリソースを管理するために使用されるユニットです。 プロジェクトのエンドポイントを使用して、Simple Log Serviceのリソースにアクセスできます。 このトピックでは、Simple Log Service SDK for Javaを使用してプロジェクトを作成、変更、クエリ、および削除する方法について説明し、サンプルコードを提供します。

前提条件

  • Simple Log Serviceが有効化されています。 詳細については、次をご参照ください: Simple Log Serviceの有効化

  • RAM (Resource Access Management) ユーザーが作成され、必要な権限がRAMユーザーに付与されます。 詳細については、「RAMユーザーの作成とRAMユーザーへの権限付与」をご参照ください。

  • ALIBABA_CLOUD_ACCESS_KEY_IDおよびALIBABA_CLOUD_ACCESS_KEY_SECRET環境変数が設定されています。 詳細については、「環境変数の設定」をご参照ください。

    重要
    • Alibaba CloudアカウントのAccessKeyペアには、すべてのAPI操作に対する権限があります。 RAMユーザーのAccessKeyペアを使用して、API操作を呼び出したり、ルーチンのO&Mを実行したりすることを推奨します。

    • プロジェクトコードにAccessKey IDまたはAccessKey secretを保存しないことを推奨します。 そうしないと、AccessKeyペアが漏洩し、アカウント内のすべてのリソースのセキュリティが侵害される可能性があります。

  • Java開発環境がインストールされています。

    Simple Log Service SDK for Javaは、Javaランタイム環境 (JRE) 6.0以降をサポートしています。 java -versionコマンドを実行して、JREのバージョンを確認できます。 JREがインストールされていない場合は、Java公式サイトからインストールパッケージをダウンロードしてJREをインストールできます。

  • Simple Log Service SDK for Javaがインストールされています。 詳細については、「Simple Log Service SDK For Javaのインストール」をご参照ください。

使用上の注意

この例では、中国 (杭州) リージョンのパブリックSimple Log Serviceエンドポイントが使用されています。これは https://cn-hangzhou.log.aliyuncs.com です。 プロジェクトと同じリージョンにある他のAlibaba Cloudサービスを使用してSimple Log Serviceにアクセスする場合は、内部のSimple Log Serviceエンドポイント ( https://cn-hangzhou-intranet.log.aliyuncs.com ) を使用できます。 Simple Log Serviceのサポートされているリージョンとエンドポイントの詳細については、「エンドポイント」をご参照ください。

プロジェクトの作成に使用されるサンプルコード

次のサンプルコードは、ali-test-projectという名前のプロジェクトを作成する方法の例を示しています。

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

public class CreateProject {
    public static void main(String[] args) throws LogException {
        // Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Enter a project name. 
        String projectName = "ali-test-project";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client. 
        Client client = new Client(host, accessId, accessKey);
        // Create a project. 
        try {
            String projectDescription = "project description";
            System.out.println("ready to create project");
            client.CreateProject(projectName, projectDescription);
            System.out.println(String.format("create 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;
        }
    }
}

期待される結果

ready to create project
create project ali-test-project success

プロジェクトの変更に使用されるサンプルコード

次のサンプルコードは、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 {
        // Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Enter the project name. 
        String projectName = "ali-test-project";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client. 
        Client client = new Client(host, accessId, accessKey);
        // Modify the project. 
        try {
            String projectDescription = "This is the new project description";
            System.out.println("ready to update project");
            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;
        }
    }
}

期待される結果

ready to update project
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 {
        // Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Enter a project name. 
        String projectName = "ali-test-project";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client. 
        Client client = new Client(host, accessId, accessKey);
        // Query all projects. 
        try {
            System.out.println("ready to list project");
            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;
        }
    }
}

期待される結果

ready to list project
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 {
        // Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Enter the project name. 
        String projectName = "ali-test-project";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client. 
        Client client = new Client(host, accessId, accessKey);
        // Query the specified project. 
        try {
            System.out.println("ready to get project");
            GetProjectResponse response = client.GetProject(projectName);
            System.out.println(String.format("get project %s success",projectName));
            System.out.println("The Project description is:" + response.GetProjectDescription());
            System.out.println("The Project owner info is:" + response.GetProjectOwner());
            System.out.println("The Project region info is:" + response.GetProjectRegion());
            System.out.println("The Project status info is:" + 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;
        }
    }
}

期待される結果

ready to get project
get project ali-test-project success
The Project description is:project description
The Project owner is:Ownerid
The Project region is:cn-hangzhou
The Project status is:Normal

プロジェクト内のログのクエリに使用されるサンプルコード

次のサンプルコードは、プロジェクトのLogstore内のログ数を照会する方法の例を示しています。

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 {
        // Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Enter the project name. 
        String projectName = "ali-test-project";
        // Enter the Logstore name 
        String logstoreName = "ali-test-logstore";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client. 
        Client client = new Client(host, accessId, accessKey);
        // Use an SQL statement to query the number of logs in the specified Logstore of the specified project. 
        try {
            System.out.println("ready to get project logs");
            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;
        }
    }
}

期待される結果

ready to get project logs
{"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 {
        // Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Enter the project name. 
        String projectName = "ali-test-project";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client. 
        Client client = new Client(host, accessId, accessKey);
        // Delete the project. 
        try {
            System.out.println("ready to delete project");
            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;
        }
    }
}

期待される結果

プロジェクトを削除する

ready to delete project
delete project ali-test-project success

関連ドキュメント

  • APIを呼び出した後、Log Serviceによって返された応答にエラー情報が含まれている場合、呼び出しは失敗します。 API呼び出しが失敗したときに返されるエラーコードに基づいてエラーを処理できます。 詳細については、エラーコードをご参照ください。

  • Alibaba Cloud OpenAPI Explorerは、デバッグ機能、SDK、サンプル、および関連ドキュメントを提供します。 OpenAPI Explorerを使用して、リクエストを手動でカプセル化したり署名したりすることなく、Log Service API操作をデバッグできます。 詳細については、をご覧ください。 OpenAPIポータル

  • Log Serviceは、Log Serviceの自動設定の要件を満たすコマンドラインインターフェイス (CLI) を提供します。 詳細については、「Log Service CLI」をご参照ください。

  • プロジェクト関連のAPI操作の詳細については、以下のトピックを参照してください。

  • サンプルコードの詳細については、GitHubの「Alibaba Cloud Log Service SDK For Java」をご参照ください。