インデックスは、キーワードと論理ポインタからなる逆ストレージ構造です。 論理ポインタは、実際のデータを参照することができる。 インデックスを使用すると、キーワードに基づいてデータ行をすばやく見つけることができます。 インデックスはデータカタログに似ています。 インデックスを設定した後にのみ、ログデータをクエリおよび分析できます。 このトピックでは、Simple Log Service SDK for Pythonを使用してインデックスを作成、変更、クエリ、および削除する方法とサンプルコードについて説明します。
前提条件
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ペアが漏洩し、アカウント内のすべてのリソースのセキュリティが侵害される可能性があります。
Python用のSimple Log Service SDKがインストールされています。 詳細については、「Simple Log Service SDK For Pythonのインストール」をご参照ください。
ログはLogstoreに書き込まれます。 詳細については、「データ収集の概要」をご参照ください。
使用上の注意
この例では、中国 (杭州) リージョンのパブリック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のサポートされているリージョンとエンドポイントの詳細については、「エンドポイント」をご参照ください。
生ログ
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インデックスの作成に使用されるサンプルコード
Simple Log Serviceコンソールでインデックスを簡単に管理できます。 詳細については、「インデックスの作成」をご参照ください。
次のサンプルコードは、インデックスの作成方法の例を示しています。 この例では、フルテキストインデックスが有効になっており、request_methodフィールドとstatusフィールドに対してフィールドインデックスが有効になっています。 この例は生ログに基づいています。

from aliyun.log import LogClient, IndexConfig
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 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.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of the project.
project_name = "ali-test-project"
# The name of the Logstore.
logstore_name = "ali-test-logstore"
if __name__ == '__main__':
# Before you create indexes, you must plan the configurations of full-text indexing and field indexing. In this example, full-text indexing is enabled, and field indexing is enabled for the request_method and status fields.
logstore_index = {'line': {
'token': [',', ' ', "'", '"', ';', '=', '(', ')', '[', ']', '{', '}', '?', '@', '&', '<', '>', '/', ':', '\n',
'\t',
'\r'], 'caseSensitive': False, 'chn': False}, 'keys': {'request_method': {'type': 'text',
'token': [',', ' ', "'", '"', ';', '=',
'(', ')', '[', ']', '{', '}',
'?', '@', '&', '<', '>', '/',
':', '\n', '\t', '\r'],
'caseSensitive': False, 'alias': '',
'doc_value': True, 'chn': False},
'status': {'type': 'long', 'alias': '',
'doc_value': True}},
'log_reduce': False,
'max_text_len': 2048}
print("ready to create index")
index_config = IndexConfig()
index_config.from_json(logstore_index)
client.create_index(project_name, logstore_name, index_config)
print("create index success ")期待される結果
ready to create index
create index successインデックスの変更に使用されるサンプルコード
Simple Log Serviceコンソールでインデックスを簡単に管理できます。 詳細については、「インデックスの作成」をご参照ください。
次のサンプルコードは、インデックスの変更方法の例を示しています。 この例では、フルテキストインデックスが有効になっており、request_methodフィールドとstatusフィールドでフィールドインデックスが有効になっており、request_methodフィールドではcaseSensitiveがTrueに設定されています。 この例は生ログに基づいています。

from aliyun.log import LogClient, IndexConfig
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 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.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of the project.
project_name = "ali-test-project"
# The name of the Logstore.
logstore_name = "ali-test-logstore"
if __name__ == '__main__':
# Set caseSensitive to True for the request_method field.
logstore_index = {'line': {
'token': [',', ' ', "'", '"', ';', '=', '(', ')', '[', ']', '{', '}', '?', '@', '&', '<', '>', '/', ':', '\n',
'\t',
'\r'], 'caseSensitive': False, 'chn': False}, 'keys': {'request_method': {'type': 'text',
'token': [',', ' ', "'", '"', ';', '=',
'(', ')', '[', ']', '{', '}',
'?', '@', '&', '<', '>', '/',
':', '\n', '\t', '\r'],
'caseSensitive': True, 'alias': '',
'doc_value': True, 'chn': False},
'status': {'type': 'long', 'alias': '',
'doc_value': True}},
'log_reduce': False,
'max_text_len': 2048}
print("ready to update index")
index_config = IndexConfig()
index_config.from_json(logstore_index)
client.update_index(project_name, logstore_name, index_config)
print("update index success ")期待される結果
ready to update index
update index successインデックスのクエリに使用されるサンプルコード
次のサンプルコードは、指定したLogstoreのインデックスを照会する方法の例を示しています。
from aliyun.log import LogClient, IndexConfig
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 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.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of the project.
project_name = "ali-test-project"
# The name of the Logstore.
logstore_name = "ali-test-logstore"
if __name__ == '__main__':
# Query indexes.
print("ready to list index")
res = client.get_index_config(project_name, logstore_name)
print("The index config is :%s" % res.get_index_config().to_json())
print("list index success ")期待される結果
ready to list index
The index config is :{'line': {'token': [',', ' ', "'", '"', ';', '=', '(', ')', '[', ']', '{', '}', '?', '@', '&', '<', '>', '/', ':', '\n', '\t', '\r'], 'caseSensitive': False, 'chn': False}, 'keys': {'request_method': {'type': 'text', 'token': [',', ' ', "'", '"', ';', '=', '(', ')', '[', ']', '{', '}', '?', '@', '&', '<', '>', '/', ':', '\n', '\t', '\r'], 'caseSensitive': True, 'alias': '', 'doc_value': True, 'chn': False}, 'status': {'type': 'long', 'alias': '', 'doc_value': True}}, 'log_reduce': False, 'max_text_len': 2048}
list index successインデックスの削除に使用されるサンプルコード
次のサンプルコードは、指定したLogstoreのインデックスを削除する方法の例を示しています。
from aliyun.log import LogClient, IndexConfig
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 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.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of the project.
project_name = "ali-test-project"
# The name of the Logstore.
logstore_name = "ali-test-logstore2"
if __name__ == '__main__':
# Delete indexes.
print("ready to delete index")
client.delete_index(project_name, logstore_name)
print("delete index success ")期待される結果
ready to delete index
delete index success関連ドキュメント
Alibaba Cloud OpenAPI Explorerは、デバッグ機能、SDK、サンプル、および関連ドキュメントを提供します。 OpenAPI Explorerを使用して、リクエストを手動でカプセル化したり署名したりすることなく、Log Service API操作をデバッグできます。 詳細については、をご覧ください。 OpenAPIポータル。
Log Serviceは、Log Serviceの自動設定の要件を満たすコマンドラインインターフェイス (CLI) を提供します。 詳細については、「Log Service CLI」をご参照ください。
インデックス関連のAPI操作の詳細については、以下のトピックを参照してください。
サンプルコードの詳細については、GitHubの「Alibaba Cloud Simple Log Service SDK For Python」をご参照ください。