All Products
Search
Document Center

Simple Log Service:Use Simple Log Service SDK for Python to use the Dedicated SQL feature

Last Updated:Sep 18, 2024

This topic describes how to use Simple Log Service SDK for Python to use the Dedicated SQL feature.

Prerequisites

  • A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.

  • The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables in Linux, macOS, and Windows.

    Important
    • The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.

    • We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.

  • Simple Log Service SDK for Python V0.7.5 or later is installed. For more information, see Install Simple Log Service SDK for Python.

Background information

Simple Log Service provides the Dedicated SQL feature to enhance SQL analysis capabilities. You can use this feature to process hundreds of billions of data records. For more information, see Enable Dedicated SQL.

Simple Log Service provides the execute_logstore_sql and execute_project_sql operations. You can call the operations to use the Dedicated SQL feature in an efficient manner.

  • execute_logstore_sql: uses the Dedicated SQL feature in a specified Logstore. This operation supports the SQL-92 syntax. A query statement is in the Search statement|Analytic statement format, and the analytic statement follows the standard SQL-92 syntax.

  • execute_project_sql: uses the Dedicated SQL feature in a specified project. This operation supports the SQL-92 syntax. You must specify a filter condition and a time range in the WHERE clause of an SQL statement.

Note

If you want to filter data before you analyze the data, we recommend that you call the execute_logstore_sql operation and specify a query statement in the Search statement|Analytic statement format to improve analysis efficiency.

Sample code that is used to use the Dedicated SQL feature

The following sample code provides an example on how to use the Dedicated SQL feature. For more information, see aliyun-log-python-sdk.

# encoding: utf-8
from __future__ import print_function

import time
import os
from aliyun.log import *


def main():
    # 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'
    # Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
    access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
    access_key = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
    # The name of the project. 
    project_name = 'aliyun-test-project'
    # The name of the Logstore. 
    logstore_name = 'aliyun-test-logstore'

    # Create a Simple Log Service client. 
    client = LogClient(endpoint, access_key_id, access_key)

    # Execute an SQL statement in the specified Logstore. 
    print("===sample_execute_logstore_sql===")
    res = client.execute_logstore_sql(project_name, logstore_name,
                                      int(time.time() - 60),int(time.time()),
                                      "* | select count(1) as cnt",True)
    # Display the statistics about the analysis results. 
    res.log_print()
    # The number of rows of log data that is processed. 
    print("processed_rows: %s" % res.get_processed_rows())
    # The time that is consumed to execute the SQL statement. 
    print("elapsed_mills: %s" % res.get_elapsed_mills())
    # Display whether an SQL statement is used. 
    print("has_sql: %s" % res.get_has_sql())
    # The WHERE clause that precedes the vertical bar (|). 
    print("where_query: %s" % res.get_where_query())
    # The SELECT statement that follows the vertical bar (|). The SELECT statement contains an aggregate function. 
    print("agg_query: %s" % res.get_agg_query())
    # The CPU time that is consumed to execute the SQL statement after the Dedicated SQL feature is enabled. Unit: seconds. You are charged for the Dedicated SQL feature based on the CPU time. For more information, see the topics that are related to billable items. 
    print("cpu_sec: %s" % res.get_cpu_sec())
    # The number of CPU cores that are used to execute the SQL statement after the Dedicated SQL feature is enabled. 
    print("cpu_cores: %s" % res.get_cpu_cores())


    # Execute an SQL statement in the specified project. 
    print("===sample_execute_project_sql===")
    res = client.execute_project_sql(project_name,"select count(1) as cnt from %s where __time__ > %s"
                                     % (logstore_name, int(time.time() - 60)),True)
    # Display the statistics about the analysis results. 
    res.log_print()
    # The number of rows of log data that is processed. 
    print("processed_rows: %s" % res.get_processed_rows())
    # The time that is consumed to execute the SQL statement. 
    print("elapsed_mills: %s" % res.get_elapsed_mills())
    # Display whether an SQL statement is used. 
    print("has_sql: %s" % res.get_has_sql())
    # The WHERE clause that precedes the vertical bar (|). 
    print("where_query: %s" % res.get_where_query())
    # The SELECT statement that follows the vertical bar (|). The SELECT statement contains an aggregate function. 
    print("agg_query: %s" % res.get_agg_query())
    # The CPU time that is consumed to execute the SQL statement after the Dedicated SQL feature is enabled. Unit: seconds. You are charged for the Dedicated SQL feature based on the CPU time. For more information, see the topics that are related to billable items. 
    print("cpu_sec: %s" % res.get_cpu_sec())
    # The number of CPU cores that are used to execute the SQL statement after the Dedicated SQL feature is enabled. 
    print("cpu_cores: %s" % res.get_cpu_cores())


if __name__ == '__main__':
    main()
  • execute_logstore_sql operation

    You can call the execute_logstore_sql operation to use the Dedicated SQL feature. You must create a request in the res = client.execute_logstore_sql(project, logstoreName, from, to, query, powerSql) format. The following table describes the request parameters.

    Parameter

    Type

    Required

    Example

    Description

    project_name

    String

    Yes

    N/A

    The name of the project.

    When you create a Simple Log Service client, you must specify a value for the project_name parameter. Therefore, you do not need to configure the parameter again.

    logstore_name

    String

    Yes

    N/A

    The name of the Logstore.

    When you create a Simple Log Service client, you must specify a value for the logstore_name parameter. Therefore, you do not need to configure the parameter again.

    from

    Long

    Yes

    int(time.time() - 60)

    The beginning of the time range to query. The value is a UNIX timestamp representing the number of seconds that have elapsed since the epoch time January 1, 1970, 00:00:00 UTC.

    to

    Long

    Yes

    int(time.time())

    The end of the time range to query. The value is a UNIX timestamp representing the number of seconds that have elapsed since the epoch time January 1, 1970, 00:00:00 UTC.

    query

    String

    Yes

    "* | select count(1) as cnt"

    The query statement. Format: Search statement|Analytic statement. For more information, see Syntax.

    By default, Simple Log Service returns 100 rows of data. You can use a LIMIT clause to specify the number of data rows to return. For more information, see LIMIT clause.

    powerSql

    Boolean

    No

    True

    Specifies whether to use the Dedicated SQL feature. For more information, see Enable Dedicated SQL.

    • True: The Dedicated SQL feature is used.

    • False (default): The Standard SQL feature is used.

  • execute_project_sql operation

    You can call the execute_project_sql operation to use the Dedicated SQL feature. You must create a request in the res = client.execute_project_sql(project, query, powerSql) format. The following table describes the request parameters.

    Parameter

    Type

    Required

    Example

    Description

    project_name

    String

    Yes

    aliyun-test-project

    The name of the project.

    When you create a Simple Log Service client, you must specify a value for the project_name parameter. Therefore, you do not need to configure the parameter again.

    query

    String

    Yes

    "select count(1) as cnt from %s where __time__ > %s" % (logstore_name, int(time.time() - 60))

    The SQL statement. You must specify the search condition in the WHERE clause of the SQL statement.

    By default, Simple Log Service returns 100 rows of data. You can use a LIMIT clause to specify the number of data rows to return. For more information, see LIMIT clause.

    powerSql

    Boolean

    No

    True

    Specifies whether to use the Dedicated SQL feature. For more information, see Enable Dedicated SQL.

    • True: The Dedicated SQL feature is used.

    • False (default): The Standard SQL feature is used.

Sample code that is used to create, modify, and query an SQL instance

After the Dedicated SQL feature is enabled, you can create an SQL instance to specify the number of CUs. You can change the number of CUs to control the cost of the Dedicated SQL feature. The following sample code provides an example on how to create, modify and query an SQL instance. For more information, see aliyun-log-python-sdk.

# encoding: utf-8
from __future__ import print_function

import time
import os
from aliyun.log import *

def main():
    # The Simple Log Service endpoint. For more information, see Endpoints. 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'
    # Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
    access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
    access_key = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
    # The name of the project. 
    project_name = 'aliyun-test-project'
    # The name of the Logstore. 
    logstore_name = 'aliyun-test-logstore'

    # Create a Simple Log Service client. 
    client = LogClient(endpoint, access_key_id, access_key)

    # Create an SQL instance. You can specify the number of CUs for the SQL instance. Valid values: 0 to 1000. 
    print("===sample_create_sql_instance===")
    res = client.create_sql_instance(project_name, 500)
    res.log_print()

    # Modify the configurations of the SQL instance. You can change the number of CUs for the SQL instance. Valid values: 0 to 1000. 
    print("===sample_update_sql_instance===")
    res = client.update_sql_instance(project_name, 800)
    res.log_print()

    # Query the configurations of the SQL instance. 
    print("===sample_list_sql_instance===")
    res = client.list_sql_instance(project_name)
    res.log_print()


if __name__ == '__main__':
    main()