Büyük ölçekli günlük analizi için Özel SQL özelliğiyle SQL sorguları çalıştırmak üzere Simple Log Service SDK for Python'ı kullanabilirsiniz.
Önkoşullar
Simple Log Service etkinleştirilmiştir.
Simple Log Service SDK for Python başlatılmıştır.
Arka plan bilgileri
Simple Log Service, SQL analiz yeteneklerini geliştirmek ve yüz milyarlarca veri kaydını işlemek için Özel SQL özelliğini sunar. Daha fazla bilgi için bkz. Yüksek performanslı tam doğrulukta sorgu ve analiz (Özel SQL).
Simple Log Service, Özel SQL özelliğini kullanmak için execute_logstore_sql ve execute_project_sql işlemlerini sağlar.
execute_logstore_sql: Belirtilen bir Logstore'da Özel SQL özelliğini çalıştırır. Bu işlem SQL-92 sözdizimini destekler. Sorgu ifadesi
Arama ifadesi|Analitik ifadebiçimindedir ve analitik ifade standart SQL-92 sözdizimini izler.execute_project_sql: Belirtilen bir projede Özel SQL özelliğini çalıştırır. Bu işlem SQL-92 sözdizimini destekler. SQL ifadesinin WHERE yan tümcesinde bir filtre koşulu ve zaman aralığı belirtmeniz gerekir.
Analizden önce verileri filtrelemek için daha iyi verimlilik amacıyla execute_logstore_sql işlemini çağırmanızı ve Arama ifadesi|Analitik ifade biçiminde bir sorgu ifadesi belirtmenizi öneririz.
Özel SQL özelliğini kullanmaya yönelik örnek kod
İşlem açıklaması
-
execute_logstore_sql işlemi
Özel SQL özelliğini kullanmak için execute_logstore_sql işlemini çağırabilirsiniz. İsteği
res = client.execute_logstore_sql(project, logstoreName, from, to, query, powerSql)biçiminde oluşturun. Aşağıdaki tabloda istek parametreleri açıklanmaktadır.Parametre
Tür
Gerekli
Örnek
Açıklama
project_name
String
Evet
Yok
Projenin adı.
Bu değer, Simple Log Service istemcisini oluştururken belirtilir, bu nedenle yeniden yapılandırmanız gerekmez.
logstore_name
String
Evet
Yok
Logstore'un adı.
Bu değer, Simple Log Service istemcisini oluştururken belirtilir, bu nedenle yeniden yapılandırmanız gerekmez.
from
Long
Evet
int(time.time() - 60)
Sorgulanacak zaman aralığının başlangıcı. Değer, 1 Ocak 1970, 00:00:00 UTC epoch zamanından bu yana geçen saniye sayısını temsil eden bir UNIX zaman damgasıdır.
to
Long
Evet
int(time.time())
Sorgulanacak zaman aralığının sonu. Değer, 1 Ocak 1970, 00:00:00 UTC epoch zamanından bu yana geçen saniye sayısını temsil eden bir UNIX zaman damgasıdır.
query
String
Evet
"* | select count(1) as cnt"
Sorgu ifadesi. Biçim:
Arama ifadesi|Analitik ifade. Daha fazla bilgi için bkz. Sözdizimi.Simple Log Service varsayılan olarak 100 satır veri döndürür. Döndürülecek veri satırı sayısını belirtmek için bir LIMIT yan tümcesi kullanabilirsiniz. Daha fazla bilgi için bkz. LIMIT yan tümcesi.
powerSql
Boolean
Hayır
True
Özel SQL özelliğinin kullanılıp kullanılmayacağını belirtir. Daha fazla bilgi için bkz. Yüksek performanslı tam doğrulukta sorgu ve analiz (Özel SQL).
-
True: Özel SQL özelliği kullanılır.
-
False (varsayılan): Standart SQL özelliği kullanılır.
-
-
execute_project_sql işlemi
Özel SQL özelliğini kullanmak için execute_project_sql işlemini çağırabilirsiniz. İsteği
res = client.execute_project_sql(project, query, powerSql)biçiminde oluşturun. Aşağıdaki tabloda istek parametreleri açıklanmaktadır.Parametre
Tür
Gerekli
Örnek
Açıklama
project_name
String
Evet
aliyun-test-project
Projenin adı.
Bu değer, Simple Log Service istemcisini oluştururken belirtilir, bu nedenle yeniden yapılandırmanız gerekmez.
query
String
Evet
"select count(1) as cnt from %s where __time__ > %s" % (logstore_name, int(time.time() - 60))
SQL ifadesi. SQL ifadesinin WHERE yan tümcesinde arama koşulunu belirtmeniz gerekir.
Simple Log Service varsayılan olarak 100 satır veri döndürür. Döndürülecek veri satırı sayısını belirtmek için bir LIMIT yan tümcesi kullanabilirsiniz. Daha fazla bilgi için bkz. LIMIT yan tümcesi.
powerSql
Boolean
Hayır
True
Özel SQL özelliğinin kullanılıp kullanılmayacağını belirtir. Daha fazla bilgi için bkz. Yüksek performanslı tam doğrulukta sorgu ve analiz (Özel SQL).
-
True: Özel SQL özelliği kullanılır.
-
False (varsayılan): Standart SQL özelliği kullanılır.
-
Örnek kod
Aşağıdaki örnek kod, Özel SQL özelliğinin nasıl kullanılacağını gösterir. Daha fazla bilgi için bkz. aliyun-log-python-sdk.
# encoding: utf-8
from __future__ import print_function
import time
import os
from aliyun.log import *
def main():
# Specify a Simple Log Service endpoint.
endpoint = 'ap-southeast-1.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()
SQL örneği oluşturmak, değiştirmek ve sorgulamak için örnek kod
Özel SQL özelliği etkinleştirildikten sonra CU sayısını belirtmek için bir SQL örneği oluşturabilirsiniz. Maliyetleri kontrol etmek için CU sayısını ayarlayabilirsiniz. Daha fazla bilgi için bkz. Yüksek performanslı tam doğrulukta sorgu ve analiz (Özel SQL).
Aşağıdaki örnek kod, bir SQL örneğinin nasıl oluşturulacağını, değiştirileceğini ve sorgulanacağını gösterir. Daha fazla bilgi için bkz. 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 = 'ap-southeast-1.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()