All Products
Search
Document Center

Simple Log Service:Use SQL statements to query and analyze logs

Last Updated:Sep 05, 2024

To use SQL statements to query and analyze logs in Simple Log Service, you can use the Java Database Connectivity (JDBC), Python MySQLdb, and MySQL command-line tools to connect to Simple Log Service. This topic describes how to use JDBC, Python MySQLdb, and MySQL command-line tools to connect to Simple Log Service.

Limits

  • Simple Log Service supports only JDBC 5.1.49.

  • If you use drivers such as JDBC in Java or MySQL Connector in Python to access Simple Log Service, you must use only an internal endpoint of Simple Log Service. An internal endpoint can be accessed over the classic network or a virtual private cloud (VPC) of Alibaba Cloud. For more information, see Endpoints. If you use a different endpoint, the following error occurs: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure,Caused by: java.net.ConnectException: Connection timed out: connect.

Prerequisites

  • An AccessKey pair is created for an Alibaba Cloud account or a Resource Access Management (RAM) user. For more information, see AccessKey pair.

    If you want to use the AccessKey pair of a RAM user, make sure that the RAM user belongs to the Alibaba Cloud account of your project and the RAM user is granted the read permissions on the project. For more information, see Configure the permission assistant feature.

  • Data is collected to a source Logstore. For more information, see Data collection overview.

Background information

MySQL is a popular relational database service. A large number of software products can obtain MySQL data by using the MySQL protocol and SQL syntax. To meet log query and analysis requirements in different business scenarios and systems, Simple Log Service can be used as a MySQL database. You can use a standard MySQL connection tool to connect to Simple Log Service and then use standard SQL syntax to query and analyze logs. The following clients support the MySQL protocol: MySQL client, JDBC, and MySQL Connector/Python. For information about how to convert Simple Log Service query statements into SQL statements, see Search and analysis syntax.

Scenarios:

  • Use Simple Log Service as the data source to perform visual analysis in visualization tools such as DataV, Tableau, and Grafana.

  • Use JDBC or MySQL Connector/Python to connect to Simple Log Service to query, analyze, or use data in Simple Log Service in Java or Python programs.

The following table compares Simple Log Service and MySQL databases.

Simple Log Service

MySQL

Project

Database

Logstore

Table

Index

Table field

Important

When you use SQL statements to query and analyze logs in Simple Log Service, paging is not support.

Connect to Simple Log Service by using JDBC

  1. Create a Maven project and add the following JDBC dependency to the pom.xml file of the project. Sample code:

    <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.49</version>
    </dependency>
  2. Use a program that supports the MySQL connector to connect to Simple Log Service, such as JDBC and Python MySQLdb. Then, use the MySQL syntax to query data in Simple Log Service. In this example, JDBC is used.

    Important

    You must include the __date__ or __time__ field in a WHERE clause to limit the time range of a query. The data type of the __date__ field is timestamp. The data type of the __time__ field is bigint. Examples:

    • __date__ > '2017-08-07 00:00:00' and __date__ < '2017-08-08 00:00:00'

    • __time__ > 1502691923 and __time__ < 1502692923

    /**
    * Created by mayunlei on 2017/6/19.
    */
    import com.mysql.jdbc.*;
    import java.sql.*;
    import java.sql.Connection;
    import java.sql.Statement;
    /**
    * Created by mayunlei on 2017/6/15.
    */
    public class CollectTest {
     public static void main(String args[]){
         // The project name and an internal endpoint of Simple Log Service. Replace the name and endpoint with actual values. 
         final String endpoint = "trip-demo.cn-hangzhou-intranet.log.aliyuncs.com"; 
         // The port that is used for JDBC-based access. The default port is 10005. 
         final String port = "10005"; 
         // The name of the Simple Log Service project. 
         final String project = "trip-demo"; 
         // The name of the Simple Log Service Logstore. 
         final String logstore = "ebike";
         // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
         final String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");  
         final String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");  
         Connection conn = null;
         Statement stmt = null;
         try {
             // Step 1: Load the JDBC driver. 
             Class.forName("com.mysql.jdbc.Driver");
             // Step 2: Create a connection string. 
             conn = DriverManager.getConnection("jdbc:mysql://"+endpoint+":"+port+"/"+project+"?useSSL=false",accessKeyId,accessKey);
             // Step 3: Create a statement. 
             stmt = conn.createStatement();
             // Step 4: Specify a query statement to query the number of logs that were generated on October 11, 2017 and whose op field is set to unlock. 
             String sql = "select count(1) as pv from "+logstore+"  " +
                     "where     __date__  >=  '2017-10-11 00:00:00'   " +
                     "     and  __date__  <   '2017-10-12 00:00:00'" +
                     " and     op ='unlock'";
             // Step 5: Execute the query statement. 
             ResultSet rs = stmt.executeQuery(sql);
             // Step 6: Extract the query and analysis results. 
             while(rs.next()){
                 // Retrieve by column name
                 System.out.print("pv:");
                 // Display pv in the results. 
                 System.out.print(rs.getLong("pv"));
                 System.out.println();
             }
             rs.close();
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         } catch (SQLException e) {
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             if (stmt != null) {
                 try {
                     stmt.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }
             if (conn != null) {
                 try {
                     conn.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }
         }
     }
    }

    Parameter

    Description

    Required

    Example

    project

    Target project name.

    Yes

    trip-demo

    endpoint

    The Internal endpoint of Simple Log Service.

    Yes

    cn-hangzhou-intranet.log.aliyuncs.com

    port

    The port that is used for JDBC-based access.

    Yes

    10005

    accessId

    The AccessKey ID that is used to identify a user.

    Yes

    LT*******************KX

    accessKey

    The AccessKey secret that is used to encrypt and verify a signature string.

    Yes

    -

  3. Query data.

    image

Connect to Simple Log Service by using MySQL Connector/Python

  1. In the command-line tool, run one of the following commands as an administrator to install mysql-connector-python:

    pip install mysql-connector-python # Command for Python 2.
    pip3 install mysql-connector-python # Command for Python 3.
  2. Create the mysqlQuery.py file and specify the parameters in the following code based on your business requirements.

    Important

    You must include the __date__ or __time__ field in a WHERE clause to limit the time range of a query. The data type of the __date__ field is timestamp. The data type of the __time__ field is bigint. Examples:

    • __date__ > '2017-08-07 00:00:00' and __date__ < '2017-08-08 00:00:00'

    • __time__ > 1502691923 and __time__ < 1502692923

    import mysql.connector
    import os
    
    
    def main():
        # The username.
        user = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
        # The password.
        password = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
        # The endpoint.
        host = 'cn-chengdu-intranet.log.aliyuncs.com'
        # The database name.
        project = 'aliyun-test-project'
        # The port number.
        port = 10005
        # The Logstore name.
        logstore = 'logstore-test'
        print("collect database start ")
        # Connect to the database.
        cnx = mysql.connector.connect(host=host, user=user, password=password, database=database, port=port)
        print("collect database success ")
        # Create a cursor object.
        cursor = cnx.cursor()
    
        # Execute a query.
        query = "select count(1) as pv from " + logstore + " where __date__  >=  '2024-05-27 00:00:00' and  __date__  <   '2024-05-31 00:00:00'"
        print("query data success")
        cursor.execute(query)
        print("query data success")
        # Obtain the query result.
        for (column1) in cursor:
            print("{}".format(column1))
    
        # Close the cursor and the connection.
        cursor.close()
        cnx.close()
    
    
    if __name__ == '__main__':
        main()
    

    Parameter

    Description

    Required

    Example

    project

    The destination project.

    Yes

    trip-demo

    logstore

    The destination Logstore.

    Yes

    logtore-test

    endpoint

    The internal endpoint provided by Alibaba Cloud.

    Yes

    cn-hangzhou-intranet.log.aliyuncs.com

    port

    The port number. Set the value to 10005.

    Yes

    10005

    user

    The AccessKey ID of your Alibaba Cloud account. We recommend that you use the AccessKey ID of a RAM user.

    Yes

    LT*******************KX

    password

    The AccessKey secret of your Alibaba Cloud account. We recommend that you use the AccessKey secret of a RAM user.

    Yes

    aaw************************Qf

  3. Run one of the following commands to query data:

    python mysqlQuery.py # Command for Python 2.
    python3 mysqlQuery.py # Command for Python 3.

    image

Connect to Simple Log Service by using an ECS instance

  1. Run the following command to connect to Simple Log Service:

    mysql -h my-project.host -uuser -ppassword -P port

    Example:

    mysql -h my-project.cn-hangzhou-intranet.log.aliyuncs.com -ubq****mo86kq -p4f****uZP -P 10005

    Parameter

    Description

    host

    The Simple Log Service endpoint. You must include a project name in the endpoint. You must enter an endpoint in the Project name.Internal endpoint format. Example: my-project.cn-hangzhou-intranet.log.aliyuncs.com.

    Only an internal endpoint that is accessible over the classic network or a virtual private cloud (VPC) is supported. For more information, see Endpoints.

    port

    The port that you can use to connect to Simple Log Service. Default value: 10005.

    user

    The AccessKey ID of your Alibaba Cloud account. We recommend that you use the AccessKey ID of a RAM user.

    password

    The AccessKey secret of your Alibaba Cloud account. We recommend that you use the AccessKey secret of a RAM user.

    my-project

    The name of the Simple Log Service project.

    Important

    A database can be connected to only one project at a time.

  2. Run the following command to enter the current project:

    use my-project;
  3. Query data.

    Important

    You must include the __date__ or __time__ field in a WHERE clause to limit the time range of a query. The data type of the __date__ field is timestamp. The data type of the __time__ field is bigint. Examples:

    • __date__ > '2017-08-07 00:00:00' and __date__ < '2017-08-08 00:00:00'

    • __time__ > 1502691923 and __time__ < 1502692923

    select count(1) as pv from my-logstore where __date__  >=  '2024-05-27 00:00:00' and  __date__  <   '2024-05-31 00:00:00'

    连接示例

    • Replace the values marked 1 and 2 with your project name.

    • Replace the value marked 3 with your Logstore name.

Search and analysis syntax

  • Filtering syntax

    Note

    You must include the __date__ or __time__ field in a WHERE clause to limit the time range of a query. The data type of the __date__ field is timestamp. The data type of the __time__ field is bigint. Examples:

    • __date__ > '2017-08-07 00:00:00' and __date__ < '2017-08-08 00:00:00'

    • __time__ > 1502691923 and __time__ < 1502692923

    The following table describes the filtering syntax of a WHERE clause.

    Semantics

    Example

    Description

    String search

    key = "value"

    Queries data after tokenization.

    Fuzzy string search

    • key has 'valu*'

    • key like 'value_%'

    Queries data in fuzzy match mode after tokenization.

    Numeric value comparison

    num_field > 1

    Queries data by using comparison operators. The following comparison operators are supported: greater-than (>), greater-than-or-equal-to (>=), less-than (<), less-than-or-equal-to (<=), and equal-to (=).

    Logical operation

    and or not

    Queries data based on logical operations. Examples: a = "x" and b ="y" and a = "x" and not b ="y".

    Full-text search

    __line__ ="abc"

    Queries data based on full-text search. If you perform a full-text search, you must use the __line__ key.

  • Calculation syntax

    Calculation operators are supported. For more information, see Log analysis overview.

  • SQL-92 syntax

    In the following example, the query statement uses the SQL-92 syntax, which includes filtering and calculation syntax.

    status>200 |select avg(latency),max(latency) ,count(1) as c GROUP BY  method  ORDER BY c DESC  LIMIT 20

    In the following example, the SQL statement uses the standard SQL-92 syntax, which includes the analytic statement in the preceding query statement and a time condition.

    select avg(latency),max(latency) ,count(1) as c from sample-logstore where status>200 and __time__>=1500975424 and __time__ < 1501035044 GROUP BY  method  ORDER BY c DESC  LIMIT 20