This topic describes how to connect to Simple Log Service by using Java Database Connectivity (JDBC). This topic also describes how to query and analyze logs by using the MySQL protocol and SQL syntax.
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 connect to Simple Log Service by using JDBC, you must be familiar with the SQL syntax. Simple Log Service allows you to query and analyze logs based on the MySQL protocol. You can use a standard MySQL client to connect to Simple Log Service. Then, you can use standard SQL syntax to query and analyze logs. The following clients support the MySQL protocol: MySQL client, JDBC, and MySQL Connector.
Use scenarios:
Use visualization tools such as DataV, Tableau, or Grafana to connect to Simple Log Service over the MySQL protocol.
When you need to query, analyze, or use data from Simple Log Service in a Java or Python program, you can connect to Simple Log Service by using JDBC or MySQL Connector.
The following table shows a comparison between Simple Log Service and MySQL databases.
Simple Log Service | MySQL databases |
Simple Log Service | MySQL databases |
Project | Database |
Logstore | Table |
Index | Table column |
Important
In Simple Log Service, SQL queries do not support pagination.
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
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.
Field indexes are configured for the required fields and Enable Analytics is turned on for the fields. For more information, see Create indexes.
Sample log
Each log of a bike-sharing service contains information about a user. The information includes the age, gender, battery usage, bike ID, operation latency, latitude, lock type, longitude, operation type, operation result, and unlocking type. The logs are stored in the ebike logstore of the trip-demo project. The project resides in the Singapore region.
Sample log:
Time :10-12 14:26:44
__source__: 192.168.0.0
__topic__: v1
age: 55
battery: 118497.673842
bikeid: 36
gender: male
latency: 17
latitude: 30.2931185245
lock_type: smart_lock
longitude: 120.052840484
op: unlock
op_result: ok
open_lock: bluetooth
userid: 292
JDBC-based query and analysis
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>
Create a Java program to query logs by using JDBC. The following sample code shows how to create a Java program to query logs by using JDBC.
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:
JDBC connection format: jdbc:mysql://{project}.{endpoint}:10005/{project}
. For more information about {project}
and {endpoint}
, see Parameter description.
import com.mysql.jdbc.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.Statement;
public class CollectTest {
public static void main(String args[]){
final String endpoint = "ap-southeast-1-intranet.log.aliyuncs.com";
final String port = "10005";
final String project = "trip-demo";
final String logstore = "ebike";
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 {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://"+endpoint+":"+port+"/"+project+"?useSSL=false",accessKeyId,accessKey);
stmt = conn.createStatement();
String sql = "select count(1) as pv,avg(latency) as avg_latency from "+logstore+" " +
"where __date__ >= '2017-10-11 00:00:00' " +
" and __date__ < '2017-10-12 00:00:00'" +
" and op ='unlock'";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
System.out.print("pv:");
System.out.print(rs.getLong("pv"));
System.out.print(" ; avg_latency:");
System.out.println(rs.getDouble("avg_latency"));
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 |
Parameter | Description | Required | Example |
project | Target project name. | Yes | trip-demo |
endpoint | The internal endpoint of Simple Log Service. | Yes | ap-southeast-1-intranet.log.aliyuncs.com |
port | The port that is used for JDBC-based access. The value is fixed to 10005. | 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 | - |