日志服务支持您在数据库(例如MySQL)中使用JDBC协议连接日志服务,并通过标准的SQL92语法查询和分析日志。
前提条件
已创建阿里云账号或RAM用户的AccessKey。具体操作,请参见访问密钥。
如果使用RAM用户的AccessKey,则RAM用户必须是当前Project所属阿里云账号的RAM用户,且具备Project级别的读权限。如何授权,请参见配置权限助手。
已创建Logstore。具体操作,请参见创建Logstore。
版本说明
目前日志服务仅支持JDBC 5.1.49版本。
连接参数
此处以MySQL数据库为例,介绍连接参数。
MySQL JDBC不支持分页。
连接语法的格式
mysql -h host -u user -p password -P port use database;
示例
mysql -h my-project.cn-hangzhou-intranet.log.aliyuncs.com -u bq****mo86kq -p 4f****uZP -P 10005 use my-project;
参数说明
参数
说明
host
日志服务访问域名,必须添加Project名称,即格式为
Project名称.私网服务入口
。例如my-project.cn-hangzhou-intranet.log.aliyuncs.com。目前仅支持私网服务入口(阿里云经典网络和VPC网络的访问域名)。更多信息,请参见私网。
port
默认使用10005。
user
阿里云AccessKey ID。建议使用RAM用户的AccessKey。
password
阿里云AccessKey Secret。建议使用RAM用户的AccessKey。
database
日志服务Project名称。
重要一个数据库同时只支持连接一个Project。
table
日志服务Logstore名称。
重要您需在查询和分析语句中指定Logstore。
查询和分析语法
过滤语法
说明在where条件中必须包含__date__字段或__time__字段来限制查询的时间范围。__date__字段是timestamp类型,__time__字段是bigint类型。例如:
__date__ > '2017-08-07 00:00:00' and __date__ < '2017-08-08 00:00:00'
__time__ > 1502691923 and __time__ < 1502692923
where过滤语法说明如下表所示。
语义
示例
说明
字符串搜索
key = "value"
查询的是分词之后的结果。
字符串模糊搜索
key has 'valu*'
key like 'value_%'
查询的是分词之后模糊匹配的结果。
数值比较
num_field > 1
比较运算符包括>、>=、=、<和<=。
逻辑运算
and or not
例如a = "x" and b ="y"或a = "x" and not b ="y"。
全文搜索
__line__ ="abc"
如果使用全文索引搜索,需使用特殊的key(__line__)。
计算语法
支持计算操作符。更多信息,请参见分析语法。
SQL92语法
过滤语法和计算语法组合成为SQL92语法。
status>200 |select avg(latency),max(latency) ,count(1) as c GROUP BY method ORDER BY c DESC LIMIT 20
您可以将上述查询和分析语句中的分析语句与时间条件组合成为查询条件,变成标准SQL92语法,如下所示:
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
通过JDBC协议访问日志服务
程序调用
您可以在任何一个支持MySQL connector的程序中使用MySQL语法连接日志服务。例如JDBC、Python MySQLdb,示例如下:
import com.mysql.jdbc.*; import java.sql.*; import java.sql.Connection; import java.sql.ResultSetMetaData; import java.sql.Statement; public class testjdbc { public static void main(String args[]){ Connection conn = null; Statement stmt = null; try { //STEP 1: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); //STEP 2: Open a connection System.out.println("Connecting to a selected database..."); conn = DriverManager.getConnection("jdbc:mysql://projectname.cn-hangzhou-intranet.log.aliyuncs.com:10005/sample-project","accessid","accesskey"); System.out.println("Connected database successfully..."); //STEP 3: Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql = "SELECT method,min(latency,10) as c,max(latency,10) from sample-logstore where __time__>=1500975424 and __time__ < 1501035044 and latency > 0 and latency < 6142629 and not (method='Postlogstorelogs' or method='GetLogtailConfig') group by method " ; String sql_example2 = "select count(1) ,max(latency),avg(latency), histogram(method),histogram(source),histogram(status),histogram(clientip),histogram(__source__) from test10 where __date__ >'2017-07-20 00:00:00' and __date__ <'2017-08-02 00:00:00' and __line__='abc#def' and latency < 100000 and (method = 'getlogstorelogS' or method='Get**' and method <> 'GetCursorOrData' )"; String sql_example3 = "select count(1) from sample-logstore where __date__ > '2017-08-07 00:00:00' and __date__ < '2017-08-08 00:00:00' limit 100"; ResultSet rs = stmt.executeQuery(sql); //STEP 4: Extract data from result set while(rs.next()){ //Retrieve by column name ResultSetMetaData data = rs.getMetaData(); System.out.println(data.getColumnCount()); for(int i = 0;i < data.getColumnCount();++i) { String name = data.getColumnName(i+1); System.out.print(name+":"); System.out.print(rs.getObject(name)); } 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(); } } } } }
工具类调用
在经典网络或VPC网络中通过MySQL客户端进行连接。
①、②处配置为您的Project。
③处配置为您的Logstore。